How do I copy files using Windows Batch?

2019-02-18 02:38发布

I have a directory with several subdirectories with files.
How can I copy all files in the subdirectories to a new location?

Edit: I do not want to copy the directories, just the files...

As this is still on XP, I chose the below solution:

 for /D %S IN ("src\*.*") DO  @COPY "%S\" "dest\"

Thanks!

6条回答
Viruses.
2楼-- · 2019-02-18 02:55

If I understood you correctly you have a big directory tree and you want all the files inside it to be in one directory. If that's correct, then I can do it in two lines:

dir /s /b "yourSourceDirectoryTreeHere" > filelist.txt
for /f %f in (filelist.txt) do @copy %f "yourDestinationDirHere"

In a batch file vs. the command line change %f to %%f

查看更多
倾城 Initia
3楼-- · 2019-02-18 02:58

The Xcopy command should help here.

XCOPY /E SrcDir\*.* DestDir\

Or if you don't want any of the files in SrcDir, just the sub directories, you can use XCOPY in conjunction with the FOR command:

FOR /D %s IN (SrcDir\*) DO @XCOPY /E %s DestDir\%~ns\
查看更多
倾城 Initia
4楼-- · 2019-02-18 03:02

robocopy "c:\source" "c:\destination" /E

查看更多
smile是对你的礼貌
5楼-- · 2019-02-18 03:03

Ok. With your edit that says you don't want the directory structure, i think you're going to want to use something like this:

for /F "usebackq" %s IN (`DIR /B /S /A-D SrcDir`) DO @(
    XCOPY %s DestDir\%~nxs
)
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-18 03:05
 for /D %S IN ("src\*.*") DO  @COPY "%S\" "dest\"
查看更多
【Aperson】
7楼-- · 2019-02-18 03:19

If you want to keep the same folder structure on the other end, sounds as simple as XCOPY

xcopy c:\old\*.* d:\new\ /s

Use /e instead of /s if you want empty directories copied too.

查看更多
登录 后发表回答