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

发布于 2024-07-30 05:35:38 字数 618 浏览 5 评论 0原文

我是一名新手,所以请耐心等待...

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

"C:\Main directory\sub directory" 目录 /b /s *.doc > “C:\主目录\子目录\filelist.txt”

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

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

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

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

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

I am trying to copy all .doc files 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.txt of all the files (there are hundreds) out of these directories that I want to copy using:

"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.txt and xcopies them?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

大海や 2024-08-06 05:35:38

诸如此类的事情就是我转向 Powershell 的原因。 尝试一下,很有趣:

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

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

Get-ChildItem -Recurse -Include *.doc | % {
    Copy-Item $_.FullName -destination x:\destination
}
帅的被狗咬 2024-08-06 05:35:38

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

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

即可使其“递归”

Just use the XCOPY command with recursive option

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

/s will make it "recursive"

我很坚强 2024-08-06 05:35:38

在批处理文件解决方案中

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

代码的工作方式如下;

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

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

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 for in directory c:\source and subdirectories /R that match pattern (\*.xml) put the file name in variable %%f, then for each file do copy file copy %%f to destination 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 %f variable name version, as described in the linked question above.

总攻大人 2024-08-06 05:35:38

您还可以使用 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 

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

c:\test> cscript /nologo mycopy.vbs

you can also use 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

c:\test> cscript /nologo mycopy.vbs
写下不归期 2024-08-06 05:35:38

这可以使用以下命令来完成:

for /r "C:\source" %f in (*.doc) do @copy "%f" "C:\destination"

您还可以运行这些命令以使其简单

set source="C:\src"
set destination="C:\dest"
set extension=doc

for /r %source% %f in (*.%extension%) do @copy "%f" %destination%

如果您愿意使用文件类型父文件夹名称进行复制,请尝试使用此命令

xcopy "C:\src\*.doc" "C:\dest" /sy

This can be done using this command:

for /r "C:\source" %f in (*.doc) do @copy "%f" "C:\destination"

you could also run these to make it simple

set source="C:\src"
set destination="C:\dest"
set extension=doc

for /r %source% %f in (*.%extension%) do @copy "%f" %destination%

In case you are willing to copy with file types parent folder name try using this command

xcopy "C:\src\*.doc" "C:\dest" /sy
笑忘罢 2024-08-06 05:35:38

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

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
)

希望这可以帮助。

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

你可能想尝试这个:

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

作为复制行。 如果您从中得到了一些东西,请告诉我(无法测试复制失败的自动提款机..)

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.

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..)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文