Geo
Geo

Reputation: 96797

Platform independent way of doing a touch on a file?

How could a platform independent touch be achieved, without using the actual touch executable? I cannot rely on touch being in PATH, or even existing on a particular system.

Upvotes: 1

Views: 92

Answers (2)

payne
payne

Reputation: 14177

touch is a pretty simple program. You could easily extract the essential steps and implement them as a subroutine in your system. See the source code.

touch depends on utime(), which is POSIX and should be available on most platforms.

Alternatively, you could just provide your own touch implementation as an executable (if you need to call it from a script).

Upvotes: 1

maraguida
maraguida

Reputation: 68

Create an empty file and copy the original file + the empty file back to the original file. I just tested it on Windows and it worked.

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>

Upvotes: 1

Related Questions