2011-03-14 53 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,应该在大多数平台上可用。

或者,您可以将自己的触摸实现作为可执行文件提供(如果需要从脚本调用它)。