注释文件中具有重复模式的行

时间:2015-08-13 12:27:42

标签: powershell batch-file

即将重新制定问题(删除第一个问题,因为我无法回答:-():

我有一个hobocopy bat脚本,除了进行增量备份之外,还生成一个用于恢复备份的文件,使用这种行:

xcopy "w:\path\to\file\2015-08-13__11_56_INCREMENTAL\file" "E:\path\to file\file*" /E /C /I /Q /R /Y /X

每次我执行公司时,第一条路径都会改变。备份,但我想注释掉重复第二个路径的行,除了最后一个。(以避免复制和覆盖文件。

这是bat填充restore.bat:

的部分
for /f "tokens=1,* delims=¶" %%i in ( 'dir /A-D /s /B /W "!ruta_bkp!"') do (    
    set "str=%%i*"
    set str=!str:_INCREMENTAL\=_INCREMENTAL!
    set str=!str:%rutadest%=!
    set str=!str:%times%=!      
    set def=!ruta:~0,3!!str!

    echo xcopy "%%i" "!def!" /E /C /I /Q /R /Y /X >> "!direc!\TO_RESTORE.bat"   
)

如你所见,我为dir commnad中的每个文件写了一行,但是,由于这个文件有老问题的执行,我想要注释掉重复第二条路径的行...所以它会是某种东西像:

for line in TO_RESTORE_BAT{
    findstr pattern(!def!) --> if found --> REM line
}
 echo xcopy %%i !def! >> TO_RESTORE_BAT

或类似的......

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个:

$NewFile = @()
$TextFile = Get-Content c:\test.txt
$TextFile | % {
    if ($_ -match "\D+")
    {
    $NewFile += "REM $_"
    }
    else
    {
    $NewFile += $_
    }
}
$NewFile | Out-File c:\test.txt

*当然,如果重复模式是字母字符,就像您的示例一样,否则您需要更改正则表达式"\D+",这意味着非数字到其他地方

请检查:http://www.powershellcookbook.com/recipe/qAxK/appendix-b-regular-expression-reference