-1

I am developing a program in batch that will play the board game Talisman (second edition). It is in very early development and not ready for testing.

At one point in the code I need to print the top line of a text document. This part of the code looks like this:

find /I "alchemy" priorities.txt >nul
if %errorlevel%==0 (
set /p alchemObj=< 2Alchemise.txt
echo I will visit the alchemist in the city.
echo.
echo The Alchemist converted my %alchemObj% into 1 gold
set /a gold=%gold%+1
echo I now have %gold% gold.

for /f "skip=1 delims=*" %%a in (2Alchemise.txt) do (
echo %%a >>newfile.txt
)
xcopy newfile.txt 2Alchemise.txt /y >nul
del newfile.txt /f /q >nul

)

When this program is run everything except the

echo The Alchemist converted my %alchemObj% into 1 gold

seems to work. When it is run it prints "The Alchemist converted my into 1 gold"

I have tried running:

set /p alchemObj=< 2Alchemise.txt

and then

echo The Alchemist converted my %alchemObj% into 1 gold

and it seems to work just as expected.

Thanks for your help, Edje

(Click here to download whole file)

  • 4
    You have code between parentheses. You may need delayed expansion i.e. `!alchemObj!`. View `set /?` and `setlocal /?` for help information. To add 1 gold, you can also do `set /a gold+=1`. – michael_heath Oct 09 '18 at 01:47
  • Your other option would be to not create the code block and just skip over the code if the errorlevel is not zero. `IF NOT %errorlevel%==0 GOTO SKIP`. – Squashman Oct 09 '18 at 03:22
  • Even better would be `if errorlevel 1 goto SKIP` as this means if exit code of `find` is greater or equal 1, i.e. __FIND__ could not find the string. Run in command prompt window `if /?` for help. The advantage of `if errorlevel 1 goto SKIP` or the opposite `if not errorlevel 1 goto SKIP` meaning less than 1, i.e. equal 0 for `find` and nearly all other console applications, is that it works even in a command block without usage of delayed expansion. See also [Single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Oct 09 '18 at 05:04

1 Answers1

0

Thanks a lot to michael_heath for their answer.

I was using delayed expansion for an earlier part of the code. For what ever reason I needed to use the '!' variable deliminator instead of the default '%' deliminator. Thanks also to Squashman and Mofi as I used these suggestions to improve my code further.

Thanks a lot this have given me a great first impression of Stack Overflow.

Evyn