简体   繁体   English

批处理文件以从上次修改的文件夹中获取文件

[英]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.我有一个比较两个文件的批处理文件,如果时间戳不同,则使用 xcopy 复制文件。 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% .这将从%build_dir%中最后修改的文件夹中获取名称为%source_filename%的文件。 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.如果你想在 C:\apps\test 目录中找到最后修改的文件夹名称,那么你可以将下面的代码片段放在批处理脚本中并执行它,这将打印最后修改的文件夹名称的名称。

@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%

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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