Unix To PowerShell - Touch

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

Technorati Tags: , , , , , ,

Categories:  ,  ,  ,  
© 2008 SYS-CON Media