windows 批处理文件 将具有特定扩展名的文件从多个目录复制到一个目录中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1224880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 06:37:58  来源:igfitidea点击:

batch file Copy files with certain extensions from multiple directories into one directory

windowsbatch-filecommand-promptxcopy.doc

提问by Simeon Pilgrim

I'm a newbie, so bear with me...

我是新手,请多多包涵...

I am trying to copy all .docfiles that I have scattered throughout several subdirectories of one main directory into another directory using a batch file. I have managed to get a filelist.txtof all the files (there are hundreds) out of these directories that I want to copy using:

我正在尝试.doc使用批处理文件将分散在一个主目录的几个子目录中的所有文件复制到另一个目录中。我设法filelist.txt从这些目录中获取了所有文件(有数百个),我想使用以下方法复制:

"C:\Main directory\sub directory" dir /b /s *.doc > "C:\Main directory\sub directory\filelist.txt"

"C:\Main directory\sub directory" dir /b /s *.doc > "C:\Main directory\sub directory\filelist.txt"

What script would I use to xcopy those into one directory? Can I use some code that actually grabs those file names from filelist.txtand xcopies them?

我将使用什么脚本将它们复制到一个目录中?我可以使用一些实际从中filelist.txt获取这些文件名并对其进行复制的代码吗?

For reference, I looked at the question below because it looked like it was doing what I want to do, but it didn't work for me.

作为参考,我查看了下面的问题,因为它看起来像是在做我想做的事情,但对我不起作用。

Using xcopy to copy files from several directories to one directory

使用 xcopy 将文件从多个目录复制到一个目录

Also, I would really like to understand this concept, so please break down the code for me to tell me what each item does, or at least include a link that will explain it.

另外,我真的很想理解这个概念,所以请分解代码让我告诉我每个项目的作用,或者至少包含一个解释它的链接。

回答by Simeon Pilgrim

In a batch file solution

在批处理文件解决方案中

for /R c:\source %%f in (*.xml) do copy %%f x:\destination\

The code works as such;

代码是这样工作的;

for each file forin directory c:\sourceand subdirectories /Rthat match pattern (\*.xml)put the file name in variable %%f, then for each file docopy file copy %%fto destination x:\\destination\\

对于匹配模式的for目录c:\source和子目录/R中的每个文件,(\*.xml)将文件名放在变量中%%f,然后对于每个文件do将文件复制copy %%f到目标x:\\destination\\

Just tested it here on my Windows XP computer and it worked like a treat for me. But I typed it into command prompt so I used the single %fvariable name version, as described in the linked question above.

刚刚在我的 Windows XP 计算机上测试了它,它对我来说就像一种享受。但是我将它输入到命令提示符中,所以我使用了单个%f变量名称版本,如上面链接的问题中所述。

回答by userJT

Just use the XCOPY command with recursive option

只需使用带有递归选项的 XCOPY 命令

xcopy c:\*.doc k:\mybackup /sy

/s will make it "recursive"

/s 将使其“递归”

回答by user15071

Things like these are why I switched to Powershell. Try it out, it's fun:

诸如此类的事情就是我切换到 Powershell 的原因。试试看,很有趣:

Get-ChildItem -Recurse -Include *.doc | % {
    Copy-Item $_.FullName -destination x:\destination
}

回答by Jay

Brandon, short and sweet. Also flexible.

布兰登,短而甜美。还灵活。

set dSource=C:\Main directory\sub directory
set dTarget=D:\Documents
set fType=*.doc
for /f "delims=" %%f in ('dir /a-d /b /s "%dSource%\%fType%"') do (
    copy /V "%%f" "%dTarget%\" 2>nul
)

Hope this helps.

希望这可以帮助。

I would add some checks after the copy (using '||') but i'm not sure how "copy /v" reacts when it encounters an error.

我会在复制后添加一些检查(使用“||”),但我不确定“复制/v”在遇到错误时如何反应。

you may want to try this:

你可能想试试这个:

copy /V "%%f" "%dTarget%\" 2>nul|| echo En error occured copying "%%F".&& exit /b 1

As the copy line. let me know if you get something out of it (in no position to test a copy failure atm..)

作为复制线。如果您从中得到什么,请告诉我(无法测试复制失败 atm ..)

回答by ghostdog74

you can also use vbscript

你也可以使用 vbscript

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder = "c:\test"
strDestination = "c:\tmp\"
Set objFolder = objFS.GetFolder(strFolder)

Go(objFolder)

Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders       
        Go eFolder
    Next
    For Each strFile In objDIR.Files
        strFileName = strFile.Name
        strExtension = objFS.GetExtensionName(strFile)
        If strExtension = "doc" Then
            objFS.CopyFile strFile , strDestination & strFileName
        End If 
    Next    
  End If  
End Sub 

save as mycopy.vbs and on command line

另存为 mycopy.vbs 并在命令行上

c:\test> cscript /nologo mycopy.vbs