Solved

Replace 1 line in files without changing the modified date

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now