Jun 052016

one of my usb hard drives went belly-up lately, and so i replaced it & in the process rearranged all my hard drives & backups.  the files all move ok, but i find that the folder datestamps are all wrong, which is a pain.

i couldn’t find any routine that would do that quickly & easily.  so i wrote (and so i’m sharing also) this windows powershell routine to correct the folder datestamps.  the timestamp turns out to be midnight (00:00) but i didn’t care about that.

to run the routine

create a .ps1 batch file to call the powershell routine.  copy the ps1 batch file & the ps1 script file into the same folder where the folders to update are..  run the ps1 batchfile.

note! the routine fails on all but basic characters, and on a lot of them too.  so folders with names including @ [ ] & and some other characters are a problem.  (i fix that when i create the ps1 batch file in excel, actually.)

ps1 batch file syntax

.\folderdate_v3.ps1 "foldername" "yyyy-mm-dd" "reference"

including the ” as shown above, where
.\  is because i copy the files into the same folder as noted above
folderdate_v3.ps1  is the name of the routine to run
“foldername” is the name of the folder to update
“yyyy-mm-dd” is the new date stamp (e.g. “2016-06-01”)
“reference” is any text you want (it will display on the output but doesn’t actually do anything

this file can have as many lines as you want, 1 line per folder to update.

sample file: folderdateBAT.zip

the ps1 script

this is stored in a separate .ps1 file.  i called my file ‘folderdate_v3.ps1’

sample file: folderdate_v3.zip

param([string]$Vfoldername = $args[0], [string]$Vdate = $args[1], [string]$Vcat = $args[2])

$Vtestpath = Test-Path $Vfoldername

if ( $Vdate -eq '1900-01-00') 
 { Write-Host "Skipped (date)" $Vcat " " $Vdate " " $Vfoldername} 
 if ( $Vtestpath -eq 'True')
 $a = get-item $Vfoldername
 $a.CreationTime = $Vdate
 $a.LastWriteTime = $Vdate
 $a.LastAccessTime = $Vdate
 Write-Host "UPDATED" $Vcat " " $Vdate " " $Vfoldername
 else { Write-Host "Skipped (path)" $Vcat " " $Vdate " " $Vfoldername }




 Posted by at 11:46

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>