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.

Roman Toasov
  • 205
  • 2
  • 9
  • 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. – Vomit IT - Chunky Mess Style Jul 02 '23 at 00:40
  • 1
    This might work : `/XD http*`. – harrymc Jul 02 '23 at 08:11
  • https://superuser.com/q/1353959/705502 – Joep van Steen Jul 02 '23 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 Jul 03 '23 at 15:31

3 Answers3

2

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

/XD http*
harrymc
  • 455,459
  • 31
  • 526
  • 924
  • I'd also throw in /E but only because my test was with empty folders. – Doug Jul 03 '23 at 15:27
  • According to the official documentation, this would not work (See [this answer](https://superuser.com/a/1417671/918341)), 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. – ComputerUser121212 Jul 05 '23 at 11:17
  • @ComputerUser121212: I also saw the answer in your link, but it's simply an incorrect understanding of the text : "/xd [ ...] Excludes directories that match the specified **names** and paths". Note the distinction between "names" and "paths". – harrymc Jul 05 '23 at 11:21
  • @RomanToasov: Does this answer work for you ? – harrymc Jul 07 '23 at 09: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.

ComputerUser121212
  • 600
  • 2
  • 5
  • 21
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

Joep van Steen
  • 4,730
  • 1
  • 17
  • 34