My batch file is instantly closing when run [duplicate] - batch-file

This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed last month.
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

The solution to the main issue with your submitted code is probably the most common throughout the pages under the batch-file 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

Related

Need a little tip on batch files

I'm very new to coding and iI'm having a problem that is probably trivial, but is making me pull out my hair.
I'm using a batch script to automate mounting a VHD, executing a file inside and then pause until the user presses any key, which makes the VHD get unmounted and the script exits.
This is the main batch file:
#echo off
set fileVHD=Gord
CD /D "%~dp0"
powershell -command "Start-Process mount.cmd '%~dp0%fileVHD%.vhd' -Verb runas"
timeout /t 1
for /f %%D in ('wmic volume get DriveLetter^, Label ^| find "%fileVHD%"') do set usb=%%D
CD /D %usb%
index.html
echo "!!!!!!!!!!!!!!!!!!!!Press any key to fully close this program.!!!!!!!!!!!!!!!!!!!!!!!!!"
pause
CD /D "%~dp0"
powershell -command "Start-Process unmount.cmd '%~dp0%fileVHD%.vhd' -Verb runas"
exit
This is the mount script (Not made by me):
#echo off
setlocal enabledelayedexpansion
if "%~1"=="" (
echo Usage: %~nx0 [vhd] [letter]
exit /b 1
)
set "vhdPath=%~dpnx1"
set "driveLetter=%2"
if "!driveLetter!"=="" (
echo Mounting "!vhdPath!"
) else (
echo Mounting "!vhdPath!" to "!driveLetter!":
)
REM
REM create diskpart script
REM
set "diskPartScript=%~nx0.diskpart"
echo select vdisk file="!vhdPath!">"!diskPartScript!"
echo attach vdisk>>"!diskPartScript!"
REM assign the drive letter if requested
if not "!driveLetter!"=="" (
echo select partition 1 >>"!diskPartScript!"
echo assign letter="!driveLetter!">>"!diskPartScript!"
)
REM Show script
echo.
echo Running diskpart script:
type "!diskPartScript!"
REM
REM diskpart
REM
diskpart /s "!diskPartScript!"
del /q "!diskPartScript!"
echo Done!
endlocal
When all the files are located in a system path that contains no spaces, everything works fine. But it breaks where there are spaces.
That means that somewhere in the code a path is badly defined by the lack of quotes, probably in the mount script. The trouble is that i don't fully grasp the mount script when it starts using all the "%~...." variable path names.
I had to mix in some powershell commands because for some reason the script wouldn't work unless executed as Administrator.
If someone could give some insight to a newbie, it would be greatly appreciated.
You need end quotes around your parameters when you change directory, i.e.
CD /D "%~dp0"
You can also see all of the %~ options by running 'help for' in a console window. In those scripts it's getting the path or filename from a variable.
Discovered the root of my problem.
The path from script 1 was not being passed faithfully to script 2, even using using quotes or multiquotes.
Thanks for all the input guys!

Command prompt "forgets" command after first use

I can't figure out why the command prompt suddenly forgets certain commands after first use.
I have created a folder on my computer to store my custom commands to make my life easier. I have added this folder to my PATH environment variable so I can quickly access my commands.
For example:
Running ipconfig twice or more results in the the command working as expected every time.
Running the alias command (code included below) twice or more works as expected.
Running my deletefolder command twice only works first time. Second time it says 'deletefolder' is not recognized as an internal or external command, operable program or batch file. Now the alias command does not work anymore either (not recognized), but ipconfig still works. To get my commands to work again, I have to restart the command prompt.
I have tried to search for the reason behind this, but have not found any answer to this behavior. It feels like it's something simple but I really can't find out what. I have tried to be as thorough as I can, please let me know if I should clarify anything.
deletefolder batch file:
#echo off
set path=%1
IF DEFINED path (
GOTO run
) ELSE (
GOTO help
)
:help
echo. Usage:
echo. deletefolder [path]
exit /B
:run
CHOICE /C YN /M "Are you sure?"
IF ERRORLEVEL 1 (
:del /f/s/q %path% > nul
:rmdir /s/q %path%
)
exit /B
alias command (author: Benjamin Eidelman, beneidel#gmail.com)
#echo off
set operation=%1
set aliasname=%2
set aliasfile=%~dp0%2.cmd
IF "%~1"=="" GOTO help
IF /I "%~1"=="list" GOTO listaliases
IF /I "%~1"=="set" GOTO setalias
IF /I "%~1"=="get" GOTO getalias
IF /I "%~1"=="delete" GOTO deletealias
IF /I "%~1"=="here" GOTO setaliashere
:help
echo. Usage:
echo. alias list - list available cmd aliases
echo. alias set [name] [command line] - set an alias
echo. alias get [name] - show an alias
echo. alias delete [name] - delete alias
echo. alias here [name] [command line] - create alias cmd on cwd
exit /B
:listaliases
dir /B %~dp0*.cmd
exit /B
:setaliashere
set aliasfile=%2.cmd
:setalias
if "%aliasname%"=="alias" (
echo ERROR: cannot set this alias
exit /B 1
)
echo %1 %2> "%aliasfile%"
for %%a in ("%aliasfile%") do set /a length=%%~za
set /a length=length-1
set commandline=%*
setlocal enableDelayedExpansion
call set commandline=!commandline:~%length%!
set commandline=%commandline% %%*
echo %commandline%> "%aliasfile%"
echo INFO: alias "%aliasname%" set
exit /B
:getalias
if exist %aliasfile% (
type %aliasfile%
) ELSE (
echo ERROR: alias not found
exit /B 1
)
exit /B
:deletealias
if /I "%aliasname%"=="alias" (
echo ERROR: cannot delete this alias
exit /B 1
)
if exist %aliasfile% (
del %aliasfile%
echo INFO: alias deleted
) ELSE (
echo INFO: alias not found
)
exit /B
The PATH environment variable has very special (and crucial) meaning:
The %PATH% environment variable contains a list of folders. When a
command is issued at the CMD prompt, the operating system will first
look for an executable file in the current folder, if not found it
will scan %PATH% to find it.
Do not change the PATH variable arbitrary, use another variable name, e.g. _path as follows:
set "_path=%1"
IF DEFINED _path (
GOTO run
) ELSE (
GOTO help
)
rem your script continues here

Use of "set /p myvariable=<myfile.txt" in batch file

I'm trying to set the contents of a text file (it contains a single line of text) to a variable. To do this, I'm utilizing "set /p myvariable=
SetLocal EnableExtensions EnableDelayedExpansion
Set RegExist=No
Set RegUnins=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
Reg Query "%RegUnins%\{########-####-####-####-############}"
If "%errorlevel%"=="0" Set RegExist=Yes
If "%regexist%"=="Yes" (
Reg Query "%RegUnins%\{########-####-####-####-############}" /V "DisplayName" >"%temp%\appnam1.txt"
Find /I "This is the value for 'DisplayName'" "%temp%\appnam1.txt" >"%temp%\appnam2.txt"
More +2 "%temp%\appnam2.txt" >"%temp%\appnam3.txt"
Set /P _appnam=<"%temp%\appnam3.txt"
Set appnam=%_appnam:~24,53%
If "%appnam%"=="This is the value for 'DisplayName'" (
Echo - Current version installed: Performing reinstall...
Start "Reinstall" /B /High /Wait "\\server\installs\reinstall.exe"
) Else (
Echo - Previous version installed: Performing upgrade...
Start "Upgrade" /B /High /Wait "\\server\installs\upgrade.exe"
)
) Else (
Echo - Existing version not found: Performing new install...
Start "Install" /B /High /Wait "\\server\installs\new.install.exe"
)
However, even though the "%temp%\appnam3.txt" file contains the correct text, when I check the variables "%_appnam%" and "%appnam%" at those points in the code, they are blank. Therefore, if the machine does have the initial registry entry, it will always perform the upgrade process instead of the reinstall process. Any suggestions? I don't understand why the "set /p" line isn't working. Thanks in advance.
PLease use delayed expansion.
I tried to reproduce your case below. It seems the variable are set delayed, If you'll try to run you script several times, the values will be set.
#echo off
setlocal enableDelayedExpansion
set regexist=Yes
If "%regexist%"=="Yes" (
Set /P _appnam=<"C:\result.csv"
echo !_appnam!
Set appnam=!_appnam:~24,53!
echo !appnam!
)

Problems with BAT file: IF/ELSE not working as expected

I'm trying to make a short BAT file and I'm having trouble with one of its functions. I've tried a number of different ways to do this and none of them seem to work, but being a beginner at this I can't figure out the problem. Basically, the script, as it runs, is supposed to check if a certain .BAT file exists, and if it does, the script asks if the user wants to run it. If the user indicates Y, the other BAT is called and then the original script proceeds. If the user indicates N, the script is supposed to proceed without calling the other BAT. So far the script always notices and asks about the file, but choosing Y at the prompt never works. I'm sure the solution is obvious, but it's escaping me. Here's the code:
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
The problem is your variables are being evaluated before they enter the if's, which means cmd won't see any changes until they have ended.
This is causing problems for you as your variables kmpreproc and, depending on the first if result, kmname change within the if blocks.
The fix (presuming the rest of your code is working) is to enable delayed expansion and use delayed expansion instead of normal expansion, by changing the %'s to ! on your variables.
setlocal enabledelayedexpansion
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\!kmname!\!kmname!.flt (
ECHO !kmname!.flt found, will now create !kmname!.ive.
CD C:\Visual\area\!kmname!\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "!kmpreproc!" EQU "Y" (
GOTO PREPROC
) ELSE (
GOTO CONTINUE
)
)
GOTO CONTINUE
) ELSE (
ECHO C:\Visual\area\!kmname!\!kmname!.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
)
:PREPROC
ECHO Running Preprocess.bat.
:CONTINUE
ECHO Continuing process.
PAUSE
Here is below my corrected and tided up code :
#echo off
SET /P kmname=Enter database name:
:kmstart
IF EXIST C:\Visual\area\%kmname%\%kmname%.flt (
ECHO %kmname%.flt found, will now create %kmname%.ive.
CD C:\Visual\area\%kmname%\
IF EXIST Preprocess.bat (
SET /P kmpreproc=Found Preprocess.bat. Do you want to run it now?
IF /I "%kmpreproc%"=="Y" (
GOTO PREPROC
)
GOTO CONTINUE
)
)
ECHO C:\Visual\area\%kmname%\%kmname%.flt does not exist. Try again.
SET /P kmname=Enter database name:
GOTO kmstart
:PREPROC
ECHO Running Preprocess.bat.
call "cmd /c start Preprocess.bat"
pause
:CONTINUE
ECHO Continuing process.
PAUSE
If it doesn't work try writting setlocal EnableDelayedExpansion under #echo off and replacing all % with ! .

2 consecutive for loops will not run

Why is batch not running this second for loop? What am i doing wrong? The first loop runs fine, but the second loop is never hit. I even put an echo statement after the first loop and it is not even displayed.
FOR /D %%X in (..\Apps\Mine\*) do if exist "%%X\AndroidManifest.xml" ("%1\android.bat" update project -p "%%X") else (
echo This is not an android project.
)
FOR /D %%Y in (..\Apps\Theirs\*) do if exist "%%Y\AndroidManifest.xml" ("%1\android.bat" update project -p "%%Y") else (
echo This is not an android project.
)
More details
The current working directory also has no () or spaces in the name.
Windows7 64bit.
This is the exact argument i am using:
> update_project.bat C:\Users\MyUserName\android-sdks\tools
That is all the contents of the batch script. There is nothing else in it.
Here is the directory structure. The batch script is run from the CWD.
Projects
Apps
Mine
App1
App2
Theirs
App3
App4
Tools (CWD)
Add a CALL statement in front of "%1\android.bat". If you don't use CALL, control will not be returned from "%1\android.bat".
Like this,
FOR /D %%X in (..\Apps\Mine\*) do if exist "%%X\AndroidManifest.xml" (CALL "%1\android.bat" update project -p "%%X") else (
echo This is not an android project.
)
FOR /D %%Y in (..\Apps\Theirs\*) do if exist "%%Y\AndroidManifest.xml" (CALL "%1\android.bat" update project -p "%%Y") else (
echo This is not an android project.
)

Resources