Compare folder contents and rename files

Solved
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

6 answers

  1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    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
    1
    1. nicolas.74
       
      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
      0
  2. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    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.
    1
    1. nicolas.74
       
      I don't think it's working, or I'm doing something wrong in my folders, but I get the error: "The syntax of the file name, directory name, or volume is incorrect."
      0
    2. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > nicolas.74
       
      You may have copied-pasted badly, or you’ve removed or added a space somewhere, or you forgot a ".

      No time this morning, I’ll re-check the script this afternoon.
      0
    3. nicolas.74
       
      No problem, you’re already really nice to help me!
      Actually, the issue isn’t the comparisons but the new step; I think there’s a missing line to rename the files, so it can’t copy them.
      0
    4. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > nicolas.74
       
      In the new step, the copy command renames the files as it copies them, I’m sure of this part I tested and re-tested yesterday. The only things I haven't tested are your modifications, the two FOR loops, which I integrated at the beginning and the end of the script, otherwise the rest is fine.
      0
    5. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
       
      Verify that you left a space after the slash on the right in this command:

      for /f "tokens=1,2,3,* delims=/ " %%E in
      0
  3. nicolas.74
     
    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
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      What do you call the last modification name?

      Give an example.
      0
    2. nicolas.74
       
      I expressed myself poorly, sorry, I meant to say add the latest modification date to the name.
      0
    3. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      Is this step to be done before the rest of the script?

      Because otherwise the files renamed with today's date will end up with a second date in addition to the first.
      0
    4. nicolas.74
       
      Sorry for the late reply, no it won’t put the name twice, because the files that are renamed with the current date are in both directories, whereas now we need to rename only the files that are in directory 2, so no risk of double renaming, but the order isn’t very important.
      0
    5. nicolas.74
       
      I correct what I said, order really matters, because if we copy this "old" files at the end, the files that were renamed earlier (with today’s date) will also be copied since they do not exist with that name in the temp folder.
      0
  4. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
     
    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?
    0
    1. nicolas.74
       
      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
      0
    2. nicolas.74
       
      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
      0
    3. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      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
      0
    4. nicolas.74 > barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention  
       
      Oh yes, I understand, I didn't think it worked like that
      0
    5. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930 > nicolas.74
       
      At first I had done it in 2 commands like you above, but it was impossible to get working, then I understood that I could copy and rename in a single command and it worked.
      0
  5. nicolas.74
     
    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
    0
    1. barnabe0057 Posted messages 14329 Registration date   Status Contributor Last intervention   4 930
       
      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).
      0
    2. nicolas.74
       
      Okay great, thanks! I was trying to break the commands down one by one, but you had to twist them a bit to understand. Thanks a lot! If a mod passes by here, can they mark the topic as resolved? I changed computers, so I no longer have access to the button to mark it as resolved.
      0