View unanswered posts | View active topics It is currently 20 Apr 2014 14:56



Post new topic Reply to topic  [ 6 posts ] 
processing string from file 
Author Message

Joined: 18 Sep 2011 23:35
Posts: 13
Post processing string from file
I have been working (per request of a challenge) on a batch script to process a text file opened with it, and eventually want to renotate the .txt into batch

However, simply prep-ing the file for processing is throwing problems

I have two files comp.bat and text.txt

code.bat
Code:
@echo off
set /p inputFile=<%1
echo %inputFile%
for /f "tokens=1 delims=;" %%a in ("%inputFile%") do set a=%%a;&set inputFile=%inputFile:%%a%%="%
echo %inputFile%
echo %a%
pause


text.txt
Code:
print ("Hello World!");this is pointless code;


The current %'s has gotten the closest to what I want (to break the string into two, putting part 1 into variable a, and leaving the rest in inputFile for later processing), but instead, inputFile becomes %a%, when I want to remove that from the string, not make it equal it


28 Feb 2012 22:11
Profile

Joined: 10 Feb 2012 02:20
Posts: 3656
Post Re: processing string from file
Does this do what you want?

Code:
@echo off
set /p inputFile=<%1
echo %inputFile%
for /f "tokens=1,* delims=;" %%a in ("%inputFile%") do (
set "a=%%a;"
set "b=%%b"
)
echo %b%
echo %a%
pause


28 Feb 2012 22:32
Profile

Joined: 18 Sep 2011 23:35
Posts: 13
Post Re: processing string from file
Yes! Thank you!

However, why did you put a=%%a; in quotes?
Is there reason for this syntax?
(for is the my least understood of cmd commands, so link to reference isn't discouraged . . .)


28 Feb 2012 22:39
Profile

Joined: 10 Feb 2012 02:20
Posts: 3656
Post Re: processing string from file
The outside quotes allow you to include poison characters in a variable, and I do it by habit now as it doesn't include the quotes in the variable itself.

In your case they aren't strictly necessary.


28 Feb 2012 22:44
Profile

Joined: 18 Sep 2011 23:35
Posts: 13
Post Re: processing string from file
One last tangential question:
How do you make "for" repeat until there are no more delimiters, ie.
Code:
for /f "tokens=1, *. delims=;" %%a in ("%inputFile%") do (
...
)


when *. is the repeating anychar (to cycle until no delimiters left)


28 Feb 2012 23:17
Profile

Joined: 10 Feb 2012 02:20
Posts: 3656
Post Re: processing string from file
Captcha142 wrote:
How do you make "for" repeat until there are no more delimiters,



It depends. If the number of tokens is known then you would use something like one of these.

"tokens=1-10 delims=;"
"tokens=1,2,3,4,5,6,7,8,9,10 delims=;"
"tokens=1,5,7 delims=;"

Another way is to parse the entire line character by character.

The exact makeup of the text can help to decide which is the easiest method.


29 Feb 2012 02:41
Profile
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], penpen, Yahoo [Bot] and 10 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Forum style by Vjacheslav Trushkin for Free Forums/DivisionCore.