Windows CMD を使用して複数のテキスト ファイル (ディレクトリ内) 内の文字列を検索して置換する方法 パート 2

Windows CMD を使用して複数のテキスト ファイル (ディレクトリ内) 内の文字列を検索して置換する方法 パート 2

これは、この質問

残念ながら、私が作業しているシステムでは PowerShell スクリプトが無効になっています。単純な(Get-Content).さえ使用できません。

特定の PS ファイル内の特定の文字列を変更する方法はわかりました (返信のおかげです)。ただし、一度に実行できるのは 1 つの PS ファイルのみで、PS ファイルの名前を指定してバッチ ファイル自体を編集する必要がありました (ハードコードされています)。残っているのは、バッチ ファイルで同じディレクトリ (サブディレクトリなし) 内のすべての PS ファイルを処理することだけです。

コードは次のとおりです:


REM Start of Code  
REM Auto-process PS files within a directory  
REM Changes how PS files look when displayed   
REM This batch file searches for instances of   
REM "OldStringx" within the file and replaces it   
REM with "NewStringx"   

REM Thicken line width from 1 to 5  
Set "OldString1=1 setlinewidth"  
Set "NewString1=5 setlinewidth"  

REM Change Courier font to Helvetica  
Set "OldString2=Courier"  
Set "NewString2=Helvetica-Bold"  

REM To do: This batch file should process all PS files within  
REM the same directory where the batch file is located  
REM (Batch file and all PS files to be edited should be  
REM found on the same path).  

REM Specified below is the PS file to edit. Hard-coded for now.    
set file="psfile_to_edit.ps"  

@echo off  
cd /d .  
for /F "usebackq delims=" %%F in (`dir *.ps /b`) do set outFile="%%~nF_edited%%~xF"  
(  
    for /f "skip=2 delims=" %%a in ('find /n /v "" %file%') do (  
        set "ln=%%a"  
        Setlocal enableDelayedExpansion  
        set "ln=!ln:*]=!"  
        if defined ln set "ln=!ln:%OldString1%=%NewString1%!"  
        if defined ln set "ln=!ln:%OldString2%=%NewString2%!"  
        echo(!ln!  
        endlocal  
    )  
)>%outFile%  

REM Convert edited PS files to JPG  
REM This requires convert.exe to work  
REM Currently commented out to debug above parts.  
REM convert.exe %outFile% -autocrop %outfile:~0,-4%.jpg  
REM End of Code

基本的に、このコードで同じディレクトリ内のすべての PS ファイルを処理するようにしたいだけです。助けてください。よろしくお願いします!

答え1

未テスト

@ECHO OFF &SETLOCAL
cd /d . 

for %%x in (*.ps) do call:process "%%~x"
goto:eof

:process 
set "outFile=%~n1_edited%~x1"  
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (  
    set "ln=%%a"  
    Setlocal enableDelayedExpansion  
    set "ln=!ln:*]=!"  
    if defined ln (
        set "ln=!ln:%OldString1%=%NewString1%!"  
        set "ln=!ln:%OldString2%=%NewString2%!"
    )
    echo(!ln!  
    endlocal  
))>"%outFile%"
exit /b

答え2

2 週間以上経って、ついにこのコードが動作するようになりました。Endoro に感謝します。

REM Start of Code  
REM Auto-process PS files within a directory  
REM Changes how PS files look when displayed   
REM This batch file searches for instances of   
REM "OldStringx" within the file and replaces it   
REM with "NewStringx"   

REM Thicken line width from 1 to 5  
Set "OldString1=1 setlinewidth"  
Set "NewString1=5 setlinewidth"  

REM Change Courier font to Helvetica  
Set "OldString2=Courier"  
Set "NewString2=Helvetica-Bold"  

@ECHO OFF &SETLOCAL
cd /d . 
for %%x in (*.ps) do call:process "%%~x"
goto:eof

:process 
set "outFile=%~n1_edited%~x1"  
(for /f "skip=2 delims=" %%a in ('find /n /v "" "%~1"') do (  
    set "ln=%%a"  
    Setlocal enableDelayedExpansion  
    set "ln=!ln:*]=!"  
    if defined ln (
        set "ln=!ln:%OldString1%=%NewString1%!"  
        set "ln=!ln:%OldString2%=%NewString2%!"
    )
    echo(!ln!  
    endlocal  
))>"%outFile%"
exit /b

REM Convert edited PS files to JPG  
REM This requires convert.exe to work  
REM Currently commented out to debug above parts.  
REM convert.exe %outFile% -autocrop %outfile:~0,-4%.jpg  
REM End of Code

最後の部分(画像への変換)に進みます。@Endoro に改めて感謝します(mwahugs!)。

答え3

記録のために、Linuxの1行コード

/home/usuario/micarpeta/ -name *.txt -exec sed -i "s/OldStringx/NewStringx/g" を検索します {} \

関連情報