I have a batch program that executes commands from distinct libraries. The problem is that those libraries finish console automatically when they're executed.
It seems that pause command doesn't work because probably those libraries will have they're own exit command. I tried with the command cmd /k that I found on google, but it doesn't works too.
:start
cls
echo.
echo 1) Desc 1
echo 2) Desc 2
set /p option=Enter an option:
IF "%option%"=="1" (
rem this is an example of library that exit console after being executed
pm2 start ecosystem.config.js --env development
)
IF "%option%"=="2" (
pm2 monit
)
pause
goto start
The main idea is if there's any method or param to avoid closing the console with those kind of libraries without editing the proper libraries.
Using the command call before the methods will work:
:start
cls
echo.
echo 1) Desc 1
echo 2) Desc 2
set /p option=Enter an option:
IF "%option%"=="1" (
rem this is an example of library that exit console after being executed
call pm2 start ecosystem.config.js --env development
)
IF "%option%"=="2" (
call pm2 monit
)
pause
goto start
Related
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
So i have this simple homework of doing a batch file that shows 3 options, and an error message for the entry for the option. so far it seems simple for me. I saw a youtube video and according to what i know it suppose to run ok but the problem is that when I run it and choose option 2 it opens also option 1, and when I choose option 3 it doesn't exit the program. And when I choose a parameter that is not specify the error message do not show. I am gonna copy paste my batch file because is easy to understand.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p Op=Write the option:
if %op%==1 (
start %windir%\system32\cleanmgr.exe
)
else if %op%==2 (
start %windir%\system32\dfrgui.exe
)
else if %op%==3 (
exit
)
else (
cls
echo Error not defined
)
pause
Is there something that I am missing or wrote wrong please let me know. If you can copy paste and run it in your computer and tell me if it is working fine because something tell me that in the process of learning I try a few times and i think I messed up with something that suppose to run correctly the file since my instructor told me that we have to be careful when using the commands in a batch file.
Your code formatted correctly but is susceptible to command injection because of the usage of the SET /P command.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p Op=Write the option:
if "%op%"=="1" (
start "" "%windir%\system32\cleanmgr.exe"
) else if "%op%"=="2" (
start "" "%windir%\system32\dfrgui.exe"
) else if "%op%"=="3" (
exit /b
) else (
cls
echo Error not defined
pause
)
A best practice solution which handles invalid input options.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk cleanup
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
choice /C 123 /N /M "Select an Option:"
set "op=%errorlevel%"
if "%op%"=="1" start "" "%windir%\system32\cleanmgr.exe"
if "%op%"=="2" start "" "%windir%\system32\dfrgui.exe"
if "%op%"=="3" exit /b
I did it with this code.
#echo off
echo Choose an option:
echo.
echo 1)Open Disk Cleanup Utility
echo 2)Open Disk Defragmenter
echo 3)Exit
echo.
Set /p op/3=Write the number option:
if %op/3%==1 (
start %windir%\system32\cleanmgr.exe
)
if %op/3%==2 (
start %windir%\system32\dfrgui.exe
)
if %op/3%==3 (
exit
)
else (
cls
echo Error not defined
)
pause
the only problem that I have is that the error message appears even though options 1 and 2 are selected, it executes the options but it also shows the error message. I only want that the error message to show if any of those parameters aren't chosen.
I know that else is responding because if I choose option 1 else it executes because is not number 2 or 3. What it comes to my mind is if it is possible to define the op variable in an enumeration format like a group set of options.
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
I am having trouble with my code. If I run the file and follow the prompt, enter a workstation name then select site 1, it continues to open up the cmd window to execute the psexec command, it does not do the "IF NOT EXIST" for site 1, but for other sites it comes back fine. If no file exists it will output to the prompt, if it does it continues to psexec. Any idea what I am doing wrong? Thank you.
:MAN
SET /P S=Please enter a workstation name or IP:
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Site you want to deploy from:
IF %D%==1 SET D="\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET D="\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET D="\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET D="\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %D% GOTO MSG
START CMD /K PsExec.exe #%S% -s -h cmd /c msiexec.exe /i "%D%" /qn
PAUSE
GOTO EOF
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
Note the possibility of doing:
goto label%D%
You can also almost do:
set D=\\10.%D%.2.1\Operations\Sofware\Packages\quest.msi
instead of the 4 IF-statements.
Here is my working code, there was an issue with PsExec as well not taking the %s% variable, I instead outputted the user input to a text file and had Psexec read from that file. I also edited with everyone's suggestions. Thank you all.
:MAN
#ECHO OFF
SET /P S=Please enter a workstation name or IP:
ECHO %S% >> man.txt
ECHO.
ECHO 1 - Site 1
ECHO 2 - Site 2
ECHO 3 - Site 3
ECHO 4 - Site 4
SET /P D=Select which Branch you want to deploy from:
IF %D%==1 SET "R=\\site1\Operations\Sofware\Packages\file.msi"
IF %D%==2 SET "R=\\site2\Operations\Sofware\Packages\file.msi"
IF %D%==3 SET "R=\\site3\Operations\Sofware\Packages\file.msi"
IF %D%==4 SET "R=\\site4\Operations\Sofware\Packages\file.msi"
IF NOT EXIST %R% GOTO MSG
START CMD /k "PsExec.exe #man.txt -s -h cmd /c msiexec.exe /i %R% /qn & DEL man.txt"
PAUSE
GOTO MENU
:MSG
CALL :color 1a "ERROR: MSI PACKAGE DOES NOT EXIST"
== is not advised in a .bat, .cmd file; I'd use EQU
fun stuff!!
Manager has just bought a humdinger of a utility. It does everything. It even has command line functionality. If you run BugFixer /a the application will automatically scan the entire drive for cooties. If you run the Bugfixer /b it will scan all of your files on the Windows\system 32 directory and if you run Bugfixer /c the program will scan and repair your Registry. Need to write a batch file that will allow your users to avoid that pesky GUI and efficiently run the Bug fixer through the command line, by asking them which option they would like to initialize.
:TOP
ECHO WHICH BUG FIXER DO YOU NEED TO RUN? A=ALL B=SOME c=REPAIR
SET /P = %USERSPEC%
IF "%1"=="A=ALL" GO TO :FIRST
IF "%1"=="A=all" GO TO :FIRST
:FIRST
CHKDSK C:
ECHO CHECKING ALL FILES ARE COMPLETE
IF "%2"=="B=SOME" GO TO :NEXT
IF "%2"=="b=some" GO TO :NEXT
:NEXT
CHKDSK /F /R C:\WINDOWS/SYSTEM32
ECHO CHECKING SOME FILES ARE COMPLETE
IF "%3"=="REPAIR" GO TO :LAST
IF "%3"=="repair" GO TO :LAST
:LAST
CHKDSK /c
ECHO REPAIR FILES ARE COMPLETED
Like I said fun stuff. Anyone wanna help?
Next script logic could become helpful:
#echo OFF >NUL
setlocal enableextensions
:TOP
echo(
set "USERSPEC="
set /P "USERSPEC=Which bug fixer do you need to run? A=all B=some C=repair? "
if /I "%USERSPEC%"=="A" goto :FIRST
if /I "%USERSPEC%"=="B" goto :NEXT
if /I "%USERSPEC%"=="C" goto :LAST
echo NO CHECK CHOOSEN, BATCH ENDS
goto :ENDSCRIPT
:FIRST
chkdsk C:
echo CHECKING ALL FILES ARE COMPLETE
goto :TOP
:NEXT
chkdsk /F /R C:\WINDOWS/SYSTEM32
echo CHECKING SOME FILES ARE COMPLETE
goto :TOP
:LAST
chkdsk /c
echo REPAIR FILES ARE COMPLETED
goto :TOP
:ENDSCRIPT
endlocal
goto :eof
However, running it:
==>30019117.bat
Which bug fixer do you need to run? A=all B=some C=repair? a
Access Denied as you do not have sufficient privileges.
You have to invoke this utility running in elevated mode.
CHECKING ALL FILES ARE COMPLETE
Which bug fixer do you need to run? A=all B=some C=repair?
NO CHECK CHOOSEN, BATCH ENDS
==>
Resources (advised reading):
An A-Z Index of the Windows CMD command line (command reference)
Windows CMD Shell Command Line Syntax (additional particularities)
Script resources for IT professionals (a huge Script repository)