Solved How can I edit text in multple files

August 26, 2014 at 07:52:34
Specs: Windows 7
Using the following code I can achieve the result I want but I can't figure out how to apply it to all the files in the 'set'. I want to edit every PCF file (550 files), replace the old text with the new text and save each file in a new directory. The code I have grasped from forums seems to loop through the number of files in he directory but as I have a named source and desinaion it just keeps adding to the same file unil the loop ends.

:Test1

REM Set the source location
Set Src=C:\CAD-Test\Bentley\V8i-SS3\Workspace\Projects\ISO-requirements

REM Set the location on the new files
Set Dstn=C:\CAD-Test\Bentley\V8i-SS3\Workspace\Projects\

REM Set new filename from Existing

for /f %%x IN ('set') do call :EditFile

:EditFile

@echo off > newfile & setLocal enableDELAYedexpansion
Set old=MS_CELL
Set new=#MS_CELL

for /f "tokens=* delims= " %%a in (2000.pcf) do (
set str=%%a
echo %%str
set str=!str:%old%=%new%!
>> %Dstn%2000.pcf echo !str!
)


See More: How can I edit text in multple files

Report •


#1
August 26, 2014 at 09:01:44

Report •

#2
August 26, 2014 at 09:10:38
Thanks ijack but I can't really utilise a third party utility, I'd hoped to retain control in the script directly. I'm sure there must be a fairly simple solution to edit out he single specific file names and pick up the file being processed.

Report •

#3
August 26, 2014 at 10:12:25
✔ Best Answer
::====== script starts here ===============
::
:: MarkC.bat 2014-08-26 23:55:56.31
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN


copy x\*.pcf > nul
Set old=MS_CELL
Set new=#MS_CELL

:main
for /f "tokens=* delims= " %%a in ('dir/b *.pcf') do (
call :sub1 %%a
move/y NEWFILE %%a
)
goto :eof

:sub1
for /f "tokens=* delims= " %%i in (%1) do (
set S=%%i
set S=!S:%old%=%new%!
echo.!S!
) >> NEWFILE
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

Related Solutions

#4
August 27, 2014 at 01:12:16
Thanks Mechanix2Go, this works a treat, any chance you can explain what each section is doing?

I'm guessing that each file is being read into the NEWFILE and then written back to the original?

Cheers,

Mark.

message edited by MarkCouch


Report •

#5
August 27, 2014 at 03:22:12
Hi Mark,

Yep, you guessed it.

The :main CALLs the sub with each filename.
:sub1 does the string alteration and writes to NEWFILE.

=====================
M2 Golden-Triangle


Report •

#6
August 27, 2014 at 03:29:57
Thanks for your help Mechanix2Go really appreciated!

With some tinkering and trial and error I have developed your solution a little further for my needs, probably could have been achieved a bit more elegantly but it is working so if it's not broken why fix it? - Here is what I've done:

::====== script starts here ===============
::
:: MarkC.bat 2014-08-27 11:22
::
:: Selects all the files in the processed directory one by one
:: and applies each of the sub routines by generating temporary
:: newfile(s). Copying the original text and replacing with
:: the selected edits. Seperate subs target specific edits, each
:: one requires it's own temporary holding file before the original
:: text file is over written.

copy x\*.pcf > nul
Set old=MS_CELL
Set new=#MS_CELL

Set old1=MS_DGN
Set new1=#MS_DGN

Set old2=MS_VBA
Set new2=#MS_VBA

:main
for /f "tokens=* delims= " %%a in ('dir/b *.pcf') do (
call :sub1 %%a
move/y NEWFILE %%a
call :sub2 %%a
move/y NEWFILE1 %%a
call :sub3 %%a
move/y NEWFILE2 %%a
)

goto :eof

:sub1
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims= " %%i in (%1) do (
set S=%%i
set S=!S:%old%=%new%!
echo.!S!
) >> NEWFILE

:sub2
@echo off > NEWFILE1 & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims= " %%j in (%1) do (
set T=%%j
set T=!T:%old1%=%new1%!
echo.!T!
) >> NEWFILE1

:sub3
@echo off > NEWFILE2 & setLocal enableDELAYedeXpansioN
for /f "tokens=* delims= " %%k in (%1) do (
set U=%%k
set U=!U:%old2%=%new2%!
echo.!U!
) >> NEWFILE2

goto :eof

::====== script ends here =================

message edited by MarkCouch


Report •

Ask Question