Solved batch script reading lines from a text file

March 18, 2014 at 21:26:20
Specs: windows7
While reading text file and editing the content , I am facing a problem. If the data in line has brackets , my program is termination at that point. Is there a fix for this ? This is the script

::--------------------------------------------------------------------------------------
@echo off > Prefix_added.txt & setLocal enableDELAYedeXpansioN
:main
for /f "tokens=* delims=" %%a in (%1) do (
call :sub1 %%a
if !T!==111 (echo.111/%%a) else (echo.%%a)
) >> Prefix_added.txt
goto :eof
:sub1
for /f "tokens=* delims= " %%i in ('echo.%*') do (
set S=%%i
set T=!S:~0,3!
)
goto :eof
::------------------------------------------------------------

This is my input text file format

*Script_Start
111
Name
Date
Sex (Male or Female)
Age
111
*Script_End


See More: batch script reading lines from a text file

Report •


✔ Best Answer
March 20, 2014 at 23:13:19
Ok, so what you're really looking for is pattern 012_abcdef.TXT (ie: 3 digits, an underscore, then indefinite content until .txt extension). I don't think the () are really a problem, as these will be echoed correctly (at least, I have no problems on my xp). So what you really want is to take the 1st 3 digits of pattern and make into a path. Right?
maybe this (again, using findstr to get the pattern, although batch could do it in a stretch):
@echo off & setlocal
for /f "tokens=1* delims=_" %%a in (%1) do (
echo %%a_%%b | findstr /i /r "^[0-9][0-9][0-9]_.*\.txt">nul&&echo %%a\%%a_%%b||if "%%b" neq "" (echo %%a_%%b) else (echo %%a)
)

message edited by nbrane



#1
March 19, 2014 at 02:20:55
You can't use "(" or ")" inside e.g. (echo...) so I suggest you change in your text file ( ) into [ ] (i.e. square brackets). That is the simplest and straightforward fix.

Report •

#2
March 19, 2014 at 08:32:55
I don't grok the need for a :sub, but at my age plenty gets past me.

::====== script starts here ===============
::
:: rohan.bat 2014-03-19 22:09:53.19
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=* delims= " %%a in (myfile) do (
set S=%%a
set T=!S:~0,3!
if !T!==111 (echo.111/%%a) else (echo.%%a)
) >> NEWFILE
goto :eof
::====== script ends here =================

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


Report •

#3
March 19, 2014 at 09:47:25
it is not possible to remove brackets from input text file.. :-(
Is there any alternative to consider brackets ( ) as bracket itself .

Report •

Related Solutions

#4
March 20, 2014 at 03:42:17
The following batch does what you want, but be aware you must avoid any bracket inside brackets on the same line of any statement of your script and that may lead to cumbersome code. Better you use VBScript or something else since batch is strongly limited in handling special characters.

@echo off  > Prefix_added.txt
:MAIN
for /F "tokens=1,*" %%a in (%1) do (
  if "%%a"=="111" (
    echo.111/%%a %%b
  ) else (
    echo.%%a %%b
  )
) >> Prefix_added.txt

message edited by IVO


Report •

#5
March 20, 2014 at 21:45:56
I guess my question is, what are you ulitmately trying to accomplish, since this exercise seems rather pointless to me... (but then, I'm usually slow on the uptake). The previous couple of posts seemed to solve it regardless. Despite Findstr limitations, it could still be used maybe:
@echo off & setlocal
for /f "tokens=*" %%a in (%1) do (
for /f "tokens=*" %%b in ("%%a") do echo %%a | findstr /r "^111">nul&&echo 111/%%a||echo %%a
)

message edited by nbrane


Report •

#6
March 20, 2014 at 22:32:04
@nbrane .let me brief you abt the situation. I have an input main file which calls a list of other files.. All the files starts with a standard 3 digit number... like
*include
200_something.txt
340_another.txt
Now each of the files is kept inside folder with the name same as the first 3 digits..like folder 200 will have 200_something.txt
So in the main file I need to add folder path like 200/200_something.txt
Above scripts works fine in adding prefix. But I have some comments also written in the main file which is unavoidable...which gives history of the file like ... who did what on this main file ..on which date etc etc... These comments has Brackets which is also important.. basically I dont want to edit the comments..
Above script doesnt work if the comments has brackets.. I am looking for a fix for this..I need to add prefix 3 number to only lines that start with 3 digit number.

Report •

#7
March 20, 2014 at 23:13:19
✔ Best Answer
Ok, so what you're really looking for is pattern 012_abcdef.TXT (ie: 3 digits, an underscore, then indefinite content until .txt extension). I don't think the () are really a problem, as these will be echoed correctly (at least, I have no problems on my xp). So what you really want is to take the 1st 3 digits of pattern and make into a path. Right?
maybe this (again, using findstr to get the pattern, although batch could do it in a stretch):
@echo off & setlocal
for /f "tokens=1* delims=_" %%a in (%1) do (
echo %%a_%%b | findstr /i /r "^[0-9][0-9][0-9]_.*\.txt">nul&&echo %%a\%%a_%%b||if "%%b" neq "" (echo %%a_%%b) else (echo %%a)
)

message edited by nbrane


Report •


Ask Question