0

I'm trying to make it so when the script is first ran it checks if there is a file, if there isn't then this is the first time the script has been run and it should run some code else it is been run more then once and should execute other code. Every time I run the file cmd crashes.

This is my file

@echo off

cd src/ops

IF EXIST setupdone.txt (
    set /p setting=Do you want to run the server or change your settings?:

    echo "1 - Run Server"
    echo "2 - Change Settings"

    IF %setting% == "1" (
        echo it is 1
    ) ELSE (
        IF %setting% == "2" (
            echo it is 2
        ) ELSE (
            echo Invalid Input! Rerun this file to try again.
        )
    )
) ELSE (
    cd src/server

    echo Installing Modules...

    call npm install

    cd ../settings

    call npm install

    cd %~dp0

    cd src/ops

    echo setupdone>setupdone.txt

    cd %~dp0
)

echo done

pause
Haloxx
  • 35
  • 4

1 Answers1

2

The solution to the main issue with your submitted code is probably the most common throughout the pages under the tag. Whenever a variable is defined or modified within a parenthesized block, in order for it to be used with its new value, delayed variable expansion needs to be enabled.

However, you had no need to do it using a variable, you simply needed not to use Set with its /P option. The choice.exe utility is provided for exactly this type of task.

Example:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

CD /D "%~dp0."

If Exist "src\ops\setupdone.txt" (
    Echo 1. Run Server
    Echo 2. Change Settings
    "%SystemRoot%\System32\choice.exe" /C 12 /M "Do you want to run the server, or change your settings"
    If Not ErrorLevel 2 (Echo It is 1.) Else Echo It is 2.
) Else (
    PushD "src\server" 2>NUL && (
        Echo Installing Modules...
        Call "npm.cmd" install
        PopD
    )
    PushD "src\settings" 2>NUL && (
        Call "npm.cmd" install
        1>"..\ops\setupdone.txt" Echo setupdone
        PopD
    )
)

Echo Done. 
Pause
Compo
  • 36,585
  • 5
  • 27
  • 39