2011-06-21 137 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
相關問題