用于复制和保留重复项的 Windows 批处理文件 [英] Windows batch file to copy and keep duplicates
问题描述
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"
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
would be fine too. I need it just using a standard .bat/.cmd file though, no external software.
Thanks for your help!
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屋!