Input -> Goto don't work -> After resolving, doesn't work - variables

Beginning in bat: input -> goto don't work
And I tried to get the thing work. But it doesn't seem to want to work... my script is batch as following.
#echo off
title [title here]
setlocal enabledelayedexpansion
color 0F
mode con cols=1000 lines=1000
[code between (nothing to do with InstallChoice]
set /p InstallChoice = -^>
if "%InstallChoice%"=="1" goto :Install1
goto :Install
So...
if "%InstallChoice%" == 1 goto :Install1
doesn't work, it sends me again to :InstallChoice input, so it seem like %InstallChoice% isn't 1.
if "%InstallChoice%"=="1" goto :Install1
Unexpected goto at this time... the message I don't really get. System does not have to understand it, execute it, over...

I think you just have to remove extra space set /p InstallChoice = -^> should be set /p InstallChoice=-^>

Related

Return the status of an FTP upload using WinSCP batch scripting executed from VBA?

I am sharing an Excel workbook with multiple users who are executing a macro that executes the following WinSCP batch script:
"C:\Program Files (x86)\WinSCP\WinSCP.com" ^
/command ^
"open ftp://user:pass#ftp.website.org/" ^
"cd /incoming/data" ^
"put ""%~dp0file.txt""" ^
"exit"
set WINSCP_RESULT=%ERRORLEVEL%
if %WINSCP_RESULT% equ 0 (
echo Success
) else (
echo Error
)
exit /b %WINSCP_RESULT%
The script is executed from VBA as follows:
Call Shell("C:\Users\" & Environ("username") & "\Sharepoint - Library Folder\FTP\ftpupload.bat")
When executed, the command window appears for 1-2 seconds and goes away. Is there a way to leave it up with the Success/Error result or even better would be to pass it back to VBA so I can display the result in an Ok-Window?
Note: I'd like to avoid having to register the WinSCP COM in VBA as this workbook is being used by multiple people and I need to keep it simple with as little prerequisites as possible.
Your batch file already returns exit code indicating an error/success.
So all you need is to capture the code and act accordingly.
For that, see Is it possible to return error code to VBA from batch file?
Set oSHELL = VBA.CreateObject("WScript.Shell")
Dim exitCode As Integer
exitCode = oSHELL.Run("""C:\Users\" & Environ("username") & "\Sharepoint - Library Folder\FTP\ftpupload.bat""", 0, True)
If exitCode <> 0 Then
MsgBox "Failed", vbOKOnly, "Failure"
Else
MsgBox "Succeeded", vbOKOnly, "Success"
End If

Get Return Value from Console Application in Batch file

I'm trying to get return value from console application in batch file. Console application keep returns 0 value even when the rtnValue is 1 in console application
Console Application Code
Public Function Main() As Integer
Dim rtnValue as Int16 = 0
if Not Process Then rtnValue = 1
Return rtnValue
End Function
Batch File
#ECHO OFF
start /d "%~dp0" ConsoleApp.exe
if "%ERRORLEVEL%" == "0" goto success
:success
echo Success with error code: %ERRORLEVEL%
PAUSE
goto end
:error
echo Failed with error code: %ERRORLEVEL%
PAUSE
:end
Don't use start /d "%~dp0" ConsoleApp.exe that is launching a new shell to run it in. Instead just use
cd %~dp0
ConsoleApp.exe
and then test for the exit code.

/ was unexpected at this time - batch script

im using the batch script below and i keep getting the error "/ was unexpected at this time."
any ideas?
thanks
# Echo off
set n_user =%%%% username password;
c:\windows\system32\find / n "%% n_user" C:\Users\Voyager\Desktop\server installs\OpenVPN\config\list.txt> result.txt
for / f "skip = 1"%% a in (result.txt) do set n =%% a
if "% s%" == "----------" exit 1
exit 0
Remove the spaces between / and the switch character in / n (should be /n) and / f (should be /f). (You have a couple of other space issues, too. I've fixed them as well. You also need to surround pathnames containing space characters in double-quotes.)
# Echo off
set n_use =%%%% username password;
c:\windows\system32\find /n "%%n_user" "C:\Users\Voyager\Desktop\server installs\OpenVPN\config\list.txt"> result.txt
for /f "skip=1" %%a in (result.txt) do set n=%%a
if "%s%" == "----------" exit 1
exit 0

How to capture the PID of a process when launching it from command line?

Is there a way to do this purely in a .bat file?
The purpose is to launch iexplore.exe, then kill just that instance when it's finished.
Here's what I use:
#echo off
rem there is a tab in the file at the end of the line below
set tab=
set cmd=javaw -jar lib\MyProg.jar
set dir=%~dp0
echo Starting MyProg
set pid=notfound
for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
`wmic process call create "%cmd%"^, "%dir%"`
) do (
if /I %%i EQU ProcessId (
set pid=%%j
)
)
echo %pid% > MyProg.pid
The directory is set to the directory that the cmd file is located in. Change dir to alter that. Modify cmd to change which command is run.
If you want a stop.cmd that kills it, it would look like this
#echo off
for /F %%i in (%~dsp0MyProg.pid) do taskkill /F /PID %%i
del %~dsp0MyProg.pid
you can use vbscript, here's an example creating notepad, then terminating it using its pid
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set objStartup = objWMIService.Get("Win32_ProcessStartup")
Set objConfig = objStartup.SpawnInstance_
Set objProcess = GetObject("winmgmts:root\cimv2:Win32_Process")
errReturn = objProcess.Create("notepad.exe", null, objConfig, PID)
If errReturn = 0 Then
WScript.Echo "Process ID is: " & PID
End If
WScript.Echo "Ready to kill process: " & PID & "? [Y|y]"
Do While Not WScript.StdIn.AtEndOfLine
strInput = strInput & WScript.StdIn.Read(1)
Loop
If LCase(strInput) = "y" Then
WScript.Echo "Select * from Win32_Process Where ProcessId = '" & PID & "'"
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where ProcessId = '" & PID & "'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
End If
save as myscript.vbs and on command line
c:\test> cscript /nologo myscript.vbs
A slight variation on the answer provided by #kybernetikos since it has a parsing issue. Note the line if %%j gr 0 (
#echo off
rem there is a tab in the file at the end of the line below
set tab=
set cmd=javaw -jar lib\MyProg.jar
set dir=%~dp0
echo Starting MyProg
set pid=notfound
for /F "usebackq tokens=1,2 delims=;=%tab% " %%i in (
`wmic process call create "%cmd%"^, "%dir%"`
) do (
if %%j gtr 0 (
set pid=%%j
)
)
echo %pid% > MyProg.pid
Most often you do know what task you start - in this case, which page iexplorer shall show.
So how about
taskkill /FI "Windowtitle eq *yourpagetitle*"
It will kill all instances of something showing your page title, but with a specific title most often there should be exactly one.
Tom
Ummm, TaskList & TaskKill?!
For some reason your approach of getting process id did not work for me, but since I'm expert in batches, I've coded my own approach, attaching here:
#echo off
call:AsyncCmd
rem call:AsyncCmd "echo hello world"
rem call:AsyncCmd "call build.bat"
exit /b
rem ------------------------------------------------------------------------------------------
rem Starts asynchronous command execution
rem %1 is command, if empty - only aborts existing build.
rem ------------------------------------------------------------------------------------------
:AsyncCmd
if exist %~dp0SetupBuild_Completed.txt (
del /f %~dp0SetupBuild_Pid.txt >nul 2>&1
del /f %~dp0SetupBuild_Completed.txt >nul 2>&1
)
if not exist %~dp0SetupBuild_Pid.txt goto lStartProc
rem --------------------------------------------------
rem Abort build process
rem --------------------------------------------------
set /p pid=<%~dp0SetupBuild_Pid.txt
echo Cancelling setup build process, process id %pid%
pskill -t %pid%
del /f %~dp0SetupBuild_Pid.txt >nul 2>&1
:lStartProc
if "%~1" == "" exit /b 0
rem --------------------------------------------------
rem Starts asyncronous build process
rem --------------------------------------------------
set dir=%~dp0
set dir=%dir:~0,-1%
for /f "tokens=2 delims==; " %%a in ('wmic process call create "cmd /c mincon.exe && %~1 && echo 1>%~dp0SetupBuild_Completed.txt"^, "%dir%" ^| findstr /r "ProcessId"') do set pid=%%a
echo Setup build started, process id: %pid%
echo %pid%>%~dp0SetupBuild_Pid.txt
exit /b 0
PowerShell can be used for this:
powershell -executionPolicy bypass -command "& {$process = start-process $args[0] -passthru -argumentlist $args[1..($args.length-1)]; exit $process.id}" notepad test.txt
echo Process ID of new process: %errorlevel%
I think you can't do that with simple command line utilities, as IE actually spawns child processes for each tab, i.e. if IE is not yet running you would get one parent IE process and a child process for the tab, and if IE is already running you would simply get a single child process.
It will be even quite tricky when you write your own tool to kill IE because when you kill a child (tab) process, IE will automatically recover this tab.
See also this related question: How to obtain process of newly created IE8 window? (though there is no good answer there).

batch file which asks for username/password + registration

I would like a batch file that can register a user into itself.
something like:
#echo off
echo Choose an option:
echo 1:Register
echo 2:Login
Set option=
set /p option=Your option:
if %option%==1 goto reg
if %option%==2 goto login
...
:reg
--The registration script--
goto login
...
:login
Set usr=
set /p usr=Username:
if %usr%== goto pass
echo False username!
#pause
goto login
...
:pass
Set passw=
set /p passw=Password:
if %passw%== goto success
echo False password!
#pause
goto login
So... something like that. 'if %passw%== "" and if %usr%== ""' === it should automatticly make that at registration.
It will be nice if more than 1 people could register.
To test for an empty string, put quotes around both sides:
if "%usr%"==""
To append to a text file, use >>
echo %usr%:%pass% >> passwd.txt
To loop over the contents of a file, use for /f:
for /f "delims=: tokens=1,2" %%x in (passwd.txt) do (
if "%%x"=="%usr%" then (
set usrfound=true
)
)
if "%usrfound%"=="" then (
rem Register user
) else (
rem Check password and succeed/fail
)
During testing or debugging, make sure echo is on. If you want to set and test a variable inside a for loop or other multiline statement, put this near the start of the file:
setlocal enabledelayedexpansion
and any time you need to read a variable that might have changed inside the block, use !var! instead of %var%.
Hmmm. This is what i found out by some help:
#echo off
title REGISTERANDLOGIN
if exist programbase.dll goto login
if not exist programbase.dll goto register
:register
title Register
echo Register
echo.
echo Please fill in the blanks.
echo.
Set usrname=
set /p usrname=Username:
Set passw=
set /p passw=Password:
echo %usrname%>> programdata.dll
echo %passw%>> programbase.dll
goto login
:login
:begin
set usr=
set /p usr=Enter your username:
if {%usr%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (programdata.dll) do (
if {%%a}=={%usr%} set authenticated=true
)
if not defined authenticated (echo Invalid Username & goto :begin)
:passwo
set pass=
set /p pass=Enter your password:
if {%pass%}=={} goto :begin
set authenticated=
for /f "tokens=*" %%a in (programbase.dll) do (
if {%%a}=={%pass%} set authenticated=true
)
if not defined authenticated (echo Invalid password & goto :begin)
:loggedin
cls
echo Welcome.
#pause
exit /b 0
I wonder if this will work:
# echo off
goto username
:username
echo Hello, please log in with your user ID or type "reg" to register.
echo Username:
set /p user
if %user%== username(s)here goto password
if %user%==reg goto register
if not defined user (
cls
echo Bad username!
pause
exit
)
Then carry out the rest of the program. I'd try this, and if it doesn't work, tell me about it!
You can try this one . . . I wrote a batch file that has a login & signup module. It creates a file where accounts created are stored. When logged in, special features are included like "Color Changer" and "TicTacToe". The TicTacToe is NOT mine, I got it from
www.screwthelotofyou.webs.com/files/ . This file and the Color Changer were mine.
"Reproduce, edit and distribute all you want. As if I could stop you." - ScrewTheLotofYou
I can't paste the code here, it's too long . . . Link: http://www.mediafire.com/?kwzzvk24nuze8fq
Hope it worked. :D ~ meilviruz99
Found some seriously evil tricks on this web site, including:
ECHO Enter some input, and press Enter when ready . . .
ECHO ←[13;0;64;13p
COPY CON USRINPUT.TMP
ECHO ←[13;13p
CLS
ECHO You typed:
TYPE USRINPUT.TMP
Which uses ANSI key translation to change Enter into CTRL-Z.
I couldn't quite say I fully understand your question, but if in doubts about anything related to batch scripting, I could recommend checking out this website this website.
The tutorial is really exhaustive, easy to follow and has helped me a number of times before.
#echo off
if exist "%userprofile%\documents\Login\Accounts" goto start0
if not exist "%userprofile%\documents\Login\Accounts" md "%userprofile%\documents\Login\Accounts"
:start0
title Login
color 02
cls
echo ---------------------------------
echo Login.
echo ---------------------------------
echo.
echo.
echo.
echo 1.Create New Username.
echo 2.Log in.
echo.
echo.
echo.
set /p A=Choice:
IF '%A%' == '1' GOTO NewUser0
IF '%A%' == '2' GOTO Login0
IF NOT '%A%' == '1' GOTO start0
:NewUser0
cls
color 02
cd "%userprofile%\documents\Login\Accounts"
set /p newuser=New Username:
IF EXIST %newuser%.bat GOTO inval0
set /p newpass=New Password:
echo set realusername=%newuser% >> %newuser%.bat
echo set password=%newpass% >> %newuser%.bat
IF EXIST %newuser%.bat GOTO NewUser1
IF NOT EXIST %newuser%.bat GOTO NewUser2
:NewUser1
echo Your Account Has Been Created Successfully. Press An Key To Continue.
pause >nul
GOTO start0
:NewUser2
cls
color 0c
del /f /q %newuser%.bat
echo ERROR!Account Could Not Be Created For Unknown Reasons.Press Any Key To Continue
pause >nul
GOTO start0
:Login0
cls
color 02
cd "%userprofile%\documents\Login\Accounts"
echo Login.
echo.
echo.
echo.
echo.
set /p loguser=Username:
IF NOT EXIST %loguser%.bat GOTO inval1
:Login1
set /p logpass=Password:
call %loguser%.bat
if %logpass% == %password% goto success0
goto invalid2
:inval0
cls
color 0c
echo Desired Username Already Exists. Press Any Key To Continue.
pause >nul
GOTO start0
:inval1
cls
color 0c
echo Desired Username Does NOT Exist. Press Any Key To Continue.
pause >nul
GOTO start0
:invalid2
cls
color 0c
echo Invalid Password. Press Any Key To Continue.
pause >nul
cls
color 02
echo Login.
echo.
echo.
echo.
echo.
echo Username:%loguser%
GOTO Login1
:Success0
cls
color 02
title End Of My Script
echo You Successfully Logged In.
echo Here Is Where You Continue The Script.
ping localhost -n 3 >nul
echo.
echo.
echo.
echo Press Any Key To Exit.
pause >nul
exit
Check out this incredibly simple chatting program:
#echo off
cls
echo #prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
:LoGINSCR
ECHO Euronet MessageBoard
ECHO v1.0 [BETA]
goto LOGIN
:LOGIN
ECHO.
set/p user=Login As:
if %user%==Eugene goto PASSWORD
if %user%==Toxic goto PASSWORD
if %user%==Plague goto PASSWORD
if %user%==Jason goto PASSWORD
goto LOGIN
:PASSWORD
set/p pass=Password:
if %pass%==$euronet$ goto SYSTEM
goto LOGIN
:SYSTEM
ECHO Welcome to Euronet MessageBoard, %user%.
goto INPUTMAIN
:INPUTMAIN
ECHO.
set/p main=%user%/chat:
if %main%==help goto HELP
if %main%==join goto JOIN
if %main%==quit goto EXIT
if %main%==ic goto IC
goto ERROR
:IC
ping google.com
PAUSE
ping wikipedia.org
PAUSE
ping facebook.com
PAUSE
ipconfig
PAUSE
goto INPUTMAIN
:ERROR
ECHO [ERROR] No command found ("%main%")
goto INPUTMAIN
:HELP
ECHO Type help to show the help page.
ECHO Type join to join board
ECHO Type quit to exit MessageBoard
ECHO Type ic for internet check
ECHO After you joined the board, you can update
ECHO your screen by pressing "Shift & 6".
goto INPUTMAIN
:JOIN
ECHO.
set/p mesgc=Enter your message:
ECHO %user% [%time%] - %mesgc% >> messg1
goto TRAFFIC
:TRAFFIC
cls
TYPE messg1
goto JOIN
:CLS
cls
goto TRAFFIC
:EXIT
exit
It uses one file, but multiple users could run the bat file and connect to the file. It works by looping the TYPE and set/p commands repeatedly. Although it couldn't update the chat list by itself, you could update it by pressing Shift and 6, and hitting Enter.
Why not just compare input to a user file?
make a file for each user and maybie get a encryption and decryption program to run automaticly for max security.
Batch is a verry simple launguage. just mess around with it for a while.
I'm decently proud of this :)
This has two different files: login.bat which handles the login and login2.bat which handles the registration.
login.bat:
#echo off
choice /c:RL /m "Choose an option: Register (R) or Login (L).:
if errorlevel 2 goto login
if errorlevel 1 goto register
:register
start /wait C:\[path]\login2.bat
cls
goto login
:in
cls
echo Welcome %u%
echo.
echo Bla Bla Bla or start "a program"
pause
exit
:login
set /p u=Username
set /p p=Password
and login2.bat:
#echo off
:a
set /p a="Choose a Username"
set /p b="Choose a Password"
echo.
choice /m "Are you sure you would like your Username to be %a% and your Password to be %b%?"
if errorlevel 2 goto a
set q="if %%u%% equ %a% if %%p%% equ %b% goto in"
for /f "tokens=*" %%I in (%q%) do set m=%%I
echo %m% >>C:\[path]\login.bat
exit
The reason why :in, the label which handles what happens after you login successfully has to be in the middle instead of at the end of login.bat is so that the username/password combinations could be appended to the :login label. Obviously you could add other embellishments and change the name of the files, but this is my basic design. I like how short and comprehensible these are. And they work like a charm 100% of the time.
Hope this helps!
I'm not providing full source code because all of you probably know how to do this.
A really easy way to do what you're asking is to just have a registration and a login page that detects files. Just make a couple directories like userSaves or passSaves for example. Then you have the system just detect whether or not the files exist on login:
:page
set /p usr=Username:
if exist mydir\LoginProgram\saves\userSaves\%usr% (
echo Valid Username.
pause
goto passentry
cls
) else (
echo Invalid Username.
pause
goto page
cls
)
:passentry
cls
set /p pass=Password:
if exist mydir\LoginProgram\saves\passSaves\%pass% (
echo Valid Password. Welcome!
pause
goto whateverpage
cls
) else (
echo Invalid Password. Login failed.
pause
goto page
cls
)
Super simple code. Just do the same thing for registration.