简体   繁体   English

批处理文件以递归方式从多个子目录复制和重命名文件

[英]Batch file to copy and rename files from multiple subdirectories recursively

I have a lot of files in subdirectories, which i want to have我在子目录中有很多文件,我想拥有

  • in one folder without subdirectories在一个没有子目录的文件夹中
  • renamed with the uppermost directory as prefix followed by its genuine file name.以最上面的目录作为前缀重命名,后跟其真正的文件名。

Eg:例如:

dir1\dir2\dir3\fileA.abc --> Output\dir1-fileA.abc dir1\dir2\dir3\fileA.abc --> 输出\dir1-fileA.abc

dir4\dir5\fileB.def --> Output\dir4-fileB.def dir4\dir5\fileB.def --> 输出\dir4-fileB.def

I tried to adapt the solution given in Batch file to copy and rename files from multiple directories for my purposes with the following code, but it is getting stuck in an endless loop as it seems.我尝试使用以下代码调整批处理文件中给出的解决方案,以便出于我的目的从多个目录复制和重命名文件,但它似乎陷入了无限循环。

Any ideas how to fix this?任何想法如何解决这一问题?

pushd "C:\SomeDirectory"
if not exist "Output\*" md Output
for /D %%D in (*) do (
    if /I not "%%D" == "Output" (
        for /R %%J in ("*.*") do (
            copy /B /Y "%%~fJ" "Output\%%D-%%~nxJ" >nul
        )
    )
)

pause

It looks as though your Output directory is actually "inside" your source directory.看起来您的 Output 目录实际上在您的源目录“内部”。 Then when you are copying/moving files to Output - you are actually just increasing the number of input files that need to be processed.然后,当您将文件复制/移动到 Output 时 - 您实际上只是增加了需要处理的输入文件的数量。 Classic endless loop:-)经典的无限循环:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM