2011-06-21 138 views
2

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

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

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

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

+1

可能重复:// stackoverflow.com/questions/51435/windows-version-of-the-unix-touch-command) – Helen

回答

6

我不能把所有的荣誉,但我没有仔细看一下我todo.txt找到它

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

它的JIST是您使用copy /b MYFILENAME +,,其中MYFILENAME是您的文件

+0

只是为了确保正确性,当使用长文件名时,它会是[“C :\ Documents and Settings \ User \ file.txt“+ ,,]或[”C:\ Documents and Settings \ User \ file.txt + ,,“]? – Mechaflash

+0

我用“文件”+ ,, – Mechaflash

0

只需添加相同的源提供批处理脚本。

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

这是aflat的anwer的扩展。

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

由于aflat写道,简单的答案是:

copy /b FILENAME +,, 

正如你所预料的,文件名可以包括相对或绝对路径,并像* .TXT通配符。

1.微软1支持:

以下MS-DOS命令更新一个 文件命名为“榜样”的日期和时间标记不改变文件的内容。此 与XENIX和某些第三方 MS-DOS工具包中的TOUCH实用程序类似。

COPY /B EXAMPLE +,, 

COPY命令可以连接一个文件上时在形式使用 现有文件:

COPY FILE1+FILE2 

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

因此,与上述命令时,/ 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. Touch.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 
的[Windows版本Unix的触摸命令的](HTTP
相关问题