NoNuthinNewb
NoNuthinNewb

Reputation: 11

Storing and Resetting LastModifiedDate for a file

I am attempting to add a text string to a file without updating the LastModifiedDate property. Is it possible to set the property to a variable before modifying the file, then reset the property to the stored value in the variable? I am attempting to do this on a folder containing hundreds or possibly thousands of files.

I think I have the right syntax to add the line of text I need and to set the last modified date to a variable but I'm not sure what syntax I need to reset the modified date in the for each loop.

Get-ChildItem C:\Temp -Name *.bnf* -Recurse |Out-file C:\Temp\list.txt
cd C:\Temp
$FileList = Get-Content C:\Temp\Grammarlist.txt
(Get-Content $FileList) | 
  Foreach-Object {
  $temp = Get-Item $_.LastWriteTime}
    $_ # send the current line to output
    if ($_ -match "// Version") 
    {
        #Add Lines after the selected pattern 
        "// Release 19.1.4"
                }
} | Set-Content $FileList

Upvotes: 1

Views: 52

Answers (1)

H. Ross
H. Ross

Reputation: 540

I would suggest using the read/write LastWriteTime property of the file.

Adapted from ss64.com:

$oldDate = (dir test.txt).LastWriteTime
echo asdf >> test.txt
(dir test.txt).LastWriteTime = $oldDate

Upvotes: 2

Related Questions