Search
Joe Pruitt - A Software Architect's take on Network Security
You are here: DevCentral > Weblogs

posted on Thursday, May 07, 2009 10:09 AM

PowerShell_unix PowerShell is definitely gaining momentum in the windows scripting world but I still hear folks wanting to rely on Unix based tools to get their job done.  In this series of posts I’m going to look at converting some of the more popular Unix based tools to PowerShell.

touch

The Unix “touch” command is used to change a file’s access and modification timestamps.  It can also be used to create a new empty file.

The options to the Unix touch command are implemented with the following PowerShell parameters:

Unix PowerShell Description
FILE -filespec The files to modify.
-d -datetime Parse STRING and use it instead of current time.
-F -forward Modify the time by going forward SECONDS seconds.
-r -reference Use this file as a reference instead of the current time.
-m -only_modification Change only the modification time.
-a -only_access Change only the access time.

 

   1: #----------------------------------------------------------------
   2: # Touch.ps1
   3: #----------------------------------------------------------------
   4: param
   5: (
   6:   [string]$filespec = $null,         # Files to touch
   7:   [DateTime]$datetime = ([DateTime]::Now), # Use DateTime STRING instead of current time.
   8:   [int]$forward = 0,                 # Modify the time by going forward SECONDS s.
   9:   [string]$reference = $null,        # Use this file's time instead of current time.
  10:   [bool]$only_modification = $false, # Change only the modification time.
  11:   [bool]$only_access = $false        # Change only the access time
  12: );
  13:  
  14: #----------------------------------------------------------------
  15: # Function Do-Touch
  16: #----------------------------------------------------------------
  17: function Do-Touch {
  18:   param(
  19:     [string]$filespec = $null,
  20:     [datetime]$datetime = ([DateTime]::Now),
  21:     [int]$forward = 0,
  22:     [string]$reference = $null,
  23:     [bool]$only_modification = $false,
  24:     [bool]$only_access = $false
  25:   );
  26:   
  27:   $touch = $null;
  28:   
  29:   if ( $filespec )
  30:   {
  31:     $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue)
  32:     if ( !$files )
  33:     {
  34:       # If file doesn't exist, attempt to create one.
  35:       # A wildcard patter will fail silently.
  36:       Set-Content -Path $filespec -value $null;
  37:       $files = @(Get-ChildItem -Path $filespec -ErrorAction SilentlyContinue);
  38:     }
  39:     
  40:     if ( $files )
  41:     {
  42:       if ( $reference )
  43:       {
  44:         $reffile = Get-ChildItem -Path $reference -ErrorAction SilentlyContinue;
  45:         if ( $reffile )
  46:         {
  47:           [datetime]$touch = $reffile.LastAccessTime.AddSeconds($forward);
  48:         }
  49:       }
  50:       elseif ( $datetime )
  51:       {
  52:         [datetime]$touch = $datetime.AddSeconds($forward);
  53:       }
  54:       
  55:       if ( $touch )
  56:       {
  57:         [DateTime]$UTCTime = $touch.ToUniversalTime();
  58:         foreach ($file in $files)
  59:         {
  60:           if ( $only_access )
  61:           {
  62:             $file.LastAccessTime=$touch
  63:             $file.LastAccessTimeUtc=$UTCTime
  64:           }
  65:           elseif ( $only_modification )
  66:           {
  67:             $file.LastWriteTime=$touch
  68:             $file.LastWriteTimeUtc=$UTCTime
  69:           }
  70:           else
  71:           {
  72:             $file.CreationTime = $touch;
  73:             $file.CreationTimeUtc = $UTCTime;
  74:             $file.LastAccessTime=$touch
  75:             $file.LastAccessTimeUtc=$UTCTime
  76:             $file.LastWriteTime=$touch
  77:             $file.LastWriteTimeUtc=$UTCTime
  78:           }
  79:           $file | select Name, *time*
  80:         }
  81:       }
  82:     }
  83:   }
  84: }
  85:  
  86: Do-Touch -filespec $filespec -datetime $datetime -forward $forward -reference $reference `
  87:   -only_modification $only_modification -only_access $only_access;

You can download the full script here: Touch.ps1



Feedback

7/29/2009 8:16 PM
Gravatar nice code.thanks for sharing
technology forum
8/15/2009 8:32 AM
Gravatar thnks for good script
will
6/25/2010 10:44 PM
Gravatar Very good post. I just bookmark your blog and wants to say that I have really enjoyed while reading your posts.
Thank you for sharing the information with all..
bester Online Kasino Bonus
7/19/2010 7:48 PM
Gravatar I'm really happy to read your article.
uggs outlet

Let Me Know What You Think


Please use the form below if you have any comments, questions, or suggestions.

Title:
 
Name:
 
Email: (so we can show your gravatar)
Website:
Comment: Allowed tags: blockquote, a, strong, em, p, u, strike, super, sub, code
 
Please add 3 and 1 and type the answer here:

Blog Stats

Posts:379
Comments:1066
Stories:1
Trackbacks:301
  

Article Categories

  iRules
  

Image Galleries

  

Joe's bookshelf: read

The Lost Gate
4 of 5 stars
This one started slow but I got really got into it about 1/3 of the way through. If you are an Ender's Game fan, you'll probably like this one as well.

goodreads.com


82,243 Members in 102 Countries and Growing!

Join DevCentral Today!

About DevCentral

DevCentral has been a successful, thriving community for many years. We have always strived to bring you the best technical documentation, discussion forums, blogs, media and much more that we can.

So dive in, get familiar with DevCentral. We hope you like it, we hope it makes your job easier, and lets you get that much more power out of the community. To learn more, make sure to check out the Getting Started section. And if you have any problems, or think something could be easier to use, drop us a line to let us know.

Got It !

We've received your comment and transmitted it directly to DevCentral HQ.

Thanks for taking time to let us know what's on your mind. At DevCentral | Community Matters!

Get In Touch With Us

Have questions, suggestions or just want to get something off your chest?

Use our handy form below to Direct Connect with DevCentral Mission Control.

Send Us Feedback       or