Skip to main content
deleted 37 characters in body
Source Link
T3RR0R
  • 546
  • 3
  • 7

Here's an example of the the logic involved in handling a first run initialization. Additional validation of data passed as arguments by user input strongly recommended.

@Echo off
Del "%TEMP%\%~n0_firstrun.dat" 2> nul
Set "var1="
Set "var2="
Set "var3="

Rem if file called with 3 arguments
If not "%~3" == "" (
    If not exist "%TEMP%\%~n0_firstrun.dat" (
        <nul set /p "=Initiallizing... "
        Rem assumes delayed expansion state is disabled
        Set "var1=%~1"
        Set "var2=%~2"
        Set "var3=%~3"
        >"%TEMP%\%~n0_firstrun.dat" (
            Setlocal EnableDelayedExpansion
                Echo Set "var1=!var1!"
                Echo Set "var2=!var2!"
                Echo Set "var3=!var3!"
            Endlocal
            Rem optional - use conditional operator to flag write permission / filename error to calling script
        ) || (
            Echo Failed - Permission or Filename error
            exit /b 1
        )
        Rem Optional - guard initialization file from accidental user deletion
        REM Attrib +R "%TEMP%\%~n0_firstrun.dat"
        echo - Initiallization Complete
    )
) Else If not exist "%TEMP%\%~n0_firstrun.dat" If not "%~1" == "" If "%~3" == "" (
    Echo Initiallization Failed - Insufficient arguments provided
    Exit /B 2
)
cls
Rem attempt to load the variables.
Rem flags error in the event user has deleted the initialization file 
(For /f "UsebackQ delims=" %%G in ("%TEMP%\%~n0_firstrun.dat")Do %%G) || (
    Rem user has deleted the initialization file
    Exit /b 2
)

Set Var
Pause

Here's an example of the the logic involved in handling a first run initialization. Additional validation of data passed as arguments by user input strongly recommended.

@Echo off
Del "%TEMP%\%~n0_firstrun.dat" 2> nul
Set "var1="
Set "var2="
Set "var3="

Rem if file called with 3 arguments
If not "%~3" == "" (
    If not exist "%TEMP%\%~n0_firstrun.dat" (
        <nul set /p "=Initiallizing... "
        Rem assumes delayed expansion state is disabled
        Set "var1=%~1"
        Set "var2=%~2"
        Set "var3=%~3"
        >"%TEMP%\%~n0_firstrun.dat" (
            Setlocal EnableDelayedExpansion
                Echo Set "var1=!var1!"
                Echo Set "var2=!var2!"
                Echo Set "var3=!var3!"
            Endlocal
            Rem optional - use conditional operator to flag write permission / filename error to calling script
        ) || (
            Echo Failed - Permission or Filename error
            exit /b 1
        )
        Rem Optional - guard initialization file from accidental user deletion
        REM Attrib +R "%TEMP%\%~n0_firstrun.dat"
        echo - Initiallization Complete
    )
) Else If not exist "%TEMP%\%~n0_firstrun.dat" If not "%~1" == "" If "%~3" == "" (
    Echo Initiallization Failed - Insufficient arguments provided
    Exit /B 2
)
cls
Rem attempt to load the variables.
Rem flags error in the event user has deleted the initialization file 
(For /f "UsebackQ delims=" %%G in ("%TEMP%\%~n0_firstrun.dat")Do %%G) || (
    Rem user has deleted the initialization file
    Exit /b 2
)

Set Var
Pause

Here's an example of the the logic involved in handling a first run initialization. Additional validation of data passed as arguments by user input strongly recommended.

@Echo off

Set "var1="
Set "var2="
Set "var3="

Rem if file called with 3 arguments
If not "%~3" == "" (
    If not exist "%TEMP%\%~n0_firstrun.dat" (
        <nul set /p "=Initiallizing... "
        Rem assumes delayed expansion state is disabled
        Set "var1=%~1"
        Set "var2=%~2"
        Set "var3=%~3"
        >"%TEMP%\%~n0_firstrun.dat" (
            Setlocal EnableDelayedExpansion
                Echo Set "var1=!var1!"
                Echo Set "var2=!var2!"
                Echo Set "var3=!var3!"
            Endlocal
            Rem optional - use conditional operator to flag write permission / filename error to calling script
        ) || (
            Echo Failed - Permission or Filename error
            exit /b 1
        )
        Rem Optional - guard initialization file from accidental user deletion
        REM Attrib +R "%TEMP%\%~n0_firstrun.dat"
        echo - Initiallization Complete
    )
) Else If not exist "%TEMP%\%~n0_firstrun.dat" If not "%~1" == "" If "%~3" == "" (
    Echo Initiallization Failed - Insufficient arguments provided
    Exit /B 2
)
cls
Rem attempt to load the variables.
Rem flags error in the event user has deleted the initialization file 
(For /f "UsebackQ delims=" %%G in ("%TEMP%\%~n0_firstrun.dat")Do %%G) || (
    Rem user has deleted the initialization file
    Exit /b 2
)

Set Var
Pause
Source Link
T3RR0R
  • 546
  • 3
  • 7

Here's an example of the the logic involved in handling a first run initialization. Additional validation of data passed as arguments by user input strongly recommended.

@Echo off
Del "%TEMP%\%~n0_firstrun.dat" 2> nul
Set "var1="
Set "var2="
Set "var3="

Rem if file called with 3 arguments
If not "%~3" == "" (
    If not exist "%TEMP%\%~n0_firstrun.dat" (
        <nul set /p "=Initiallizing... "
        Rem assumes delayed expansion state is disabled
        Set "var1=%~1"
        Set "var2=%~2"
        Set "var3=%~3"
        >"%TEMP%\%~n0_firstrun.dat" (
            Setlocal EnableDelayedExpansion
                Echo Set "var1=!var1!"
                Echo Set "var2=!var2!"
                Echo Set "var3=!var3!"
            Endlocal
            Rem optional - use conditional operator to flag write permission / filename error to calling script
        ) || (
            Echo Failed - Permission or Filename error
            exit /b 1
        )
        Rem Optional - guard initialization file from accidental user deletion
        REM Attrib +R "%TEMP%\%~n0_firstrun.dat"
        echo - Initiallization Complete
    )
) Else If not exist "%TEMP%\%~n0_firstrun.dat" If not "%~1" == "" If "%~3" == "" (
    Echo Initiallization Failed - Insufficient arguments provided
    Exit /B 2
)
cls
Rem attempt to load the variables.
Rem flags error in the event user has deleted the initialization file 
(For /f "UsebackQ delims=" %%G in ("%TEMP%\%~n0_firstrun.dat")Do %%G) || (
    Rem user has deleted the initialization file
    Exit /b 2
)

Set Var
Pause