3126
Finished Programs / Re: DONE: Sync folders by renaming files
« Last post by 4wd on June 02, 2013, 08:05 PM »As an aside, I think basing the rename off of file sizes is just asking for trouble since the chance of a collision does exist, however small. On any modern computer, generating any kind of checksum for such small files wouldn't be much of a price to pay to better guard against collisions.-skwire (June 02, 2013, 04:25 PM)
V2 with MD5 checksum, requires MS File Checksum Integrity Verifier in your path somewhere:
Code: Text [Select]
- @echo off
- rem RenSS.cmd
- rem Rename files in dest with same size as files in source
- rem
- rem RenSS.cmd <srce> <dest>
- setlocal EnableExtensions
- setlocal EnableDelayedExpansion
- for /r %1 %%a in (*.jpg) do (call :CheckSize "%2" %%~za "%%~fa")
- goto End
- :CheckSize
- for /r %1 %%b in (*.jpg) do (if %2 equ %%~zb call :CheckMD5 "%~3" "%%~b")
- goto :EOF
- :CheckMD5
- for /f "skip=3 delims= " %%i in ('fciv -md5 %1') do set md5_1=%%i
- for /f "skip=3 delims= " %%i in ('fciv -md5 %2') do set md5_2=%%i
- if "!md5_1!"=="!md5_2!" (ren "%~2" "%~nx1")
- set md5_1=
- set md5_2=
- goto :EOF
- :End
- endlocal
Still has built in overwrite safeguard, (ie. you can't rename to a name that already exists).
Had to find it again, the MD5 check routine came from here courtesy of James L.
V3 adds folder requesters so you can now just double-click on the cmd file

Code: Text [Select]
- @if (@CodeSection == @Batch) @then
- @echo off
- setlocal EnableExtensions
- setlocal EnableDelayedExpansion
- for /F "delims=" %%S in ('CScript //nologo //E:JScript "%~F0"') do (
- if %%S=="" exit
- set srce=%%S
- )
- for /F "delims=" %%D in ('CScript //nologo //E:JScript "%~F0"') do (
- if %%D=="" exit
- set dest=%%D
- )
- echo Source folder: "%srce%"
- echo Destination folder: "%dest%"
- for /r "%srce%" %%a in (*.jpg) do (call :CheckSize "%dest%" %%~za "%%~fa")
- :End
- set srce=
- set dest=
- pause
- exit
- :CheckSize
- for /r %1 %%b in (*.jpg) do (if %2 equ %%~zb call :CheckMD5 "%~3" "%%~b")
- goto :EOF
- :CheckMD5
- for /f "skip=3 delims= " %%i in ('fciv -md5 %1') do set md5_1=%%i
- for /f "skip=3 delims= " %%i in ('fciv -md5 %2') do set md5_2=%%i
- if "!md5_1!"=="!md5_2!" (ren "%~2" "%~nx1")
- set md5_1=
- set md5_2=
- goto :EOF
- endlocal
- End of Batch section
- @end
- // JScript section
- // Creates a dialog box that enables the user to select a folder and display it.
- var title = "Select a folder", rootFolder = 0x11;
- var shl = new ActiveXObject("Shell.Application");
- var folder = shl.BrowseForFolder(0, title, 0, rootFolder);
- WScript.Stdout.WriteLine(folder ? folder.self.path : "");
That particular bit of sorcery came from Antonio at DosTips.
I'm starting to wonder why I bother with AutoIt

EDIT: Renamed to RenSSMD5.cmd to differentiate it from V4.
NOTE: Because of the EnableDelayedExpansion setting in V4, any filename that has an exclamation mark (!) in it will NOT be renamed. Use V9 below if you want hash comparison and you use ! in filenames.

Recent Posts







