如何使用 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

最后,经过两周多的时间,这段代码终于可以运行了!感谢 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 中的一行代码

查找/home/usuario/micarpeta/-name *.txt-exec sed-i“s/OldStringx/NewStringx/g”{} \

相关内容