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

05
2014-04
  • Carla Hook

    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!

  • Answers
  • Endoro

    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
    
  • Carla Hook

    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!).


  • Related Question

    text editing - How to do Linux-like file modifications on Windows command line?
  • Sebastian P.R. Gingter

    I need to do some more or less easy replacements in certain files on Windows machines. This is part of an automated software build process, so to be able to reproduce it on all build mashines I absolutely have to rely on tools that are part of windows Windows Vista and Windows 7. No additional software installation (even if it just requires copying onto the mashine) is possible.

    I just need to replace one guid with another and additionaly replace a part-string another easy to recognize pattern.

    What would you suggest? Are there any easy commandline-only tools to do that in windows or do I have to write a powershell script? If the latter, do you have a good reference on powershell (never used it? ).


  • Related Answers
  • Alexander

    I would recommend you to use Windows PowerShell. It's part of the OS since Windows Vista and Windows 7 (can be also installed on Windows XP). PowerShell is a really powerfull object oriented scripting evironment, based on the .NET Framework, for the Windows operating system (simillar to the Linux Bash). There is a lot of documentation availible from Microsoft on how to use PowerShell.

    There is also a good free PowerShell Book availible here.