【问题标题】:Touch file to update the date/time of file? (Modified Date) (WindowsXP BatchScript)触摸文件以更新文件的日期/时间? (修改日期)(WindowsXP BatchScript)
【发布时间】:2011-06-21 21:17:04
【问题描述】:

什么是批处理脚本命令来触摸文件以将其日期和时间更新为当前日期和时间? (修改日期)

例如,Joe Blow 向我发送了他几个月前创建的文本文档,当他通过电子邮件将文档发送给我时,我想跟踪它从我收到它之日起(而不是他创建它时)的年龄,所以我想将文件的日期/时间更新为当前日期/时间。

我有一个批处理脚本可以自动清除 90 天内未编辑的文件,所以当我收到一个特别旧的文件然后突然消失时,这很麻烦。

我需要通过批处理脚本来管理它,因为我有数百个文件要管理,而且它用于归档文件。

【问题讨论】:

标签: file datetime batch-file


【解决方案1】:

我不能把所有功劳都归功于我,但我确实查看了我的 todo.txt 以找到它

Microsoft "touch" 这是 5 年前的知识库文章

主要是你使用copy /b MYFILENAME +,, MYFILENAME 是你的文件

【讨论】:

  • 为了确保正确性,当使用长文件名时,是 ["C:\Documents and Settings\User\file.txt" +,, ] 还是 ["C: \Documents and Settings\User\file.txt +,," ]?
【解决方案2】:

这是对 aflat 的扩展。

1) Microsoft 支持提供了一个解释和简单的 touch.bat 脚本。 2) 我的自定义 touch.cmd,它通过包含“touch /?”扩展了 MS 脚本帮助文本。 3)我的自定义midastouch.cmd,提供了递归操作和日期操作等多种选项。

正如 aflat 所写,简单的答案是:

copy /b FILENAME +,,

如您所料,FILENAME 可以包含相对或绝对路径以及 *.txt 等通配符。

1. Microsoft1 支持:

以下 MS-DOS 命令更新日期和时间戳 名为“EXAMPLE”的文件,而不更改文件的内容。这 类似于 XENIX 和某些第三方中的 TOUCH 实用程序 MS-DOS 工具包。

COPY /B EXAMPLE +,,

COPY 命令可以在以下情况下将文件连接到现有文件上 使用形式:

COPY FILE1+FILE2

在本例中,FILE2 的内容被附加到 FILE1,而 FILE2 保持不变。在这种模式下复制时,COPY 命令 切换到 ^Z (0x01A) 文件结束标记的 ASCII 模式 荣幸。

因此,使用上述命令,/b 强制 COPY 命令进入 二进制模式,文件名是要更新的文件,+(加号) 表示要附加一个文件,并且 ,,(逗号)是 其余参数的占位符(不包括在 这个例子)。因为没有指定要追加的文件,所以 COPY 命令将不附加任何内容,仅更新时间和日期 文件的邮票。

以下批处理文件 TOUCH.BAT 可用于自动执行 进程:

@echo off    
if %1.==. goto end    
if not exist %1 goto end    
copy /b %1 +,, > nul    
echo %1 touched!    
:end

此批处理文件需要一个参数,即要“触摸”的文件。如果 未提供参数,第 2 行将导致批处理文件 什么都不做就退出。如果指定的文件不存在, 第 3 行也会导致批处理文件退出。

2。触摸.cmd

@echo off
:: -----------------------------------------
:: Process input parameter
:: -----------------------------------------

: Help requestes?
if "%1%"=="/?" goto help
if "%1%"=="?" goto help
if "%1%"=="" goto help

:: -----------------------------------------
:: Update Modified Date property to now
:: -----------------------------------------
if not exist %1% goto end    
copy /b %1% +,, > nul    
echo %1 touched!    
goto end

:help

@echo off
echo :: --------------------------------------------------------------
echo :: Touch.cmd Help
echo :: --------------------------------------------------------------
echo.
echo Touch.cmd is batch script to update the Modified Date property 
echo of teh specified file to the current
echo date and time.  
echo.
echo Syntax: touch filename
echo         where,
echo         filename is the name of the name of the file to "touch."
echo         filename may include a relative o full path.
echo         filename may include wild cards like *.txt.
echo.

:end

3。 MidasTouch.cmd

@echo off
:: -----------------------------------------
:: Find files older than specified date
:: -----------------------------------------

:: -----------------------------------------
:: Default Values
:: -----------------------------------------
set "default_path=%cd%"
set "default_err_log=%cd%\midastouch_err.log"
set /a default_date=-365
set "open_log=False"
set "recurse=True"

:: -----------------------------------------
:: Process input parameters 
:: -----------------------------------------

: Help requestes?
if "%1%"=="/?" goto help
if "%1%"=="?" goto help
if /I "%1%"=="help" goto help

set "dir_in="
set "err_log="
set "dd="

:: Read in commandline arguements.
echo Arguements:
:arguement_loop
    if "%1%"=="/p" (
        echo     Path: %2%
        set dir_in=%2%
        shift
        goto loop_bottom)
    if "%1%"=="/l" (
        echo     Error log: %2%
        set err_log=%2%
        shift
        goto loop_bottom)    
    if "%1%"=="/-l" (
        echo     No error log.  Output to console.
        set err_log=CON
        shift
        goto loop_bottom)    
    if "%1%"=="/d" (
        echo     Date: %2%
        set /a dd=%2% 
        shift
        goto loop_bottom)
    if "%1%"=="/o" (
        echo     Open log: True
        set "open_log=True"
        goto loop_bottom)
    if "%1%"=="/-o" (
        echo     Open log: False
        set "open_log=False"
        goto loop_bottom)
    if "%1%"=="/s" (
        echo     Recursive: True
        set "recurse=True"
        goto loop_bottom)
    if "%1%"=="/-s" (
        echo     Recursive: False
        set "recurse=False"
        goto loop_bottom)
    if not "%1%"=="" (
        if "%dir_in%"=="" (
            echo     Path: %1%
            set dir_in=%1%
            goto loop_bottom)
        if "%err_log%"=="" (
            echo     Error log: %1%
            set err_log=%1%
            goto loop_bottom)
        if "%dd%"=="" (
            echo     Date: %1%
            set /a dd=%1%
            goto loop_bottom)
    )
:loop_bottom
shift
if not "%1%"=="" goto arguement_loop

if "%dir_in%"=="" ( 
    set dir_in=%default_path%)
if "%err_log%"=="" ( 
    set err_log=%default_err_log%)
if "%dd%"=="" ( 
    set /a dd=%default_date%)

:: -----------------------------------------
:: Execution
:: -----------------------------------------

:: Write header
set "header=Touch_dir.cmd Error Log"
if exist %err_log% (
    del /q %err_log%)
@echo %header% >%err_log%

set cmd_str="cmd /c copy /b @path +,,"

:: Update Modified Date property to now
if /I "%recurse%"=="True" (
    set cmd_str=forfiles /s /p %dir_in% /d %dd% /c %cmd_str%
) else (
    set cmd_str=forfiles /p %dir_in% /d %dd% /c %cmd_str%
)
echo Command: %cmd_str% >>%err_log%
echo Errors: >>%err_log%
echo. >>%err_log%
echo Executing command: %cmd_str%
@echo Updating Modified Date of files older than date: %dd%.
@echo This may take a while.  Please be patient...
@echo.
echo.
set cmd_str=%cmd_str% || @echo Failed to update: @path >>%err_log%"
%cmd_str%

:: Results
@echo Error log: %err_log% 
if "%open_log%"=="True" (start %err_log%)

goto end

:help
@echo off
echo :: --------------------------------------------------------------
echo :: Touch_dir.cmd Help
echo :: --------------------------------------------------------------
echo.
echo Touch.cmd is batch script to recursively "touch" all files in a
echo folder to update their Modified Date property to the current
echo date and time.  
echo.
echo Syntax: touch_dir /d directory /l err_log /m months
echo         where,
echo         /p  path       Path containing files with Modified
echo                        Date values to update to now.
echo                        (default = current directory).
echo         /s  (or /-s)   Recursive (or not recursive) search 
echo                        (default recursive is %recurse%).
echo         /l  err_log    Error log: list of files not updated
echo                        (default err_log: midastouch_err.log).
echo         /o  (or /-o)   Open (or do not open) error log after 
echo                        execution (default open_log: %open_log%).
echo         /d  date       Selects files with a last modified date greater
echo                        than or equal to (+), or less than or equal to
echo                        (-), the specified date using the
echo                        "MM/dd/yyyy" format; or selects files with a
echo                        last modified date greater than or equal to (+)
echo                        the current date plus "dd" days, or less than or
echo                        equal to (-) the current date minus "dd" days. A
echo                        valid "dd" number of days can be any number in
echo                        the range of 0 - 32768.
echo                        "+" is taken as default sign if not specified.
echo                        (default date is: %default_date%).
echo.

:end

【讨论】:

    【解决方案3】:

    只是补充一点,相同的来源提供了一个批处理脚本。

    @echo off
    goto start
    
    :usage
    echo usage: TOUCH.BAT "MYFILENAME"
    exit /b 0
    
    :start
    if %1.==. goto usage
    if not exist %1 goto usage
    copy /b %1 +,, > nul
    echo %1 touched!
    exit /b 0
    

    【讨论】:

      猜你喜欢
      • 2013-01-12
      • 2013-02-18
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多