Solved

Replace 1 line in files without changing the modified date

Posted on 2014-01-26
1
310 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 40

Accepted Solution

by:
footech earned 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
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 antispam), the admini…

685 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