user406871
user406871

Reputation:

Batch file loop to grab all folders in a directory except for a certain folder

Just wondering the best way to do this in a single batch file. I have a folder called C:\Program\Foo and I want to grab all the folders except for the testing folder inside of foo, and I want to xcopy into D:\ so in D:\ foo will be there but no test folder.

Is there a way I can loop through each folder and check for a certain name not to include?

using /Exclude would mean I would need an extra text file with "Testing" in it

Upvotes: 2

Views: 2561

Answers (3)

Andriy M
Andriy M

Reputation: 77657

I don't see why you could not create a temporary exclusion file (using a temporary folder, that is):

@ECHO OFF
FOR %%F IN ("%TEMP%\exclude.txt") DO SET tmpf=%%~sF
ECHO Testing>%tmpf%\exclude.txt
XCOPY source destination /EXCLUDE:%tmpf%\exclude.txt other options

Note: XCOPY does not recognise double quotes as path delimiters in the /EXCLUDE option and offers no alternative for specifying paths with spaces, which can be a problem on Windows XP systems. This limitation can be worked around by replacing the original path with its counterpart consisting only of short names. That is what the FOR loop in the above script does for the %TEMP% folder.

Upvotes: 1

Nikki9696
Nikki9696

Reputation: 6348

Use the EXCLUDE option and put your exclusions in that file. That will let you exclude entire directories.

http://www.pcreview.co.uk/forums/using-xcopy-backup-can-exclude-some-directories-t489674.html

xcopy "c:\document and settings" "i:\documents and settings\" /s /d /EXCLUDE:c:\a.txt

a.txt contains

\temp\ \temporary internet files\

You may need to use shorter DOS file names.

Upvotes: 0

aphoria
aphoria

Reputation: 20179

Can you use ROBOCOPY?

ROBOCOPY C:\Program\Foo D:\ * /E /XD Test
  • /E copies subfolders and files
  • /XD excludes directories

Upvotes: 0

Related Questions