Risorse per professionisti IT > Home page del forum > Windows PowerShell > replace text in file using wildcards
Formula una domandaFormula una domanda
 

Con rispostareplace text in file using wildcards

  • mercoledì 24 giugno 2009 11.18mnemonicator Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Hi there, i have next text in my file *20090623* this is date, i need change this text every day, so i wrote next code

    $getdate_curr=get-date -format yyyMMdd (assign to variable current date in required format) and then i'm trying replace old date to new one

    (Get-Content D:\I\moveit\config.par) |
    Foreach-Object {$_ -replace "2009*\*",$getdate_curr} |
    Set-Content D:\I\moveit\config.par

    It's work incorrectly, how can i change this code?

    Thank you.

Risposte

  • mercoledì 24 giugno 2009 18.41Marco ShawMVP, ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Check this out:
    $today=get-date -format yyyMMdd
    
    $today
    
    $yesterday=(get-date).adddays(-1)|%{
      if($_.month -lt 10){$month=[string]0+$_.month}else{$month=$_.month}
      if($_.day -lt 10){$day=[string]0+$_.day}else{$day=$_.day}
      [string]$_.year+[string]$month+[string]$day
    }
    
    $yesterday
    
    "$yesterday".replace($yesterday,$today)
    
    Using the format you're looking for to calculate yesterday is a PAIN!  Maybe there's an easier though.

    That should help you out...
    • Contrassegnato come rispostamnemonicator giovedì 25 giugno 2009 6.31
    •  

Tutte le risposte

  • mercoledì 24 giugno 2009 11.56Marco ShawMVP, ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    Are you always replacing the date from the day before?  I'm thinking you would create a variable for yesterday and today, and then use those variables in the replace to do an exact replacement.
  • mercoledì 24 giugno 2009 14.36mnemonicator Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     

    yes, you are correct. i'll try to do this, it's my first steps in PS...

  • mercoledì 24 giugno 2009 18.41Marco ShawMVP, ModeratoreMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     Con rispostaContiene codice
    Check this out:
    $today=get-date -format yyyMMdd
    
    $today
    
    $yesterday=(get-date).adddays(-1)|%{
      if($_.month -lt 10){$month=[string]0+$_.month}else{$month=$_.month}
      if($_.day -lt 10){$day=[string]0+$_.day}else{$day=$_.day}
      [string]$_.year+[string]$month+[string]$day
    }
    
    $yesterday
    
    "$yesterday".replace($yesterday,$today)
    
    Using the format you're looking for to calculate yesterday is a PAIN!  Maybe there's an easier though.

    That should help you out...
    • Contrassegnato come rispostamnemonicator giovedì 25 giugno 2009 6.31
    •  
  • giovedì 25 giugno 2009 6.31mnemonicator Medaglie utenteMedaglie utenteMedaglie utenteMedaglie utenteMedaglie utente
     
    thank you so much.