Solved

Replace 1 line in files without changing the modified date

Posted on 2014-01-26
1
304 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 39

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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