PowerShell在不同目录中查找和替换

时间:2016-06-24 13:59:24

标签: powershell replace find

我正在尝试让我的PS脚本在目录中查找如下...

C:\FAKEENV
 └───DEVbox
    ├───PRD
    |    └──BatFile.txt
    └───STG
         └──BatFile.txt

我要做的是查看过去12小时内.txt是否发生了变化。如果有,请替换bat文件中的字符串' dev'到了' stg'在STG文件夹和' stg'到'' (没有)在PRD文件夹中。我已经改变变量并操纵我的代码几个小时而没有运气。我知道我的逻辑是错的,因为当我单步执行它时,永远不会进入将操纵bat文件的if语句。

Clear-Host
$date = Get-Date
$hours = '-12'    

#$StgDestFile = 'C:\FakeEnv\DEVbox\STG'
#$PrdDestFile = 'C:\FakeEnv\DEVbox\PRD'
$dest_file = 'C:\FakeEnv\DEVbox'

foreach($file in (Get-ChildItem $dest_file -Recurse))
{
  #Check for changes from last $hours
  if($file.LastWriteTime -gt ($date).AddHours($hours))            
  {
      if($file -eq 'STG')
      {
        (Get-Content $dest_file\STG\BatFile.txt) | Foreach-Object {
          $_ -replace 'dev', 'stg' `
          #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `                   
        } | Set-Content $dest_file    
        #Insert logic for uninstall and install of 'feature'
        #Insert logic for confirmation message of changes
      }
      elseif($file -eq 'PRD')    
      {
        (Get-Content $dest_file\PRD\BatFile.txt) | Foreach-Object {
          $_ -replace 'dev', 'stg' `
          #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `                   
        } | Set-Content $dest_file    
        #Insert logic for uninstall and install of 'feature'
        #Insert logic for confirmation message of changes    
      }
    }
  }
  #If no changes occurded, log that nothing happened       
  else
  {
    $LogDestination = 'C:\FakeEnv\Logs'
    $LogPathExist = Test-Path "$LogDestination"
    #Do this if folder does not exist
    #If the folder has not already been created, create it and write to log file
    if($LogPathExist -eq $false)
    {
      New-Item $LogDestination -ItemType directory
      New-Item $LogDestination\log.log -ItemType file
      Add-Content $LogDestination\log.log "Script detected no changes in $dest_file on $date"
    }
    #Do this is folder does exist
    #if the folder exists, append to log file
    else
    {
      Add-Content $LogDestination\log.log "`nScript detected no changes in $dest_file on $date"
    }
  }    

1 个答案:

答案 0 :(得分:0)

首先确保只获取-File Get-ChildItem开关的文件(PS v3),然后获取文件夹名称并对其进行测试:

Clear-Host

$date = Get-Date
$hours = '-12'
#$StgDestFile = 'C:\FakeEnv\DEVbox\STG'
#$PrdDestFile = 'C:\FakeEnv\DEVbox\PRD'
$dest_file = 'C:\FakeEnv\DEVbox'

#get files only
foreach($file in (Get-ChildItem $dest_file -File -Recurse)) {

    if($file.LastWriteTime -gt ($date).AddHours($hours)) {

        #get parent directory name
        $parent = Split-Path $file.DirectoryName -Leaf

        if($parent -eq 'STG') {
            (Get-Content $dest_file\STG\BatFile.txt) | Foreach-Object {
                $_ -replace 'dev', 'stg' `
                #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `
            } | Set-Content $dest_file
        } elseif($parent -eq 'PRD') {
            (Get-Content $dest_file\PRD\BatFile.txt) | Foreach-Object {
                $_ -replace 'dev', 'stg' `
                #Format: -replace 'SOMETHING', 'SOMETHING ELSE' `
            } | Set-Content $dest_file
        }
    }
} else {
    $LogDestination = 'C:\FakeEnv\Logs'
    $LogPathExist = Test-Path "$LogDestination"        
    if($LogPathExist -eq $false) {
        New-Item $LogDestination -ItemType directory
        New-Item $LogDestination\log.log -ItemType file
        Add-Content $LogDestination\log.log "Script detected no changes in $dest_file on $date"
    } else {
        Add-Content $LogDestination\log.log "`nScript detected no changes in $dest_file on $date"
    }
}

另外,尽量保持缩进一致。