• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Adding a leading zero
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Adding a leading zero
      • From: "Mark J. Reed" <email@hidden>
References: 
 >Re: Adding a leading zero (From: <email@hidden>)

  • Prev by Date: Re: Can not get "item" from "position"
  • Next by Date: Re: Address book & Groups
  • Previous by thread: Re: Adding a leading zero
  • Next by thread: Re: Adding a leading zero
  • Index(es):
    • Date
    • Thread