如何在Windows上检索和更改文件的上次修改时间戳?

如何在Windows上检索和更改文件的上次修改时间戳?,windows,batch-file,vbscript,batch-processing,Windows,Batch File,Vbscript,Batch Processing,我需要在Windows和Linux上实现相同的功能 Linux部分是这样完成的: #!/bin/sh path_mainfunctions="../../data/scripts/mainfunctions.lua" path_DisplayError="scripts/DisplayError.lua" path_backup="scripts/mainfunctions.lua" if [ -f $path_backup ]; then # check if $path_main

我需要在Windows和Linux上实现相同的功能

Linux部分是这样完成的:

#!/bin/sh

path_mainfunctions="../../data/scripts/mainfunctions.lua"
path_DisplayError="scripts/DisplayError.lua"
path_backup="scripts/mainfunctions.lua"

if [ -f $path_backup ]; then
    # check if $path_mainfunctions is newer
    alias stat='stat --format=%Y'
    # retrieve Last-Modified Timestamp of mainfunctions.lua
    lmt_mainfunctions=`stat $path_mainfunctions`
    # retrieve Last-Modified Timestamp of backup file
    lmt_backup=`stat $path_backup`
    if [ $lmt_mainfunctions -gt $lmt_backup ]; then
        # mainfunctions.lua is newer
        # backup, append and touch
        cp $path_mainfunctions $path_backup
        cat $path_DisplayError >> $path_mainfunctions
        touch $path_backup
    fi
else
    # backup
    cp $path_mainfunctions $path_backup

    # append DisplayError.lua to mainfunctions.lua
    cat $path_DisplayError >> $path_mainfunctions

    # touch $path_backup to make it newer than
    # modified file $path_mainfunctions
    touch $path_backup
fi
然而,Windows部件很麻烦:

@ECHO off
SET path_mainfunctions="..\..\data\scripts\mainfunctions.lua"
SET path_DisplayError="scripts\DisplayError.lua"
SET path_backup="scripts\mainfunctions.lua"

@ECHO on
:: if it is the first time you install this mod
:: the mod should backup 'mainfunctions.lua'
:: automatically.
::
:: when the game is updated, the mod should
:: be able to detect such update:
::  -   if %path_mainfunctions% is newer than
::      %path_backup%, the mod will install itself
:: 
::  -   otherwise the mod will touch %path_backup%
IF EXIST %path_backup% (
    :: check if %path_mainfunctions% is newer
    ECHO f | XCOPY /d %path_mainfunctions% %path_backup%

    :: TODO How to get last modified timestamp more easily?
    :: SET DATE_MF=FORFILES %path_mainfunctions% /C "cmd /c ECHO @fdate"
    :: SET DATE_BK=FORFILES %path_backup% /C "cmd /c ECHO @fdate"
    ::
    :: TODO which date format will FORFILES command output?
    :: -    DD/MM/YYYY
    :: -    MM/DD/YYYY
    :: -    YYYY/MM/DD
    ::
    :: split the string and do math to get timestamp
    :: NOTE:
    :: -    SET /A RESULT = %VAR_A% * %VAR_B%
    ::
    :: SET TIME_MF=FORFILES %path_mainfunctions% /C "cmd /c ECHO @ftime"
    :: SET TIME_BK=FORFILES %path_backup% /C "cmd /c ECHO @ftime"
    ::
    :: TODO compare last-modified time-stamp and do something
) ELSE (
    :: Backup mainfunctions.lua
    ECHO f | XCOPY %path_mainfunctions% %path_backup%

    :: Append DisplayError.lua to mainfunctions.lua
    TYPE %path_DisplayError% >> %path_mainfunctions%

    :: touch %path_backup% to make it newer than
    :: modified file %path_mainfunctions%
    :: TODO how to touch a file in Windows ????
)
问题是我不知道如何在Windows上检索和更改文件上次修改的时间戳,以防您忘记我的标题:)


如果您知道如何使用VBScript解决问题,我想知道它是如何一步一步实现的,谢谢:)

这里我将介绍一个纯批处理解决方案

,虽然完全不凭直觉

copy /b "somepath\file.ext"+,, "somepath\file.ext" >nul
可以比较文件时间戳,但它非常复杂。你可以。时间戳的格式使得字符串比较几乎等同于按时间顺序进行的比较。问题在于时间戳包含时区偏移量,表示为与UTC(GMT)的分钟差。因此,在从夏时制转换到标准时间的过程中,每年有一个小时,简单的字符串比较会给出错误的答案。如果计算机上的时区发生了更改,您也可能会得到错误的答案。补偿是可能的,但有一个更简单的解决方案:-)

。只需将数据文件(保留时间戳)复制到与更新文件相同的文件夹中即可。然后可以使用DIR命令按时间顺序列出文件,最后列出的文件将是最新的

总而言之,我得到:

@echo关闭
setlocal
::设置
设置“数据路径=..\..\data\scripts”
设置“updatePath=scripts”
设置“data=mainfunctions.lua”
设置“update=DisplayError.lua”
::复制原件以用于日期比较和备份创建
复制/y“%dataPath%\%data%”%updatePath%\%data%。比较“>nul
::如果这是第一次安装,则转到:更新
如果不存在“%updatePath%\%data%”转到更新
::按时间顺序列出更新和比较文件
当前位置列出的最后一个将是最新的。
对于/f“delims=”%%f in(
'目录/b/od”%updatePath%\%data%。比较”“%updatePath%\%update%”
)是否设置“最新=%%F”
::如果最新等于更新,则转到:更新
如果“%latest%”eq“%update%”转到更新
::无事可做,请清理并退出
删除“%updatePath%\%data%.比较”
退出/b
:更新
::执行更新
键入“%updatePath%\%update%”>>“%dataPath%\%data%”
::创建备份(同时有效地“删除”比较文件)
移动/y“%updatePath%\%data%”。比较“%updatePath%\%data%”>nul
::“触摸”备份,使其上一次修改的日期大于更新日期
复制/b“%updatePath%\%data%”+,“%updatePath%\%data%”>nul
退出/b

这里我将介绍一个纯批量解决方案

,虽然完全不凭直觉

copy /b "somepath\file.ext"+,, "somepath\file.ext" >nul
可以比较文件时间戳,但它非常复杂。你可以。时间戳的格式使得字符串比较几乎等同于按时间顺序进行的比较。问题在于时间戳包含时区偏移量,表示为与UTC(GMT)的分钟差。因此,在从夏时制转换到标准时间的过程中,每年有一个小时,简单的字符串比较会给出错误的答案。如果计算机上的时区发生了更改,您也可能会得到错误的答案。补偿是可能的,但有一个更简单的解决方案:-)

。只需将数据文件(保留时间戳)复制到与更新文件相同的文件夹中即可。然后可以使用DIR命令按时间顺序列出文件,最后列出的文件将是最新的

总而言之,我得到:

@echo关闭
setlocal
::设置
设置“数据路径=..\..\data\scripts”
设置“updatePath=scripts”
设置“data=mainfunctions.lua”
设置“update=DisplayError.lua”
::复制原件以用于日期比较和备份创建
复制/y“%dataPath%\%data%”%updatePath%\%data%。比较“>nul
::如果这是第一次安装,则转到:更新
如果不存在“%updatePath%\%data%”转到更新
::按时间顺序列出更新和比较文件
当前位置列出的最后一个将是最新的。
对于/f“delims=”%%f in(
'目录/b/od”%updatePath%\%data%。比较”“%updatePath%\%update%”
)是否设置“最新=%%F”
::如果最新等于更新,则转到:更新
如果“%latest%”eq“%update%”转到更新
::无事可做,请清理并退出
删除“%updatePath%\%data%.比较”
退出/b
:更新
::执行更新
键入“%updatePath%\%update%”>>“%dataPath%\%data%”
::创建备份(同时有效地“删除”比较文件)
移动/y“%updatePath%\%data%”。比较“%updatePath%\%data%”>nul
::“触摸”备份,使其上一次修改的日期大于更新日期
复制/b“%updatePath%\%data%”+,“%updatePath%\%data%”>nul
退出/b

您始终可以安装MSYS并使用
touch
命令。我看到你在处理lua脚本了。你为什么不在卢阿这样做?我知道一个大的.bat脚本意味着一个大麻烦。我正在为一个基于Lua的游戏创建一个mod,在这个游戏中没有Lua.exe,除了我的mod之外,我不想安装任何其他东西。我的mod包括在这个游戏的核心文件中添加一个文件,这意味着bash、batch或vbscript更好。我认为这是一个非常棘手的同步问题。my mod将覆盖在文件“main functions.lua”中调用mod后声明的函数…要更新修改的日期和时间,请使用
copy/b filename.ext+,
始终安装MSYS并使用
touch
命令。我看到你在处理lua脚本了。你为什么不在卢阿这样做?我知道一个大的.bat脚本意味着一个大麻烦。我正在为一个基于Lua的游戏创建一个mod,在这个游戏中没有Lua.exe,除了我的mod之外,我不想安装任何其他东西。我的mod包括在这个游戏的核心文件中添加一个文件,这意味着bash、batch或vbscript更好。我认为这是一个非常棘手的同步问题。my mod将覆盖在文件“main functions.lua”中调用mod后声明的函数,以更新修改后的日期和时间使用