text file editing in bulk (batch file)

mcranmoss

Board Regular
Joined
Dec 13, 2011
Messages
165
I'm wanting to edit identical text in a large number of text files using a batch file. This sample code is attempting to test the principle. But all it does is open the text file.

any advice welcome.

mark

Code:
@echo off
setlocal enabledelayedexpansion
if not exist "%1" V:\Mark\VBA_Excel\BATCHTEST\New.txt &goto :eof
set /p findthis=BOB:
set /p replacewith=FLUTE:

for /f "tokens=*" %%a in (%1) do (
   set write=%%a
   if %%a==%findthis% set write=%replacewith%
   echo !write! 
   echo !write! >>%~n1.replaced%~x1
)
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Basic advice: ask real experts. In this case that means using the alt.msdos.batch.nt newsgroup which is available via Google Groups if you ISP doesn't provide nntp newsgroup access.

You seem to be replacing entire lines. If so, I'd use a subroutine call.

Code:
@echo off & setlocal enableextensions

if not exist "%~1" (
        echo/%~0: file "%~1" doesn't exist
        goto :EOF
) else (
        set out="%~dpn1.replaced%~x1"
)

set/P f=find:
if "%f%" == "" (
        echo/%~0: no find string, nothing to do, ending
        goto :EOF
)

set/P r=replace:
if "%r%" == "" (
        echo/%~0: no replace string, deleting find string '%f%'
)

for /F "delims=" %%a in (%1) do call :PROC "%%a"

goto :EOF

:PROC
(if "%~1" == "%f%" (
        if not "%r%" == "" echo/%r%
) else (
        echo/%1
)) >> %out%

Note that the %1 parameter in :PROC is the %%a from the for loop.

ALWAYS enclose arbitrary filename parameters in quotes AFTER removing possible quotes in the filename parameter. That is, NEVER user either %1 or "%1", ALWAYS use "%~1". Do the same for for loop parameters used in if tests, e.g., "%%~a". Also enclose environment variables in quotes when you use them in if tests.

The for loop options "tokens=*" and "delims=" should produce the same results, but in my expereince the latter is more robust.

Finally, enabledelayedexpansion can be very screwy. Much better not to use it but instead call subroutines with the current loop's parameters %%a, %%b, etc.
 
Upvote 0

Forum statistics

Threads
1,225,611
Messages
6,185,996
Members
453,334
Latest member
Prakash Jha

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top