Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Replace 1 line in files without changing the modified date

Posted on 2014-01-26
1
Medium Priority
?
332 Views
Last Modified: 2014-01-27
Hi,

I want to change a single word in all files in a specific folder, but I don't want the modified date changed.

Is it possible.

With this script it changes the word but also changes the modified date.

$Path = "\\Archive\test\Archive\*.xml"
Get-ChildItem -Path "$Path" |
foreach-Object{
        if((Get-Content $_) -match 'http://Archive/test/VPCflow/Forms/template.xsn'){
                "$_"
                 (Get-Content $_) |
                 Foreach-Object {$_ -replace 'http://Archive/test/VPCflow/Forms/template.xsn', 'http://Archive/test/Archive/Forms/template.xsn'} |
                 Set-Content $_
         }
}
0
Comment
Question by:Sum Wum
1 Comment
 
LVL 41

Accepted Solution

by:
footech earned 2000 total points
ID: 39811267
Kind of an odd request.
When you modify a file, the modified time is always updated, however it is possible to set this property.  First you have to store the original time, and after the file modification, set the property to this time.
$Path = "\\Archive\test\Archive\*.xml"
Get-ChildItem -Path "$Path" | ForEach-Object {
    if ((Get-Content $_) -match 'http://Archive/test/VPCflow/Forms/template.xsn')
    {
        "$_"
        $modtime = $_.LastWriteTime
        (Get-Content $_) | 
        Foreach-Object { $_ -replace 'http://Archive/test/VPCflow/Forms/template.xsn', 'http://Archive/test/Archive/Forms/template.xsn' } |
        Set-Content $_
        $_.LastWriteTime = $modtime
    }
}

Open in new window

0

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Screencast - Getting to Know the Pipeline

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question