batch-file compare similar filenames in a directory then copy the last modified file and save as to other directory

StackOverflow https://stackoverflow.com/questions/13254935

Domanda

I have a batch file that will check for updates within a directory then copy any new files or recently modified files to the other selected directory, however I'm unsure how to check the source directory for say two files that have just a revision number or letter difference: example.pdf and exampleA.pdf. I need to compare the files somehow by both string and date modified. So if the source directory has a new file that's been saved as exampleB.pdf, I need the batch to copy that file into the destination directory as example.pdf instead of the new filename. I want the copied file to have the core filename if you will, being just example.pdf

Any help would be much appreciated.

Thanks

    @Echo Off
:: variables
set drive=G:\Backup
set backupcmd=xcopy /s /c /d /e /h /i /r /y

set revchk=if 

Set _Delay=10
Set _Monitor=C:\Users\me\Desktop\Test Source Folder
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /C:"Version 5">Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop

echo ### Backing up...
%backupcmd% "C:\Users\me\Desktop\Test Source Folder" "C:\Users\me\Desktop\Test Destination Folder"


echo ### Checking for new file revisions...



Echo.Backup Complete!
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
È stato utile?

Soluzione

The Batch program below assume that newer files have revision numbers or letters in ascending alphabeticall order, so the last listed file is the newest one. This way, the program identify a set of files that begin with the same name, and copy the last one of the set with the name of the first one in the set.

@echo off
setlocal EnableDelayedExpansion
set baseName=
for %%a in (*.*) do (
   if not defined baseName (
      rem Is first name of first set
      set baseName=%%~Na
      set baseExt=%%~Xa
      set lastName=%%~Na
   ) else (
      rem Check if this name begin with same baseName
      set name=%%~Na
      for %%b in (!baseName!) do set name=!name:*%%b=!
      if "!name!" neq "%%~Na" (
         rem Yes: Is next name of same set
         set lastName=%%~Na
      ) else (
         rem No: Is first name of next set: copy previous set and pass to next one
         ECHO copy "!lastName!!baseExt!" "C:\dest\dir\!baseName!!baseExt!"
         set baseName=%%~Na
         set baseExt=%%~Xa
         set lastName=%%~Na
      )
   )      
)
rem Copy last set
ECHO copy "!lastName!!baseExt!" "C:\dest\dir\!baseName!!baseExt!"

Test the program and remove ECHO commands if it works as you want.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top