i have been trying to write a batch script in windows 7 sp1. After i start my batch script i try to type an option into the input and then i get "goto unexpected at this time" then the command prompt closes. Please help i need this for a personal project.
#echo off
:start
echo Is this the first time you ran this program on this computer
set input=
set /p intput= "y/n"
if%input%==yes goto Yes
if%input%==no goto No
:No
pause
echo Okay skipping the installation process
cd Minecraft_Server
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:serverrestart
echo Would you like to restart the server?
set input=
set /p intput= y or n
if%input%==y
pause
goto restart
if%input%==n
pause
goto norestart
:restart
echo To restart the server press any key.
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:norestart
exit
:Yes
pause
echo I will now install the Minecraft Serevr files, please make sure you
echo have me in the "Parent Directory" of the Minecraft_Server folder
echo --If you don't know what a parent directory is GOOGLE it!--
pause
mkdir Minecraft_Server
move files.exe Minecraft_Server
cd Minecraft_Server
start files.exe
timeout /t 3
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto :serverrestart
You misspelled input in the set /p line and you should use quotes. I used /i to make the compare case insensitive and shortened to one key required. Do as you wish with that. Try this:
#echo off
:start
echo Is this the first time you ran this program on this computer
set "input="
set /p input= "y/n"
if /i "%input%"=="y" goto :Yes
if /i "%input%"=="n" goto :No
echo.Enter a valid choice
pause
goto :start
Related
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
ping localhost -n 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date
cls
echo welcome to windows 71
echo before we bigen please type your name
set /P_name=here:
if %name%=admin goto admin
if not goto ms
rem ms=menu start
:ms
echo %time%
echo hello %name%
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
if not goto wc
rem wc=wrong command
:admin
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P_command=here:
if %command%=help goto help
if %command%=exit goto exit
if %command%=calendar goto cal
So the problem is that it crashes after the :LOGIN part and I don't know what to do!
I'm trying to make an OS batch (something like MS-DOS), but it crashes after the "login" part.
I tried everything I could think of and it didn't work, also I want to make a save file so users can set a password for their "account".
As mentioned in above comments, you need to correctly use your variables, you can however use choice instead of set /p for your commands.
#echo off
:load
rem imitation of loading the os
color 70
ver
title boot
echo please wait...
timeout /t 3 >nul
cls
systeminfo
rem in here user types name he wants to be his account name
:login
title login
cls
date /t
timeout /t 2>nul
cls
echo welcome to windows 71
echo Before we begin please type your name
set /P "_name=here:"
if /i "_%name%"=="admin" goto admin
rem ms=menu start
:ms
echo %time%
echo hello %_name%
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:admin
echo hello %_name% to the admin panel
echo type HELP for list to commands
CHOICE /C HEC /M "Press H for Help, E to exit C for Calender."
goto opt%errorlevel%
:opt1
echo Help stuff goes here
goto :eof
:opt2
exit
:opt3
echo Calenders stuff goes here
Some things to note. You do not require to goto ms is the user is not admin as the statement for not being admin will not be met, we will automatically faal through into the ms label.
Notice where the problems were in your code. i.e if %name%=admin should be if "%_name%"=="admin" with double equal sign and the underscore in the name. It is also double quoted to ensure that we do a match without unwanted whitespace. Lastly /I option to catch ADMIN in any case.
See if /?, choice /? from command line for more help around these functions.
Okay this code is quite wrong.
I fixed it.
#echo off
:load
rem imitation of loading the os
color 70
title boot
echo please wait...
ping localhost -n 3 >nul
cls
rem in here user types name he wants to be his account name
:login
title login
cls
echo Welcome to Microsoft Windows 7!
echo Before we begin, please type your name.
set /p name=here:
if "%name%"=="admin" goto admin
if not "%name%"=="admin" goto ms
rem ms=menu start
:ms
echo %time%
echo Hello %name%
echo Type HELP for list to commands
set /p command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
goto ms
rem wc=wrong command
:admin
CLS
echo hello %name% to the admin panel
echo type HELP for list to commands
set /P command=here:
if "%command%"=="help" goto help
if "%command%"=="exit" goto exit
if "%command%"=="calendar" goto cal
GOTO :ADMIN
Okay but you don't need to start systeminfo and all that.
I have a batch file which makes a .vbs file and is then supposed to open it. They are both in the same folder. I've tried using start, but it simply doesn't work for some reason. I also tried cscript, but since the script is supposed to be hidden, this doesn't work either. If I try to run the vbs file by hand, it works.
code:
#echo off
tasklist | find "wscript.exe" >nul: && goto runningalr
:reset
type nul > sound.vbs
(echo Set Sound = CreateObject("WMPlayer.OCX.7"^))>>sound.vbs
:begin
set /p loop=Do you want the music to repeat? (y/n) ...
if /i %loop%==y goto repeat
if /i %loop%==n goto begin1
goto begin
:repeat
(echo do until false)>>sound.vbs
goto begin1
:begin1
set /p damd=The music's directory? (type "exit" if no more) ...
if /i %damd%==exit goto start
( echo Sound.URL = "%damd%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000)>>sound.vbs
goto begin1
:runningalr
set /p kill=Music already playing. Do you want to stop it? (y/n) ...
if %kill%==y taskkill /f /im wscript.exe && exit
if %kill%==n goto gonnaexit
:gonnaexit
<nul set /p "=Press any key to exit ... "
pause >nul
exit
:start
if /i %loop%==y (echo loop)>>sound.vbs
start C:\Users\User\Desktop\server\sound.vbs
Why didn't start work for me and what works?
instead of having just "find wscript" try using "find %WINDIR%/System32/wscript.exe /I"
You might also need to run the script as an administrator, however this is unlikely.
you give a dedicated path with start, but you write the vbs script to the current working directory. The following line starts from that working directory:
start "" "%~dp0sound.vbs"
I was making my own little batch file.
It will say Bad Command then goto the typing prompt again but everything I type the wrong command (if) I made cmd my batch shuts off
Code:
:ccc
set /p cmd=C:\
if %cmd%==edit goto edit
if %cmd%==Edit goto edit
if %cmd%==exit goto exit
if %cmd%==Exit goto exit
if %cmd%==Restart goto restart
if %cnd%==restart goto restart
echo Bad Command
goto ccc
You should adding quotes and take a look at this : If /I case Insensitive string comparison
#echo off
:ccc
set /p "cmd=C:\ "
if /I "%cmd%"=="edit" goto edit
if /I "%cmd%"=="exit" goto exit
if /I "%cmd%"=="restart" goto restart
echo Bad Command
goto ccc
I need to create a bat that recognizes if a process is running, and if the process is running I need to add a prompt asking if the user wants to end the process to continue with some other tasks.
I have the command to recognize if a process is running(this is the one that worked for me):
#echo off
tasklist /nh /fi "imagename eq vmware-view.exe" | find /i "vmware-view.exe" >nul && (
echo VMWare Horizon is running
) || (
echo VMWare Horizon is not running
)
pause>nul
and I have the command to prompt for an imput:
#echo off
setlocal
:PROMPT
SET /P AREYOUSURE=Are you sure (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
But I don't know how to put those 2 together to say that, if the process is running, display prompt asking user if bat can end process to continue with an update, and if not running just go on with update.
could you please assist?
thanks in advance
I remember from the old DOS days that you could use GOTOs.
for instance a bat file containing the following:
#ECHO OFF
ECHO something
GOTO LABEL0
ECHO something else
GOTO LABEL1
:LABEL0
ECHO some label
:LABEL1
ECHO some other label
will print:
something
some label
some othe label
you will not see
something else
because the first GOTO skipped to LABEL0
You can use a label like :QUIT or :END at the end of your bat and skip at the end if you have nothing to do.
Use ERRORLEVEL to see if TASKLIST and FIND got the process. Afterwards use CHOICE.
#echo off
set task=vmware-view.exe
tasklist | find /i "%task%" > nul
if %errorlevel%==0 (
echo task is running.
choice /c yn /t 10 /d n /m "should we kill it?"
if errorlevel 2 goto end
taskkill /f /im "%task%"
) else (
echo task is not running.
)
:update
echo updating...
:end
I have the following batch script to make life easier at work.
Here is what it is supposed to work:
1- Drag and drop some file onto the .bat
2- Choose file's destination on the "menu"
3- Script copy's files to destination folder
4- Script executes remote procedure (that's the PSexec line)
5- Script copy's the result of the remote procedure to other folders.
And this works fine... except for a "small" detail with which i need some help.
When i try to copy the network location \10.250.39.116\d%... if i haven't previously logged into that machine it wont work.
I've been looking into the 'net use' command to overcome this, but i'm not sure if it suits my needs.
There are a total of 4 different machines i need to authenticate, dependent on the choice of the menu.
Actual Question:
Can i log in to such machines with the batch, and avoid creating duplicate connections every time i run the script ? If so, how?
Thank you for your time!
:)
I know the paths I have all point to the same place :)
#echo off
setlocal enabledelayedexpansion
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
ECHO 1 - Compilar em Qualidade
ECHO 2 - Compilar na HSDEV
ECHO 3 - Compilar nas DEMOS
ECHO 4 - EXIT
ECHO.
SET /P M=Type 1, 2, 3, or 4 then press ENTER:
IF %M%==1 GOTO :QUAL
IF %M%==2 GOTO :HSDEV
IF %M%==3 GOTO :DEMO
IF %M%==4 GOTO EOF
:QUAL
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:HSDEV
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:DEMO
set "PathForms6=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb6i\GH\"
set "PathForms10=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\compilador\fmb10\GH\"
set PathCompilador=\\10.250.39.116 -u Administrator -p Password1 cmd "/C d: & cd d:\GLINTTHSIAS\GLINTTHS\compilador & GH_PRIV_10_02_Forms.bat"
set PathDestinoPriv=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PRIV\GH
set PathDestinoPub=\\10.250.39.116\d$\GLINTTHSIAS\GLINTTHS\PUB\GH
goto :PROCESSA
goto EOF
:PROCESSA
set argCount=0
for %%x in (%*) do (
set /A argCount+=1
set "argVec[!argCount!]=%%~nx"
set "pathVec[!argCount!]=%%~dpx"
)
rem echo Number of processed arguments: %argCount%
for /L %%i in (1,1,%argCount%) do (
echo Vou compilar %%i - "!argVec[%%i]!"
if exist %PathForms6%!argVec[%%i]!.* del /q %PathForms6%!argVec[%%i]!.*
if exist %PathForms10%!argVec[%%i]!.* del /q %PathForms10%!argVec[%%i]!.*
robocopy "!pathVec[%%i]!." %PathForms6% !argVec[%%i]!.fmb > nul
)
c:
cd c:\pstools
psexec %PathCompilador%
for /L %%i in (1,1,%argCount%) do (
if exist "%PathForms10%!argVec[%%i]!.fmx" (
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPriv% /y
xcopy %PathForms10%!argVec[%%i]!.fmx %PathDestinoPub% /y)
)
pause
How much testing have you done with net use? Try running it twice at the command line. Notice how the output changes at the second running:
As you can see, where a connection has already been established, net use will output a summary of the connection rather than creating a duplicate connection.
If you prefer, you could use conditional execution or errorlevel checking. Using this method, you can avoid calling net use until xcopy fails, which should only be the first time. Here's a short example, simply to illustrate the mechanics:
#echo off
setlocal
ping -n 1 10.250.39.116 | find /i "TTL=" >NUL || (
echo 10.250.39.116 is offline. Unable to continue. Press any key to exit.
pause >NUL
goto :EOF
)
call :xcopy "%~1" "destination"
echo Press any key to exit.
pause >NUL
net use \\10.250.39.116\d$ /delete >NUL 2>NUL
goto :EOF
:xcopy <source> <dest_dir>
xcopy /L "%~1" "%~2" 2>NUL || (
net use \\10.250.39.116\d$ /user:username password >NUL 2>NUL
xcopy /L "%~1" "%~2"
)
goto :EOF