Windows 批处理文件复制并保留重复项 [英] Windows batch file to copy and keep duplicates

查看:18
本文介绍了Windows 批处理文件复制并保留重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有许多图像文件夹,我想创建一个批处理文件,该文件可以查看所有这些目录及其子目录,并将每个图像复制到一个新文件夹(同一文件夹中的所有文件).我使用以下方法进行这项工作:

I have many folders of images, and I want to create a batch file that can look through all these directories and their subdirectories, and copy every image to a single new folder (all files in the same folder). I have this working using the below:

md "My new folder"
for /D %i in (*) do copy "%i*" ".My New Folder"

但是,我还想保留重复的文件(例如,如果文件夹 1 和文件夹 2 都有名为 001.jpg 的图像,我希望将它们都复制到新文件夹中).新文件名是什么对我来说并不重要!有:

however, I also want to keep files with duplicates (for example if folder1 and folder2 both have images called 001.jpg, i want both copied to the new folder). It doesn't matter to me what the new filenames are! Having:

001.jpg
001(1).jpg
001(2).jpg

会很棒,但即使只是用增量计数重命名每个文件并以:

would be great, but even just renaming every single file with an incremental count and ending up with:

1.jpg
2.jpg
3.jpg
etc

也可以.我只需要使用标准的 .bat/.cmd 文件,不需要外部软件.

would be fine too. I need it just using a standard .bat/.cmd file though, no external software.

感谢您的帮助!

推荐答案

这应该适合你.它在扩展名后附加一个数字,但您可以轻松地将其移动到任何地方.我从 .src 目录复制了文件,因为如果您的源与批处理文件处于同一级别,则批处理文件也会尝试评估 test_folder.最好的选择是对 test_folder 进行硬编码,这样它就不会被 DIR/S/B... 命令评估

This should work for you. It appends a number after the extension, but you could easily move that anywhere. I copied files from the .src dir, since if you have the sources at the same level as the batch file, the batch file tries to evaluate test_folder too. The best choice would be to hardcode test_folder so it is somewhere that won't be evaulated by the DIR /S /B... command

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set TESTFOLDER=test_folder
md "%TESTFOLDER%"

set /a counter=0
FOR /F "tokens=*" %%i IN ('DIR /S /B /A-D .src*') DO FOR /F "tokens=*" %%j IN ('DIR /B "%%i"') DO IF EXIST ".\%TESTFOLDER%\%%j" (
        set /a counter=!counter!+1
        echo folder: %TESTFOLDER%
        copy "%%i" ".\%TESTFOLDER%\%%j_!counter!"
    ) ELSE copy "%%i" ".\%TESTFOLDER%\%%j"
:eof

这篇关于Windows 批处理文件复制并保留重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆