locked
Replacing string in a file RRS feed

  • Question

  • I'm trying to match and replace a string in a file that changes daily.  

    REM Break#1
    LOI 0712 232400 2324 0004 001 001 000030 000000 00000000 000 00SWIFFER28 0000
    LOI 0712 232400 2324 0004 001 002 000030 000000 00000000 000 00SWIFFER28 0000
    REM Break#1
    LOI 0712 232800 2328 0004 001 001 000030 000000 00000000 000 00SWIFFER28 0000
    LOI 0712 232800 2328 0004 001 002 000030 000000 00000000 000 00SWIFFER28 0000

    The string that I'd like to match and replace is "LOI 0***" The LOI 0 will stay but the *** will change.  I've tried variations of the following with no luck:

    $D = Get-Date -Format d
    $D.replace("/","") -replace("2016","")
    $D = $D.replace("/","") -replace("2016","")
    
    $C1 = Get-ChildItem -Path C:\*64001.SCH
    $C2 = Get-ChildItem -Path C:\*64Z01.SCH
    
    Rename-Item -Path $C1 $D'64001.SCH'
    Rename-Item -Path $C2 $D'64Z01.SCH'
    
    
    Get-Content C:\*64001.SCH | ForEach-Object { $_ -replace "LOI 0???","LOI 0'$D'"} | set-content c:\*64001.SCH

    How can I modify the last line to edit those trailing 3 characters after "LOI 0"?

    Thanks,

    Brian


    -Brian

    Wednesday, July 13, 2016 8:12 PM

Answers

  • The following accounts for all months.

    $str=[datetime]::Today.ToString('Mdd')

    $line='LOI 0712 232400 2324 0004 001 001 000030 000000 00000000 000 00SWIFFER28 0000 '
    $line -replace 'LOI\s\d+', ('LOI '+$str)


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, July 19, 2016 6:05 PM
    • Marked as answer by Brian Harkness Tuesday, July 19, 2016 6:56 PM
    Thursday, July 14, 2016 12:54 PM

All replies

  • Let's try to simplify the question. What do you want to change in what text file, based on what date?


    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 13, 2016 8:20 PM
  • What happens to the file name when you get to months 10 through 12?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 13, 2016 8:28 PM
  • OK.

    For example - I have a file called 71264001.SCH; The 712 portion is the date.  Inside the file is the string LOI 0712 which is also the date.

    I want to change the file name with todays date in the format Mdd.  i also want to change the last three characters of LOI 0712 with the same date format.

    I hope that makes sense.


    -Brian

    Wednesday, July 13, 2016 8:29 PM
  • What happens to the file name when you get to months 10 through 12?

    -- Bill Stewart [Bill_Stewart]

    Wednesday, July 13, 2016 8:31 PM
  • I agree with Bill.  You are not seeing the issue.  Months have two digits inside your file and one outside until October.

    This is silly:

    $D = Get-Date -Formatd
    $D
    .replace("/","") -replace("2016","")
    $D
    =$D.replace("/","") -replace("2016","")

    This is what you want:

    [datetime]::Today.ToString('MMdd')

    Try it.


    \_(ツ)_/

    Wednesday, July 13, 2016 9:51 PM
  • Now consider this:

    ([datetime]'11-12-2016').ToString('Mdd')


    \_(ツ)_/

    Wednesday, July 13, 2016 9:52 PM
  • yes, the format will change to MMdd, but I was able to get the filename to change myself.  I just don't know how to replace the string inside the file.  I've searched google and still cannot figure it out.

    -Brian

    Thursday, July 14, 2016 12:32 PM
  • Thanks for the tip.  I am not an avid powershell user.  Just trying to automate a manual procedure changing the dates.  

    I will try your suggestion.  I was able to get the filename to change with my code, but the real issue I'm having is replacing the string inside the file.


    -Brian

    Thursday, July 14, 2016 12:34 PM
  • The following accounts for all months.

    $str=[datetime]::Today.ToString('Mdd')

    $line='LOI 0712 232400 2324 0004 001 001 000030 000000 00000000 000 00SWIFFER28 0000 '
    $line -replace 'LOI\s\d+', ('LOI '+$str)


    \_(ツ)_/

    • Proposed as answer by jrv Tuesday, July 19, 2016 6:05 PM
    • Marked as answer by Brian Harkness Tuesday, July 19, 2016 6:56 PM
    Thursday, July 14, 2016 12:54 PM
  • Thanks, I put it into this and it works:

    (Get-Content C:\*64001.SCH) | ForEach-Object {$_ -replace 'LOI\s\d+',"LOI $Date"} | Set-Content -Path C:\*64001.SCH


    -Brian

    Thursday, July 14, 2016 1:40 PM
  • Thanks, I put it into this and it works:

    (Get-Content C:\*64001.SCH) | ForEach-Object {$_ -replace 'LOI\s\d+',"LOI $Date"} | Set-Content -Path C:\*64001.SCH


    -Brian


    Excellent.  You are on your way.

    \_(ツ)_/

    Thursday, July 14, 2016 3:59 PM
  • Hi, Please try below as

    $str=[datetime]::Today.ToString('MMdd')
    
    Get-Content C:\*64001.SCH | ForEach {$_.remove(4,4).insert(4,$str)} | Set-Content -Path C:\*64001.SCH
    
    $c=get-childitem -path C:\*64001.SCH
    
    $d=$str + '64001.SCH'
    
    rename-item -path $c $d

    Regards

    Kam

    Tuesday, July 19, 2016 4:03 PM
  • Thanks, I put it into this and it works:

    (Get-Content C:\*64001.SCH) | ForEach-Object {$_ -replace 'LOI\s\d+',"LOI $Date"} | Set-Content -Path C:\*64001.SCH


    -Brian


    Please mark an answer so others don't keep trying to add.

    \_(ツ)_/

    Tuesday, July 19, 2016 6:05 PM