Re: Adding a leading zero
Re: Adding a leading zero
- Subject: Re: Adding a leading zero
- From: "Mark J. Reed" <email@hidden>
- Date: Wed, 4 Apr 2007 20:34:30 -0400
ls -l is probably the worst way possible to extract the modification time of a file for programmatic purposes. We've just had at least two threads on this in the past month alone...
If you're on Tiger, use the stat command.
stat -f %Sm -t "%b%d%Y" /Users/gunno/Library/Preferences/Flexklockan.plist
Apr042007
Note the leading zero included in the expansion of %d. You can also get ISO8601 time:
stat -f %Sm -t %F ...
gives 2007-04-04. There are many other ways to format the date; see the strftime(3) man page for a dizzying array of possibilities for what to put after the -t.
If you're not on Tiger, your best bet would be to move up from awk to one of the next generation of scripting languages, the usual suspects being Perl, Python, Ruby, Tcl. As covered in the above earlier threads, all provide an easy way to get at the modification time of a file and format it however you want.
For future reference, however, awk has printf, which means it's trivial to get any number of leading zeroes:
ls -l /Users/gunno/Library/Preferences/Flexklockan.plist | awk '{printf "%ss%s\n", $6, $7, $9}'
No need to even use sprintf to make a string and assign it somewhere and
then print it, when you can just do the formatting and printing all at once.
Remember, printf came first; sprintf is a modification of it - "string
printf", or "do what printf does but into a string instead of to
standard output". So usually if a language has sprintf it also has
printf, and if you just want to output something, that's the way to go.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden