-2

I'm making a game in batch, but when i try to write to a file, cmd just crashes. I'm writing to a file to save data. So that the user can just come back and load it up.

Create User Code Block:

:cruser1
cls
set /a created=1
echo(
set /p name=Enter The Name of The User:: 
goto save

Load User Code Block:

:loaduser
cls
set /a created=0
echo(
set /p loaduser=Enter The Name of The User to Load:: 

if exist "%loaduser%.dll" (
    < %loaduser%.dll (
        set /p MHP=
        set /p HP=
        set /p DMG=
        set /p GOLD=
        set /p EXP=
        set /p LVL=
        set /p MEXP=
    )
    goto menu
)

if not exist "%loaduser%.dll" (
    echo File not found.
    timeout /t 3 /NOBREAK >nul
    goto loaduser
)

Save Data Code Block:

:save
cls
echo(
if %created% equ 1(
    (
        echo %MHP%
        echo %HP%
        echo %DMG%
        echo %GOLD%
        echo %EXP%
        echo %LVL%
        echo %MEXP%
    ) > %name%.dll
    attrib +h "%name%.dll"
    goto menu
)
if %created% equ 0(
    (
        echo %MHP%
        echo %HP%
        echo %DMG%
        echo %GOLD%
        echo %EXP%
        echo %LVL%
        echo %MEXP%
    ) > %loaduser%.dll
    attrib +h "%loaduser%.dll"
    goto menu
)

Does anyone know how to fix this and why it's happening?

Edit: I debugged it, after saving echoing it worked, after i killed the enemy, It did say it worked, but also Access is denied. Hopefully this helps.

  • 2
    Change `if %created% equ 1(` to `if %created% equ 1 (` (note the _space_). Anyway, please learn how to debug a batch file – see [this](https://stackoverflow.com/q/165938) and [this](https://www.tutorialspoint.com/batch_script/batch_script_debugging.htm) and [this](https://www.robvanderwoude.com/battech_debugging.php). Very important: open a Command Prompt window and run the batch file from there by typing in its path/name, so you can read potential error messages… – aschipfl Jun 09 '20 at 12:35
  • Thanks! That worked but in my code I forgot to mention that I have got that to work but it didn't actually save. It still doesn't save. – TextBasedYoutube Jun 09 '20 at 12:39
  • You appear to be requesting end user input of a name, which is subsequently used as the name of the `.dll` file holding the saved data. What happens if the end user provides a name which already exists? or a name which cannot be used as a legal filename in Windows? or contains spaces or other potentially problematic characters? – Compo Jun 09 '20 at 13:30
  • Also as this is already your second question today on parts the same script, perhaps it would make more sense if you were to provide a link to all of it, so that those who are interested in helping can provide solutions which suit the whole thing instead of performing a series of smaller, possibly conflicting or inefficient fixes. Additionally, when you receive advice in the comments, which your then tell us you've implemented, you need to update the code in your question, so that new readers are aware that the code they're seeing is pertinent to the remaining issue/issues. – Compo Jun 09 '20 at 13:31
  • @Compo [Here is the link to my current code](https://pastebin.com/V791hGs5). Also added that your not able to have 2 users with the same name and you cant name a user "con", "nul", "aux" or "..." – TextBasedYoutube Jun 09 '20 at 13:47
  • [Might I recommend a simpler method for saving and loading - can be used as a subroutine or companion batch program.](https://pastebin.com/5S3Ng6a6). With it, reloading or saving user data is as simple as calling the function with the described parameters. Simply prefix all 'permanent' user variables with `$.` to make use of the function. Bonus tip, Set /A can be used to define multiple variables with integer values on the same line Ie `Set /A var1=10,var2=20` – T3RR0R Jun 09 '20 at 14:19
  • You are executing your script with just doubleclicking it, don't you? Your working folder then is `c:\windows\system32`, where you don't have write permission (therefore `Access denied'). Insert a line `cd /d "%~dp0" right after @echo off` as a second line to set the working folder to the folder where your batchfile is stored. – Stephan Jun 09 '20 at 15:24
  • @Stephan I put ```cd /d "%~dp0"``` on the second line, but I still get the ```Access is denied``` message. – TextBasedYoutube Jun 09 '20 at 15:49
  • 1
    I must ask, did you change attrib -H before attempting the write action? Because if your attempting to write to the file while it's still set to hidden attribute, the write action will fail and access is denied error will be returned – T3RR0R Jun 09 '20 at 15:59
  • @T3RR0R Thanks! I just had to write ```if exist "%name%.dll" attrib -h "%name%.dll"``` before I saved the data! – TextBasedYoutube Jun 09 '20 at 16:14
  • feel free to mark my answer as accepted. – T3RR0R Jun 09 '20 at 17:17

1 Answers1

0

You have errant parentheses, Issue one. issue 2 failure to test existance for existing hidden file

If exist "%loaduser%.dll" attrib -h "%loaduser%.dll"
if %created% equ 0 (
    echo %MHP%
    echo %HP%
    echo %DMG%
    echo %GOLD%
    echo %EXP%
    echo %LVL%
    echo %MEXP%
) > %loaduser%.dll
attrib +h "%loaduser%.dll"
goto menu
T3RR0R
  • 2,747
  • 3
  • 10
  • 25