【问题标题】:ROBOCOPY - Copy folders content to a single folder WITH file listROBOCOPY - 将文件夹内容复制到带有文件列表的单个文件夹
【发布时间】:2014-07-16 14:04:34
【问题描述】:

由于我无法添加评论,因此我提出了一个相关问题。

原帖found here 效果很好。

有没有办法将它与文件名列表一起使用?我已经看到可以将文件列表传递给 ROBOCOPY 命令的位置,但我无法让它工作。

退后一步,我有一系列文件夹,其中有一些特定文件我想复制到一个文件夹中。我有一个列出这些文件名称的文本文件。

我正在寻找一个批处理例程,它将在每个文件夹中查找文本文件中的每个文件,然后将文件复制到一个新文件夹中。

谢谢!

【问题讨论】:

  • 所有文件夹都在一个目录树中吗?
  • 是的,有一个父文件夹,所有子文件夹都在该文件夹中。我不相信有更多级别的文件夹意味着子文件夹没有子文件夹。

标签: batch-file recursion robocopy


【解决方案1】:

对此进行测试 - file.txt 在每一行都有一个文件名。

它不处理文件名冲突。

@echo off
cd /d "c:\base\folder"
for /f "usebackq delims=" %%a in ("file.txt") do (
    for /f "delims=" %%b in ('dir "%%a" /b /s /a-d ') do copy "%%b" "d:\target\folder"
)

【讨论】:

  • 那,通过代理你,太棒了!谢谢你这么快回复!这完美无缺。再次感谢!
【解决方案2】:

我最近不得不解决这个问题,我想从层次结构移动到单个文件夹的许多文件彼此具有相同的名称,并且我仍然希望在不被覆盖的情况下扁平化层次结构. 我所做的是编写一个移动文件的脚本,但用旧的层次结构路径重命名它在名称中 例如: 源文件:

C:\files\somefiles\file.txt

C:\files\otherfiles\file.txt

目标是 C:\newdir\ 文件被创建为

C:\newdir\somefiles-file.txt

C:\newdir\otherfiles-file.txt

这是代码,批处理文件 1 遍历文件,批处理文件 2 重命名并移动它们(如果您想保留源代码,也可以复制:

@echo off
for /r %%f in (*.*pr) do @renameandmovefilespart2.bat "%%f" "%%~ff" "%%~xf"

重命名和移动文件part2.bat

@echo off
Setlocal EnableDelayedExpansion
rem set the whole file path
set origWhole=%1
set origPathOnly=%2
set extension=%3
rem here you can set where the directory to hold the flattened hierarchy is
set destDir=c:\destinationDir\
rem  set the directory to do a string replace
rem make this the starting directory, that you dont want in the newly renamed files
set startingDir=C:\starting\directory\
set nothing=
set slash=\
rem here you can set what the character to represent the directory indicator \ in the new files 
set reaplcementDirectoryCharacter=--
set quote="
rem cut out the starting part of the directory
call set newname=%%origWhole:!startingDir!=!nothing!%%
rem replace slashes with new character
call set newname=%%newname:!slash!=!reaplcementDirectoryCharacter!%%
rem remove quotes
call set newname=%%newname:!quote!=!nothing!%%
rem @echo shortened: %newname%
rem @echo source path: %origPathOnly% newPath: %startingDir%
rem @echo extension: %extension%
rem rename the files
ren %origWhole% %newname%
rem prepare to move the file, clean up the source path
call set origPathOnly=%%origPathOnly:!quote!=!nothing!%%
move "%origPathOnly%%newname%" "%destDir%"

【讨论】:

    猜你喜欢
    • 2011-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多