Windows batch file for copying and saving duplicates

I have many image folders, and I want to create a batch file that can view all these directories and their subdirectories, and copy each image to one new folder (all files in the same folder). It works for me using below:

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

however, I also want to store files with duplicates (for example, if folder 1 and folder2 have images named 001.jpg, I want both of them to be copied to a new folder). For me, it doesn't matter what the new file names are! Availability:

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

it would be great, but even just rename each file with an incremental account and as a result:

1.jpg
2.jpg
3.jpg
etc

will be fine too. I only need this using the standard .bat / .cmd file, although there is no external software.

Thank you for your help!

+3
3

. , . . \Src, , , test_folder. hardcode test_folder, -, evaulated DIR/S/B...

@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
+9

script - aflat.

script : SourcePath TargetPath.

SourcePath TargetPath, , .

, TargetPath , _n.

, / .

script , aflat. , ! . aflat .

::copyFlat sourcePath  TargetPath
@echo off
setlocal disableDelayedExpansion

:: Initialize and validate arguments
if "%~2" equ "" echo Error: Insufficient arguments>&2&exit /b 1
set "source=%~f1"
if not exist "%source%\" echo Error: Source folder "%source%" does not exist>&2&exit /b 1
set "target=%~f2"
if exist "%target%\" echo Error: Target folder "%target%" already exists>&2&exit /b 1

:: Do the work
md "%target%"
set /a n=0
for /r "%source%" %%F in (*) do if "%%~dpF" neq "%target%\" (
  if exist "%target%\%%~nxF" (
    set /a n+=1
    set "full=%%F"
    set "name=%%~nF"
    set "ext=%%~xF"
    setlocal enableDelayedExpansion
    copy "!full!" "!target!\!name!_!n!!ext!" >nul
    endlocal
  ) else copy "%%F" "%target%" >nul
)
+4

git bash (https://git-for-windows.imtqy.com/) :

mv --backup=numbered src\*.* destinationFolder

, filename.ext. ~ 1 ~, filename.ext. ~ 2 ~... , , , :

ls *~ | sed 's/\(.*\)\.\(.*\)\.~\(.*\)~/mv -i & \1.\3.\2/'

: mv filename.ext. ~ 1 ~ filename.1.ext

, , , , pipe to sh ( , , , , -i ):

  ls *~ | sed 's/\(.*\)\.\(.*\)\.~\(.*\)~/mv -i & \1.\3.\2/' | sh

(fooobar.com/questions/98377/...)

0

All Articles