Solved How to replace text lines in txt file with bat file

July 23, 2014 at 07:43:40
Specs: Windows 7
I need to write a .bat file that searches a .txt file for instances of

"index 0" - "index 179"

and renames them to have a different order - ie

"index 15" becomes "index 0"
"index 75" becomes "index 60"
"index 160" becomes "index 145"
"index 0" becomes "index 165"

essentially subtracting 15 from all the numbers after 14 and then changing 0-14 to 165-179


See More: How to replace text lines in txt file with bat file

Report •


#1
July 23, 2014 at 19:25:56
You need to post a useful chunk of the file.

Some lines which have index ###, some which don't and blanks, if any.

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


Report •

#2
July 23, 2014 at 22:31:25
The file looks like this:

index 0
Team Ant
"Ant Power"
AI\GALS.bmp
Robots: 0 1 2 3 4 5
Robot In Event: -1
0
16
16
0
0
0
0
0
100000
true
0
false
0
-1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0
0
0
index 1
....
..
....
..

On through to index 179. It's a basic .txt file.

I have had some luck with a newer version of the batchsubstitute.bat that is out there
@ http://www.dostips.com/forum/viewtopic.php?f=3&t=1516, but have had some serious issues making the file write to a .txt.
I call the .bat 179 times. It works fine when just making it an echo to the console with the standard inputs
(batchsubstitute.bat "index 15" "index 0" AI\Teams.txt )
but once I set it to write to a .txt with
(batchsubstitute.bat "index 15" "index 0" AI\Teams.txt>AI\tempteam.txt ),
it hangs after the first call.

Might be better to look for the integer after "index" and just replace it perhaps or make an array? Any help would be appreciated.

message edited by thurston_studios


Report •

#3
July 24, 2014 at 01:00:47
✔ Best Answer
::====== script starts here ===============
:: decrement index by 15
:: sub15.bat 2014-07-24 13:24:17.60
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

for /f "tokens=1,2* delims= " %%a in (myfile) do (
set S=%%a %%b %%c
if "%%a" equ "index" (
set/a #=%%b-15
if !#! lss 0 set/a #+=180
set S=%%a !#! %%c
)
echo.!S!
) >> NEWFILE
goto :eof
::====== script ends here =================

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


Report •

Related Solutions


Ask Question