0

I have folders structure like this.

C:\MyFolder\http+++one\
C:\MyFolder\http+++two\
C:\MyFolder\http+++three\
C:\MyFolder\Other+++One\
C:\MyFolder\Other+++Two\
C:\MyFolder\Other+++Three\

Need to copy only folders starting with Other+++ and DO NOT copy folders starting with http+++.

Now problem is number of folders with other and http+++ and other+++ prefix is dynamic it grows more every day so i can't exclude them by specifying each folder manually, have to use wildcard.

Need a working solution similar to this:

robocopy "C:\MyFolder" "D:\MyFolder" /MIR /XJ /NDL /NP /TEE /S /XD "C:\MyFolder\http*"

Obviously above command does not work.

4
  • I have an idea for a workaround, is there anything you are unwilling to do or try to execute your command with the necessary exclusions. Like incorporating a little PowerShell helper or batch helper perhaps that in the end would still execute the Robocopy command as you currently use. I have an idea but want to make sure there is not any "it absolutely cannot use xyx" type thing. Commented Jul 2, 2023 at 0:40
  • 1
    This might work : /XD http*.
    – harrymc
    Commented Jul 2, 2023 at 8:11
  • superuser.com/q/1353959/705502 Commented Jul 2, 2023 at 14:12
  • As harrymc points out, it's just a syntax error in the command as given. The XD parameter should be a relative path not an absolute one.
    – Doug
    Commented Jul 3, 2023 at 15:31

3 Answers 3

2
+25

As robocopy searches for wildcards anywhere in the path string, a simple syntax to exclude these folders is:

/XD http*
4
  • I'd also throw in /E but only because my test was with empty folders.
    – Doug
    Commented Jul 3, 2023 at 15:27
  • According to the official documentation, this would not work (See this answer), however, it appears that it works. Does anybody know why this is the case? I wrote my answer without testing this, as I thought this would not work. I must admit this is much cleaner than my answer. Commented Jul 5, 2023 at 11:17
  • @ComputerUser121212: I also saw the answer in your link, but it's simply an incorrect understanding of the text : "/xd <Directory>[ ...] Excludes directories that match the specified names and paths". Note the distinction between "names" and "paths".
    – harrymc
    Commented Jul 5, 2023 at 11:21
  • @RomanToasov: Does this answer work for you ?
    – harrymc
    Commented Jul 7, 2023 at 9:00
1

Given that you are using robocopy, a Windows-only utility, I'm assuming you can run a batch script.
The following script will do what you are looking for, and it does not need to be run from any of the folders in question, in case you cannot write to them.

@echo off
setlocal enableextensions enabledelayedexpansion
for /f "delims=" %%A in ('dir /a:d /b "C:/MyFolder"') do ( 
    set folder=%%A
    set firstFourChars=!folder:~0,4!
    if NOT "!firstFourChars!" == "http" (robocopy "C:/MyFolder/%%A" "D:/MyFolder/%%A" /MIR /XJ /NDL /NP /TEE /S)
)

Explanation:

@echo off declutters the output, stopping the interpreter from saying every command it runs.
setlocal enableextensions enabledelayedexpansion makes the for loop work, by making the instructions contained within (...) to be executed one by one, and allowing access to current state of variables with !var! syntax.
for /f "delims=" %%A in ('dir /a:d /b "C:/MyFolder"') do (...) loops over all folders in C:/MyFolder. "delims=" is necessary to support folder name with spaces.
set folder=%%A sets the name of each folder to the variable called folder.
set firstFourChars=!folder:~0,4! sets the first four characters of every folder's name to the variable named firstFourChars.
if NOT "!firstFourChars!" == "http" (robocopy "C:/MyFolder/%%A" "D:/MyFolder/%%A" /MIR /XJ /NDL /NP /TEE /S) runs your robocopy command only if said first four characters do not match the literal http.

0

Appears to be duplicate of Robocopy exclude directories with wildcard but I can't close. You want to have a look at the duplicate anyway, specially this answer: https://superuser.com/a/1671577/705502

1
  • The subject in that post is the same, but none of the answers is useful.
    – harrymc
    Commented Jul 3, 2023 at 14:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .