So some background information of what I'm trying to accomplish. I have a log file in a location that I am copying to another location and renaming with a timestamp in the name, but leaving the attributes of the file the same. I also have it to where depending on a variable, it may make this file hidden. This all works as intended, and is triggered through a vbscript. The thing I am now trying to accomplish is before copying this file, I want to look at it's last modified date. I then want to compare that to each of the files in this other storage location, and if this matches then the file in the storage folder will be moved to a subfolder to store duplicate files.
@echo off
::////CONFIGURATION////
:: Set source folder and target folder in quotations
Set source="C:\Users\cafogle\Desktop\TEST\Source"
Set target="C:\Users\cafogle\Desktop\TEST\Destination"
:: Set folder name of where to keep copied files with duplicate last modified dates, without quotations
Set dumpfolder=filedump
:: The file name (including extension) without quotations
Set file=Log.txt
:: The file extension without quotations
Set filetype=.txt
:: If you want the file copied to be hidden, set this variable to '+h' If not, change it to '-h'
Set hidden=+h
::////SCRIPT////
:: Move duplicate files based on last modified date
Set reffilename=%file:~0,1%*%filetype%
for /f "delims=" %%i in ('"forfiles /P %source%\ /M %file% /C "cmd /c echo @ftime" "') do set reffiletime=%%i
for /f "delims=" %%k in ('"forfiles /P %target%\ /M %reffilename% /C "cmd /c echo @ftime" "') do (
(set reftimetemp=%%k)
for /f "delims=" %%j in ('"forfiles /P %target%\ /M %reffilename% /C "cmd /c echo @file" "') do (
(set reffiletemp=%%j)
if (%reftimetemp%==%reffiletime%) (robocopy %target% %target\%dumpfolder% %reffiletemp% /mov /a+:r)
)
)
pause
:: Running robocopy
Robocopy %source% %target% %file% /a+:r
taskkill /F /IM robocopy.exe
:: Retrieve local date and time from PC and save it to MyDate variable
for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x
set timestamp=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%_%MyDate:~8,2%_%MyDate:~10,2%_%MyDate:~12,2%
:: Set variable for new file name
Set newfilename=%file%_%timestamp%%filetype%
:: Directing the rename function to destination folder and file name, this will then rename the file with current date and time.
ren %target%\%file% "%newfilename%"
:: Determine if file should be made hidden and hide if necessary
attrib %target%\%newfilename% %hidden%
This is the entire batch file, but the only part that I am having issues with is:
:: Move duplicate files based on last modified date
Set reffilename=%file:~0,1%*%filetype%
for /f "delims=" %%i in ('"forfiles /P %source%\ /M %file% /C "cmd /c echo @ftime" "') do set reffiletime=%%i
for /f "delims=" %%k in ('"forfiles /P %target%\ /M %reffilename% /C "cmd /c echo @ftime" "') do (
(set reftimetemp=%%k)
for /f "delims=" %%j in ('"forfiles /P %target%\ /M %reffilename% /C "cmd /c echo @file" "') do (
(set reffiletemp=%%j)
if (%reftimetemp%==%reffiletime%) (robocopy %target% %target\%dumpfolder% %reffiletemp% /mov /a+:r)
)
)
I've tried just about everything I can find, but the main issue is finding a way to take the date from the source file, and then comparing it to each individual file and moving them individually based on the outcome.
Any assistance would be greatly appreciated, as I have exhausted every option I can find.
Thanks!