2011-03-14 22 views
1

如何在不使用實際的touch可執行文件的情況下實現獨立於平臺的觸摸?我不能依賴於PATH中的touch,或者甚至不存在於特定系統上。平臺無關的文件觸摸方式?

回答

1

創建一個空文件並將原始文件+空文件複製回原始文件。我只是在Windows上測試它,它工作。

 
C:\tmp>dir \utils\emp* 
Volume in drive C has no label. 
Volume Serial Number is BC80-0D15 

Directory of C:\utils 

2011-03-14 11:58     0 empty_file 
       1 File(s)    0 bytes 
       0 Dir(s) 27,506,368,512 bytes free 

C:\tmp>dir *.gif 
Volume in drive C has no label. 
Volume Serial Number is BC80-0D15 

Directory of C:\tmp 

2010-10-08 12:00   20,463 cknight.gif 
2009-10-30 17:31   1,298,525 img-big.gif 
2009-10-30 17:46   225,992 img.gif 
       3 File(s)  1,544,980 bytes 
       0 Dir(s) 27,506,368,512 bytes free 

C:\tmp>copy /b img.gif+\Utils\empty_file /b img.gif 
img.gif 
\Utils\empty_file 
     1 file(s) copied. 

C:\tmp>dir *.gif 
Volume in drive C has no label. 
Volume Serial Number is BC80-0D15 

Directory of C:\tmp 

2010-10-08 12:00   20,463 cknight.gif 
2009-10-30 17:31   1,298,525 img-big.gif 
2011-03-14 12:07   225,992 img.gif 
       3 File(s)  1,544,980 bytes 
       0 Dir(s) 27,506,368,512 bytes free 

C:\tmp> 
+1

我認爲通過運行不帶參數的'touch',它會修改上次修改的時間戳。這就是我所追求的。 – Geo 2011-03-14 11:30:44

+0

@geo:我用可能的解決方案編輯了我的答案 – maraguida 2011-03-14 12:13:38

1

touch是一個非常簡單的程序。您可以輕鬆提取基本步驟並將其作爲系統中的子例程來實現。見the source code

touch取決於utime(),這是POSIX,應該在大多數平臺上可用。

或者,您可以將自己的觸摸實現作爲可執行文件提供(如果需要從腳本調用它)。