Exclude folders in batch-copy-script
Asked Answered
N

2

9

I am using a batch file on a USB stick to backup my pictures. I use the following command:

for /r C:\ %%x in (*.jpg *.png *.gif) do @copy /y %%x .

I want to exclude files in the mailfolder WINDOWS and PROGRAM FILES.

Does anyone have an idea how I can do this with a batch file?

Nikko answered 19/10, 2012 at 14:52 Comment(0)
I
20

Drop COPY and use ROBOCOPY which exists in Windows Vista+ & is downloadable for prior versions.

It supports /XD to exclude specific directories & /XF to exclude file masks at the command line.

E.g.

robocopy.exe c:\ c:\destination\ *.jpg *.png *.gif /xd "Program files" "windows" /S

(Note this will recreate the directory structure in c:\destination\, which thinking about it may not be what you want)

Ineludible answered 19/10, 2012 at 14:55 Comment(6)
Do you have an example fitting this issue. I tried it but only got error message because of some parameters it didn't accept (that was listet on Microsofts pages).Nikko
Can i set the standing folder as destination any easy way?Nikko
You mean the current dir? use a period .Ineludible
Ok, thanks :) It's say it copying now, but the files doesn't appera. Weird. Btw, can I use many sources, like C: and D:?Nikko
It copying to C:\windows\system32 instead of the standing dir with .Nikko
This saved me so much time. Worked perfectly for copying my work directory onto a backup drive and excluding all my node_modules folders within each directory which don't need copying! +1Dovecote
L
2

Turn copy into xcopy and then you can use it's /EXCLUDE switch

@xcopy %%x /y /EXCLUDE:\WINDOWS\

See xcopy /? for the details.

Lyndonlyndsay answered 19/10, 2012 at 15:0 Comment(5)
C:\Windows\system32>for /R C:\ %x in (*.jpg *.png *.gif) do @xcopy %x /y /EXCLUDE:\WINDOWS\ Ugyldig antall parametere. Ugyldig antall parametere. Ugyldig antall parametere. Ugyldig antall parametere. Ugyldig antall parametere. Ugyldig antall parametere. Ugyldig antall parametere. (illegal number of parameters)Nikko
Did you read xcopy /?? that will give you the switch details.Lyndonlyndsay
Ofcourse :) But I did it exactly as it was explained: for /R C:\ %x in (*.jpg *.png *.gif) do @xcopy %x /y /EXCLUDE:\WINDOWS\Nikko
Just checking :) In that case I'm not sure, I would prob go with Alex's answer.Lyndonlyndsay
Since I finally have enough rep to comment a year later...: @xcopy %%x /y /EXCLUDE:exclude.txt where exclude.txt has: WINDOWS\ to exclude the WINDOWS folder. The EXCLUDE switch requires ' a list of files containing strings'Alcibiades

© 2022 - 2024 — McMap. All rights reserved.