这是对 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