1.

Solve : BAT file to copy last modified file?

Answer»

Below is a script I use to find the last modified file to copy it and put into another location.

Code: [Select]SETLOCAL ENABLEDELAYEDEXPANSION

SET FILEPATH="LOCATION"
REM Arbitrary yyyymmdd starting point, nothing will be older than it
SET NewestDate=20130101

ECHO Recursively searching %FFPath%
echo.

FOR /F "delims=" %%I in ('DIR %ETPath%\*.xlsx /a:-d /s /b') DO (
SET FullDate=%%~tI

REM Set CurrDate to yyyymmdd format. Note: Will fail if regional settings changed.
SET CurrDate=!FullDate:~6,4!!FullDate:~0,2!!FullDate:~3,2!

If !CurrDate! gtr !NewestDate! (
SET NewestDate=!CurrDate!
SET NewestFile=%%~fI
)
)
ECHO Copying %NewestFile% to LOCATION

ECHO.
XCOPY /Y /R "%NewestFile%" "[i]LOCATION
[/i]"
ECHO.

This code works wonderfully, but if a user has the file open it can't seem to find the file. I don't know if it is because it CREATES a hidden file and locks it, or what. Any assistance in being able to IGNORE the hidden file or provide a fix for this would be greatly appreciated.I was able to figure it out using /a:-h
I will adjust the code in case somebody else is having a similar issue or LOOKING to do the same thing. Once again, this will copy a file using the last modified date attribute.

Code: [Select]SETLOCAL ENABLEDELAYEDEXPANSION

SET FILEPATH="LOCATION"
REM Arbitrary yyyymmdd starting point, nothing will be older than it
SET NewestDate=20130101

ECHO Recursively searching %FFPath%
echo.

FOR /F "delims=" %%I in ('DIR %ETPath%\*.xlsx /a:-d /a:-h /s /b') DO (
SET FullDate=%%~tI

REM Set CurrDate to yyyymmdd format. Note: Will fail if regional settings changed.
SET CurrDate=!FullDate:~6,4!!FullDate:~0,2!!FullDate:~3,2!

If !CurrDate! gtr !NewestDate! (
SET NewestDate=!CurrDate!
SET NewestFile=%%~fI
)
)
ECHO Copying %NewestFile% to LOCATION

ECHO.
XCOPY /Y /R "%NewestFile%" "LOCATION"
ECHO. Quote

I will adjust the code in case somebody else is having a similar issue or looking to do the same thing.

Thanks for sharing that ...

I have generally just used xcopy /s/d/y to make a copy of contents to an alt location over network to another system and the newest, last modified file is transferred to the alt location when date/time stamp changes when its last saved/modified, and all files that haven't changed and are a match between source and destination are skipped from transfer. Neat that this just transfers that 1 file.

*I may have a use for your batch to copy only the newest last modified file from one location to another, where this game called Poker Night 2 hosted by Steam, created by Tell Tale Games, has a corruption flaw in its code that many people are experiencing with no help from Steam or Tell Tale Games Co. And currently the only way to fix the problem is to uninstall and reinstall the game each time this happens. If I can detect what file was last written to before the crash occurred, I can then single out the one file that is getting corrupt and may find the one file that is causing the crash.

I was going to perform a file compare between the normal install and known good replicated location to find what file was last altered between A and B and single out that way, but your batch LOOKS like a better method.

Knowing which file is the cause... I can then make a bugfix for people to use since Steam and Tell Tale Games are ignoring requests for help on this flawed game. Some people are so mad about this when searching for a solution to this that they feel that legal action should be taken, over either a game that is poorly programmed and TROUBLED, or Steams client integration flaw that is causing the game to corrupt and fail.


Discussion

No Comment Found