【问题标题】:Batch: Copy files, with same partial name, from subfolders to another one批处理:将具有相同部分名称的文件从子文件夹复制到另一个文件夹
【发布时间】:2016-11-08 15:44:50
【问题描述】:

我想从已知数量的子文件夹中复制具有部分共同姓名的文件。我试过这段代码:

@Echo Off
rem Check if the aFolder exists and delete it if it exists

IF EXIST "%~dp0\aFolder" (
     rd /s /q "%~dp0\aFolder"
)

rem Create a new aFolder
mkdir "%~dp0\aFolder"

rem Copy the files from the subfolders inside of bFolder to aFolder    
For /F "tokens=1" %%A in (%~dp0\subFoldersList.txt) do (
    For /F "tokens=1" %%B in (%~dp0\formatList.txt) do (
        pushd "%~dp0\bFolder\%%A"
        For %%C in (%%AcommonPart.%%B) do xcopy %%C "%~dp0\aFolder"
    )
)

代码使用两个包含不同数据的 txt 文件。 在 subFoldersList.txt 中:

subFolder1
subFolder2
subFolder3

在formatList.txt中:

xls
xlsx
xlsm

检查循环不运行的代码,我不知道为什么。

编辑:

我使用的文件夹骨架是这样的:

rootFolder
|->aFolder
||-->subFolder1
|||--->subFolder1commomPart.xlsx (for example)
|||--->subFolder1other1Part.xlsm (for example)
|||--->subFolder1other2Part.xls (for example)
||-->subFolder2
|||--->subFolder1commomPart.xlsm (for example)
|||--->subFolder1other1Part.xlsx (for example)
|||--->subFolder1other2Part.xls (for example)
||-->subFolder3
|||--->subFolder1commomPart.xls (for example)
|||--->subFolder1other1Part.xlsm (for example)
|||--->subFolder1other2Part.xlsx (for example)
|->bFolder
|->subFoldersList.txt
|->formatList.txt
|->Other Stuff

我想得到这个结果:

rootFolder
|->aFolder
||-->subFolder1
|||--->subFolder1commomPart.xlsx (for example)
|||--->subFolder1other1Part.xlsm (for example)
|||--->subFolder1other2Part.xls (for example)
||-->subFolder2
|||--->subFolder1commomPart.xlsm (for example)
|||--->subFolder1other1Part.xlsx (for example)
|||--->subFolder1other2Part.xls (for example)
||-->subFolder3
|||--->subFolder1commomPart.xls (for example)
|||--->subFolder1other1Part.xlsm (for example)
|||--->subFolder1other2Part.xlsx (for example)
|->bFolder
||--->subFolder1commomPart.xlsx (for example)
||--->subFolder1commomPart.xlsm (for example)
||--->subFolder1commomPart.xls (for example)
|->subFoldersList.txt
|->formatList.txt
|->Other Stuff

【问题讨论】:

  • %~dp0 已经有一个尾随反斜杠,删除你在它后面添加的那些!
  • 我觉得For %%C in (%%AcommonPart.%%B) do xcopy %%C "%~dp0\aFolder"应该改成For %%C in ("commonPart.%%B") do xcopy "%%~C" "%~dp0\aFolder\",因为pushd已经改成子文件夹%%A了;还要注意改进的引用,%%~C 中的 ~ 和目标处的尾随反斜杠。您还应该将tokens=1 更改为delims=。请向我们展示一些文件名示例...
  • 您也必须发出POPD。
  • @Compo 谢谢。我之前已经使用了反斜杠,并且代码运行良好。
  • @Tilan04,这只是建议,尝试添加一行ECHO %~dp0,您会发现虽然您的方式可能在错误之前对您有用。我在下面的回答,它进行了其他更改,也显示了我的建议。

标签: batch-file for-loop subdirectory xcopy


【解决方案1】:

根据您迄今为止提供的内容,也许这就是您想要的:

@ECHO OFF

REM Make sure script directory is current
IF /I NOT "%CD%\"=="%~dp0" PUSHD "%~dp0" 2>NUL||EXIT/B

REM Make sure source files exist
FOR %%A IN ("subFoldersList.txt" "formatList.txt") DO IF NOT EXIST %%A EXIT/B

REM Delete aFolder if it exists
IF EXIST "aFolder\" RD/S/Q "aFolder"

REM Create aFolder
MD "aFolder"

REM Copy the files from the subfolders inside of bFolder to aFolder    
FOR /F "USEBACKQ DELIMS=" %%A IN ("subFoldersList.txt") DO (
    IF EXIST "bFolder\%%A\" (PUSHD "bFolder\%%A"
        FOR /F "USEBACKQ DELIMS=" %%B IN ("%~dp0formatList.txt") DO (
            IF EXIST "commonPart.%%B" (
                IF NOT EXIST "%~dp0aFolder\commonPart.%%B" (
                    COPY "commonPart.%%B" "%~dp0aFolder")))
        POPD))

【讨论】:

  • 代码不工作。我尝试在输入命令后暂停检查,但没有运行第一个 for 循环。
  • 为了调试或提供有关某人代码的反馈,需要做一些简单的事情,“代码不工作”对于我来说并不足以提供帮助。如果它没有进入第一个循环,那么可能至少有一个 txt 文件不存在于脚本旁边。
  • 是的……就是这样。我重命名了文件名,但缺少一个字母。大声笑感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 2014-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-19
  • 1970-01-01
相关资源
最近更新 更多