Solved

Replace 1 line in files without changing the modified date

Posted on 2014-01-26
1
315 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using 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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

734 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