Compare LastWriteTime in an 'if' statement in PowerShell -
i want run below script every 10 minutes.
i have file , file b. every 10 minutes, want check if file newer file b. if so, want set lastwritetime of file b current date. believe if
statement wrong... how can fix it?
$a = get-item c:\users\testuser\desktop\test.xml | select lastwritetime $b = get-item c:\users\testuser\desktop\test.swf | select lastwritetime if($a < $b) { $b.lastwritetime = (get-date) }
i think in above example, i'm setting variable $b current date...i want set lastwritetime of actual file current date.
you similar following change last write time of file:
$file1 = get-childitem -path "c:\temp\test1.txt" $file2 = get-childitem -path "c:\temp\test2.txt" #system.io namespace of file class in .net , getlastwritetime/setlastwritetime methods of class. if ([system.io.file]::getlastwritetime($file1) -lt [system.io.file]::getlastwritetime($file2)) { write-output "file1 lastwritetime less file2, setting lastwritetime on file2." ([system.io.file]::setlastwritetime($file2,(get-date))) } else { write-output "file1 lastwritetime not less file2 lastwritetime." }
more information on namespace, .net class, , associated methods used in above code can found here.
hope helps!
Comments
Post a Comment