Solved What is wrong with this for loop?

August 7, 2013 at 17:36:55
Specs: Windows 7
I am making a small game in batch, I am planning it to be able to load and save game progress. The only thing I need now is the actual thing that reads information about the save file, the only thing is: I don't know a THING about FOR loops. heres the part that does it (FOR loop marked with dashes)
:menu.load
Set fileTOload=0
cls
Echo To load a profile, write the name of the profile and press enter.
Echo.
Echo Available profiles:
Echo (Ignore .wssav in the end of each profile)
Echo.
dir /b Saves
Echo.
set /p FTL=
Set fileTOload=%FTL%.wssav
goto menu.loadsave
:menu.loadsave
if fileTOload==0 (
goto menu.load
)
if NOT EXIST Saves\%FTL%.wssav (
Echo This profile does not exist...
timeout /t 2 > nul
goto menu.load
)
- - for /f "tokens=2 delims=:" in "Saves\%fileTOload%.wssav" do ( - -
Set %%A
Set loadVariable=%%A
)

Help is appreciated highly :)

message edited by RainBawZ


See More: What is wrong with this for loop?

Report •


#1
August 7, 2013 at 19:45:40
✔ Best Answer
- - for /f "tokens=2 delims=:" in "Saves\%fileTOload%.wssav" do ( - -
Set %%A
Set loadVariable=%%A
)

for /f "tokens=2 delims=:" %%A in (Saves\%fileTOload%.wssav) do (
Set loadVariable=%%A
)

This won't be much use, however, since it only loads the last line in the "save" file into variable "loadvariable".
I think you would want something more like:
for /f "tokens=1.2 delims=:" %%A in (Saves\%fileTOload%.wssav) do set %%A=%%B
echo health is %health%
echo wealth is %wealth%
...
Assuming you have saved your data something like:
health:100
wealth:400
power:150

My suggestion is to tag your var.s so you can easily display them using SET:
~health:100

then:
SET ~
to see all the tilde-tagged vars and their values.


Report •

#2
August 8, 2013 at 02:31:52
I tried out your FOR loop, I got this error: .2 delims=:" was not expected

Since I don't know anything about FOR loops, I don't know whats wrong...


Report •

#3
August 8, 2013 at 05:18:42
If the "1.2" doesn't work, go for "1,2" or "1-2".

Still, I would need to see the input file before I can add anything resembling valid input.

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 8, 2013 at 06:23:57
A save file looks like this:
userName:newProfile
saveDate:06.08.2013-21:35:52,70
savedLabel:game.wakeup
xp:0
level:0

the most important parts are saveLabel, xp and level

Also tried the suggestions for the FOR loop, it turns out that using a comma works.

But since there are five lines to be loaded, should I replace the 2 in 1,2 with 5?


Report •

#5
August 8, 2013 at 06:26:12
Suggested reading:
1) Open a Command Prompt.
2) Type:
for /?

How To Ask Questions The Smart Way


Report •

#6
August 8, 2013 at 07:39:35
It's badly translated in the norwegian version

Report •

#7
August 8, 2013 at 08:38:11
Does not surprise me, but it does make me wonder why you're not picking an easier, more widely supported language. Here's a version that isn't translated: http://technet.microsoft.com/en-us/...

How To Ask Questions The Smart Way


Report •

#8
August 8, 2013 at 18:10:27
Yeh, sorry about the typo ("1.2" vs "1,2"). I got distracted trying to log back in to the forum, and lost my original reply. Thanks, Razor for taking up the slack.
I'll amend the line to:
for /f "tokens=1,2* delims=:" %%A in (Saves\%fileTOload%.wssav) do set %%A=%%B
This way, you won't lose data that has colons in it.
If you intend to pursue this using batch, you will need a good reference (in Norwegian) since most scripters use Razor's suggestion in resp #5.
Starting from scratch, the learning curve approaches that of the kuiper belt without a decent reference to help you.

Report •


Ask Question