简体   繁体   中英

Copying a file in specific folders using Batch file

I have two PDFs - .COMInterop and C# Notes - Notes 1 to 10.pdf and .COMInterop and C# Notes - Notes 11 to 20.pdf kept in the directory D:\\Dropbox\\Sample C# Notes

The folder Sample C# Notes also has some subfolders like 0001 , 0002 , 0003 and so on till 0300 .

I am writing the following command (given by SO user lotpings) in a batch file to copy the pdf from Sample C# Notes to all the subfolders inside it (0001, 0002, 0003...)

for /D %%x in ("D:\Dropbox\Sample C# Notes\*") DO (
  COPY "D:\Dropbox\Sample C# Notes\.COMInterop and C# Notes - Notes 1 to 10.pdf" "%%x\"
)

How do I modify this code so that I can copy :

.COMInterop and C# Notes - Notes 1 to 10.pdf into folders 0001 to 0100

and

.COMInterop and C# Notes - Notes 11 to 20.pdf into folders 0101 to 0300

Try this please:

@echo off
pushd "D:\Dropbox\Sample C# Notes\"
for /f %%x in ('dir /b /ad 0*') do (
    if %%x leq 0100 (
        COPY ".COMInterop and C# Notes - Notes 1 to 10.pdf" "%%x\"
    ) else (
        COPY ".COMInterop and C# Notes - Notes 11 to 20.pdf" "%%x\"
    )
)
popd
@echo on

To make life easier, I didn't use full dir path, instead I changed working path first, and copy files.
Also since you said there are only 0001 to 0030 these subfolders, so I didn't check 0101 to 0300 , just put them in the else block.
You can improve it based on these.

for /f used another command dir 's output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM