1

I am trying insert today's date on a text file at line 59. How do i do this?

Line 59 has like 30 spaces and then reads like this. After the PowerShell script runs it will change the date so it has to be something that doesn't look for the date on the file.

                                Rep.Setproperty "Date","1/1/2013"

I want to just change the date to the date I run the PowerShell script. The text file it is not a PowerShell script so.

This is something that has to run every week with no user interaction. that is why we need the date change by a PowerShell script. I had looked up the link you provided but I am stuck that is why I posted here. there are two things I still can do.

  1. Replace the date at line 59
  2. Send today's date to replace whatever was there.

this is what i have so far.

$date = Get-Date
$CMSReport = Get-content C:\reports\CMSReport.acsauto
$CMSReport | ForEach-Object {
    if ($_.Readcount 59) {
        $_ -replace '\w+','Rep.SetProperty "Dates","2/4/2013"'
    } else {
        $_
    }
} |
Set-Content testCMS.acsauto

obviously the "2/4/2013" will have to be changed with some kind of variable that will take the date of when the command is run.

Drew Lanclos
  • 188
  • 1
  • 11
Carlos
  • 21
  • 1
  • 1
  • 4

1 Answers1

0

Why use PowerShell for a one-time string match/replacement? If this is something you need to do repeatedly across a bunch of files or replace multiple times in a single file, then that's different, but it'd probably take you longer to make the script to do this than to do a one-time replacement (or even twenty of them).

In any case, what you need is going to be found here:

https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx?Redirected=true

Rigged into your scenario:

$infile = "C:\myinputfile.txt"
$outfile = "C:\myoutputfile.txt"
$content = Get-Content -Path $infile
$content[58] = $content[58] -replace "([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d", [datetime]::Today.ToShortDateString()
$content | Set-Content $outfile
Drew Lanclos
  • 188
  • 1
  • 11
  • http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers – jscott Feb 04 '13 at 20:22
  • I'd agree, but with no rep here and no way to ask the poster to flesh out the question with the details necessary, it's a bit difficult to do so. – Drew Lanclos Feb 04 '13 at 20:25
  • Code added to comment. – Drew Lanclos Feb 04 '13 at 20:52
  • This is what line 59 currently has Rep.SetProperty "Dates","2/4/2013" i want to be able to change the "2/4/2013" with today date. there seems to be a problem when i type Rep.SetProperty "Dates","2/4/2013" into the -replace command. – Carlos Feb 04 '13 at 20:55
  • You don't need to add anything to the -replace command at all. It's already matching against the date portion of that line. Example that I used to test on: "Go to the store on 1/5/2013 and get dates" becomes "Go to the store on 2/4/2013 and get dates". – Drew Lanclos Feb 04 '13 at 21:09
  • I copied the you suggested and I get a blank file in return. – Carlos Feb 04 '13 at 21:18
  • It should at least be returning your input. It's only modifying line 59. You should be able to run this script as-is, with the modification I've just made to add assignment lines for your input file and output file. – Drew Lanclos Feb 04 '13 at 21:22
  • this is what I added $CMSReport = Get-content C:\reports\CMSReport.acsauto $CMSReport[59] = $CMSReport[59] -replace "([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d", [datetime]::Today.ToShortDateString() $content | Set-Content C:\reports\testCMS.acsauto – Carlos Feb 04 '13 at 21:24
  • You replaced $content with $CMSReport in line 3, but then did not replace it in line 5. $content is empty in line 5, hence an empty file. Also, do not change 58 to 59 - these are zero-based indices, so $CMSReport[58] means line 59. – Drew Lanclos Feb 04 '13 at 21:32
  • Without a doubt you are absolutely the man. Thank you Very Much. – Carlos Feb 04 '13 at 21:38