How to search and replace a string in multiple text files (within a directory) with Windows CMD? Part 2

4

2

This is in relation to this question .

Unfortunately, PowerShell scripts are disabled on the system I am working on. I can't even use a simple (Get-Content).

I did figure out how to do change specific strings within a specific PS file (thanks to the replies). However, I can only do it on one PS file at a time, and I had to edit the batch file itself by specifying the PS file's name (its hardcoded). All that's left is for the batch file to process ALL PS files within the same directory (no subdirectories).

Here is the code:


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

Fundamentally, I just want to make this code process all PS files within the same directory. Please help. And thanks in advance!

Carla Hook

Posted 2013-12-02T02:21:42.653

Reputation: 455

Answers

2

untested

@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

Endoro

Posted 2013-12-02T02:21:42.653

Reputation: 2 036

This code works! Thanks Endoro. I've been working on it for 2 weeks now, and you solved it in under a day. I am truly grateful. Kudos to you, and have a nice day! – Carla Hook – 2013-12-02T17:19:50.230

1

Just for the record, one line code in linux

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

lisandro

Posted 2013-12-02T02:21:42.653

Reputation: 11

2Please read the question again carefully. Your answer does not answer the original question. – DavidPostill – 2017-03-09T15:14:26.827

1

Finally, after more than 2 weeks, this code finally works! Credits to 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

On to the last part (conversion to pic). Big thanks again to @Endoro (mwahugs!).

Carla Hook

Posted 2013-12-02T02:21:42.653

Reputation: 455