简体   繁体   中英

batch file to get file from last modified folder

I have a batch file that compares two files and uses xcopy to copy the file if the time stamp is different. So it goes something like this:

set sourcefiletime=
for %%X in (%Source%) do set sourcefiletime=%%~tX
set targetfiletime=
for %%X in (%Local%) do set targetfiletime=%%~tX
if "%sourcefiletime%" == "%targetfiletime%" goto noUpdate
xcopy...

The trouble I have now is that the folder structure has changed from always being a constant folder name to having different names based on date and time. And there may be multiple folders.

eg

"Build_20160411_105904"
"Build_20160410_155605"
"Build_20160410_021104"
...

Is there any way I can get my batch file to check the "last modified" folder and grab the file from there? If it helps, the file I need will always have the same name, so can I check the parent folder's directories for the last modified file instead?

Apologies if something similar has been asked before.

Not tested

@echo off
set "build_dir=c:\builds"
set "source_filename=build.result"

for /f "tokens=* delims=" %%# in ('dir /b /a:d /o:-d /t:w "%build_dir%"') do (
  set "last_build_dir=%%~f#"
  goto :break
)
:break
set "source_file=%last_build_dir%\%source_filename%"

this will get the file with name %source_filename% from the last modified folder in %build_dir% . And then you can reuse your code.

If you want to find the last modified folder name within C:\apps\test directory then you can put below code snippet in a batch script and execute it which will print the name of the last modified folder name.

@echo OFF

for /f "delims=" %%i in ('dir "C:\apps\test" /b /ad-h /t:c /o-d') do (
    set latestModifiedFolderName=%%i
    goto :break
)

:break
echo Latest modified folder within C:\apps\test directory is %latestModifiedFolderName%

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM