Compare folder contents and rename files
Solved
nicolas.74
-
nicolas.74 -
nicolas.74 -
Hello,
I’m looking to create a Batch script to compare the contents of two folders. If there are files that exist in both folders, delete them in the first folder. And if two files don’t match but have the same names, then rename the file in folder 2 and append the date.
But I can’t find anything at all, could someone help me?
Thank you,
Nicolas
I’m looking to create a Batch script to compare the contents of two folders. If there are files that exist in both folders, delete them in the first folder. And if two files don’t match but have the same names, then rename the file in folder 2 and append the date.
But I can’t find anything at all, could someone help me?
Thank you,
Nicolas
6 answers
-
Hi
@echo off
set dossier1=%USERPROFILE%\Tests1
set dossier2=%USERPROFILE%\Tests2
if not exist "%dossier1%" goto :eof
if not exist "%dossier2%" goto :eof
set liste1=%USERPROFILE%\efface.txt
set liste2=%USERPROFILE%\renomme.txt
if exist "%liste1%" (del "%liste1%")
if exist "%liste2%" (del "%liste2%")
for /f "tokens=*" %%A in ('dir /b /a-d "%dossier1%\*.*"') do (
for /f "tokens=*" %%B in ('dir /b /a-d "%dossier2%\*.*"') do (
if /i "%%~nxA"=="%%~nxB" (echo N | comp "%dossier1%\%%~nxA" "%dossier2%\%%~nxB" | findstr /C:"OK" && echo %%~nxA >> %liste1%)
if /i "%%~nxA"=="%%~nxB" (echo N | comp "%dossier1%\%%~nxA" "%dossier2%\%%~nxB" | findstr /C:"OK" || echo %%~nxB >> %liste2%)
)
)
cls
if not exist "%USERPROFILE%\efface.txt" goto suite
cd %dossier1%
for /f "tokens=*" %%C in (%liste1%) do (del "%%C" && echo Suppression de : %dossier1%\%%C)
:suite
if not exist "%USERPROFILE%\renomme.txt" goto fin
cd %dossier2% & echo.
for /f "tokens=*" %%D in (%liste2%) do (ren "%%D" "%%~nD_%date:~6,4%%date:~3,2%%date:~0,2%%%~xD" && echo Renommage de : %dossier2%\%%D)
:fin
echo. & pause
exit-
Thank you very much! I tweaked it a bit to fit my situation, and it looks like it’s working, you saved my life! Here is my final code:
@echo off if exist C:\Users\thibaxav\Desktop\script\temp\ rmdir /s /q C:\Users\thibaxav\Desktop\script\temp\ MD C:\Users\thibaxav\Desktop\script\temp\ For /f "tokens=*" %%a In ('dir /s /b /a-d "C:\Users\thibaxav\Desktop\script\source\*_IT.docx"') Do (Xcopy "%%a" "C:\Users\thibaxav\Desktop\script\temp") set dossier1=C:\Users\thibaxav\Desktop\script\temp set dossier2=C:\Users\thibaxav\Desktop\script\dossierfinalavectouslesfichiers set liste1=%USERPROFILE%\efface.txt set liste2=%USERPROFILE%\renomme.txt if exist "%liste1%" (del "%liste1%") if exist "%liste2%" (del "%liste2%") for /f "tokens=*" %%A in ('dir /b /a-d "%dossier1%\*.*"') do ( for /f "tokens=*" %%B in ('dir /b /a-d "%dossier2%\*.*"') do ( if /i "%%~nxA"=="%%~nxB" (echo N | comp "%dossier1%\%%~nxA" "%dossier2%\%%~nxB" | findstr /C:"OK" && echo %%~nxA >>%liste1%) if /i "%%~nxA"=="%%~nxB" (echo N | comp "%dossier1%\%%~nxA" "%dossier2%\%%~nxB" | findstr /C:"OK" || echo %%~nxB >>%liste2%) ) ) cls if not exist "%USERPROFILE%\efface.txt" goto suite cd %dossier2% for /f "tokens=*" %%C in (%liste1%) do (del "%%C" && echo Suppression de : %%C dans %dossier2%) :suite if not exist "%USERPROFILE%\renomme.txt" goto fin cd %dossier1% & echo. for /f "tokens=*" %%D in (%liste2%) do (ren "%%D" "%%~nD_%date:~6,4%.%date:~3,2%.%date:~0,2%%%~xD" && echo Renommage de : %%D dans %dossier1%) :fin For /f "tokens=*" %%a In ('dir /s /b /a-d "C:\Users\thibaxav\Desktop\script\temp\*.docx"') Do (Xcopy "%%a" "C:\Users\thibaxav\Desktop\script\dossierfinalavectouslesfichiers") cd C:\Users\thibaxav\Desktop\script rmdir /s /q C:\Users\thibaxav\Desktop\script\temp\ del %USERPROFILE%\renomme.txt del %USERPROFILE%\efface.txt exit
-
-
Here is the translation: Here is the script with the continuation:
@echo off
set folder1=%USERPROFILE%\Desktop\script\temp
set folder2=%USERPROFILE%\Desktop\script\dossierfinal
set folder3=%USERPROFILE%\Desktop\script\Old
if exist "%folder1%" rmdir /S /Q "%folder1%"
if not exist "%folder1%" mkdir "%folder1%"
if not exist "%folder2%" mkdir "%folder2%"
if not exist "%folder3%" mkdir "%folder3%"
For /f "tokens=*" %%a In ('dir /s /b /a-d "%USERPROFILE%\Desktop\script\source\*_IT.docx"') Do (Xcopy "%%a" "%folder1%" )
set list1=%USERPROFILE%\efface.txt
set list2=%USERPROFILE%\renomme.txt
if exist "%list1%" (del /F "%list1%")
if exist "%list2%" (del /F "%list2%")
for /f "tokens=*" %%A in ('dir /b /a-d "%folder1%\*.*"') do (
for /f "tokens=*" %%B in ('dir /b /a-d "%folder2%\*.*"') do (
for /f "tokens=1,2,3,* delims=/ " %%E in ('dir "%folder2%\%%~nxB" ^| findstr /V "volume pertoire octets"') do (
if not exist "%folder1%\%%~nxB" (copy /V /Y "%folder2%\%%~nxB" "%folder3%\%%~nB_%%G-%%F-%%E%%~xB" > nul && echo Copie de : %folder2%\%%~nxB)
)
if /i "%%~nxA"=="%%~nxB" (echo. & echo N | comp "%folder1%\%%~nxA" "%folder2%\%%~nxB" | findstr /C:"OK" && echo %%~nxA >> %list1%)
if /i "%%~nxA"=="%%~nxB" (echo. & echo N | comp "%folder1%\%%~nxA" "%folder2%\%%~nxB" | findstr /C:"OK" || echo %%~nxB >> %list2%)
)
)
cls
if not exist "%list1%" goto suite
cd %folder1%
for /f "tokens=*" %%C in (%list1%) do (del /F "%%C" && echo Suppression de : %folder1%\%%C)
:suite
if not exist "%list2%" goto fin
cd %folder2% & echo.
for /f "tokens=*" %%D in (%list2%) do (ren "%%D" "%%~nD_%date:~6,4%-%date:~3,2%-%date:~0,2%%%~xD" && echo Renommage de : %folder2%\%%D)
:fin
For /f "tokens=*" %%a In ('dir /s /b /a-d "%folder1%\*.docx"') Do (Xcopy "%%a" "%folder2%")
echo. & pause
del /F "%list1%"
del /F "%list2%"
exit
J'ai également intégré tes modifications, j'espère que ça te convient. -
-
Well, it’s not finished yet, I need help for the next step: when a file no longer exists in the source but exists in the destination folder, the file should be copied into an "old" folder with, if possible, the name of the last modification.
I’m trying things but I can’t get something that works, I feel like I’m circling around the real goal.
Could someone help?
Nicolas -
Just a small detail to fix in the new step but it’s not a problem.
What is the purpose of your cd at the end?-
Weird and more and more strange, with your last script, if there is no file in the destination (%dossier2%), no copy is done, but if there is just a single file that equals the temp folder, the copy does work
But yet it works with the last version I posted.
But besides this problem the copy into old still does not happen
More and more strange this script, I should try on another PC -
Is it possible to do it in two separate commands? Because I tried by modifying the line but it doesn’t work
if not exist "%dossier1%\%%~nxB" (copy /V /Y "%dossier2%\%%~nxB" "%dossier3%\%%~nxB" && rename %dossier3%\%%~nxB %%~nB_%%G-%%F-%%E%%~xB )
And I hadn't seen the question, the folder didn't delete without changing directory, and while searching the internet I saw someone say that you needed to put a cd first, I did that and since then it works -
with your latest script, if there is no file in the destination (%dossier2%) no copy is performed
==>> this is normal behavior since there is nothing to compare
Yet this is exactly what is requested:
1) if 2 files have the same name and the same size: delete the one from folder1
2) if 2 files have the same name but not the same size: rename the one from folder2
3) if the file is present in folder2 but not in folder1: copy it into folder3 -
-
-
-
Hello,
I’m coming back to you one last time, I like to understand what I’m doing, could someone explain to me what this command does?for /f "tokens=1,2,3,* delims=/ " %%E in ('dir "%dossier2%\%%~nxB" ^| findstr /V "volume pertoire octets"') do (
Because I’ve tried dissecting it from every angle and I still can’t understand
Thanks in advance,
Nicolas-
Hi, this command is intended to retrieve the modification date of a file. The dir command lists the file as well as its modification date, then findstr is used to remove the superfluous lines generated by the dir command in order to keep only the line that contains the date, then the for loop retrieves and splits the date into 3 parts (day, month, year).
-
-