Windows CMD를 사용하여 디렉터리 내 여러 텍스트 파일의 문자열을 검색하고 바꾸는 방법은 무엇입니까? 2 부

Windows CMD를 사용하여 디렉터리 내 여러 텍스트 파일의 문자열을 검색하고 바꾸는 방법은 무엇입니까? 2 부

이는 다음과 관련이 있습니다.이 질문.

안타깝게도 제가 작업 중인 시스템에서는 PowerShell 스크립트가 비활성화되어 있습니다. 간단한 (Get-Content).

답변 덕분에 특정 PS 파일 내에서 특정 문자열을 변경하는 방법을 알아냈습니다. 하지만 한 번에 하나의 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주 이상이 지난 후에 이 코드가 마침내 작동합니다! 엔도로에 대한 크레딧.

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

기록을 위해 리눅스의 한 줄 코드

/home/usuario/micarpeta/ -name *.txt -exec sed -i "s/OldStringx/NewStringx/g" {} \ 찾기

관련 정보