Batch file that run jar file on cmd - batch-file

Apologies if duplicate, I want to run a jar file from command prompt, So instead of going to the specific path can i create a batch file,
Simply double click on batch file will do my task,
I found something at [1]: Run a Command Prompt command from Desktop Shortcut but it do not work in my case, I want to run java -jar squirrel-sql.jar
I would like to make a batch file that:
1)Opens cmd.exe
2)Within that Command Prompt runs java -jar squirrel-sql.jar which is present on desktop
3)Leaves the window open so that I can run additional commands if I wish to
How can I do this?

Save the following as squirrel.bat:
java -jar squirrel-sql.jar
REM /P causes SET to prompt and wait for Enter.
REM The underscore is used to ensure that "sure" is not already
REM an environment variable.
set /p _sure="Do you want to exit? [press Enter]"
REM /I causes a case-insensitive comparison
if /I NOT "_sure"=="y" (
REM /B exits the batch script but not the CMD window
exit /b
) else (
REM Any other modifications
)

Related

How to change current directory inside IF statement and reference its path with environment variable CD?

I'm trying to install an app from my PC running Windows 10 using adb and I need to use command cd before I'm installing the app.
But I see a strange behavior. If I use the cd command before the if statement, it's working well. But if I use the cd command inside the if statement, it is not working.
Here is what I tried:
This one is working well:
cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk
if "%arg1%" == "aaa" (
adb install %CD%\sample_app-debug.apk
)
This one results on execution in an error:
if "%arg1%" == "aaa" (
cd %~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk
adb install %CD%\sample_app-debug.apk
)
The error output is:
Failed to stat
C:\Users\ntuser\Documents\workspace\Team\script\sample_app-debug.apk:
No such file or directory
It looks like it did not do the cd command in this case.
Whenever Windows command interpreter cmd.exe encounters the beginning of a command block starting with ( and ending with matching ), the entire block is preprocessed before the command left to the command block is executed at all. This means all environment variables using %variable% syntax are replaced by current value of the referenced environment variable before executing the command which later executes the entire command block. This behavior can be seen on running a batch file from within a command prompt window without #echo off being usually at top of the batch file.
This means on your batch file with the IF command and the command block to execute on condition being true that %CD% is replaced by current directory before the command IF is executed and so also before the command CD is executed at all.
There are two possibilities:
explicitly enable delayed environment variable expansion and reference the environment variable in the command block with using syntax !variable!
or
don't use a command block by using GOTO or a subroutine and CALL.
Solution 1 with enabled delayed environment variable expansion:
setlocal EnableDelayedExpansion
if "%arg1%" == "aaa" (
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "!CD!\sample_app-debug.apk"
)
endlocal
Solution 2 with not using a command block at all and instead use GOTO:
if "%arg1%" == "aaa" goto AAA
rem Other commands
goto :EOF
:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
goto :EOF
rem Or use a different GOTO to continue processing somewhere else in batch file.
Another solution 2 is using a subroutine and command CALL:
if "%arg1%" == "aaa" call :AAA
rem Other commands executed even if above condition is true.
rem Avoid a fall through to the subroutine below.
goto :EOF
:AAA
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install "%CD%\sample_app-debug.apk"
rem Exit the subroutine and continue on line below calling it.
goto :EOF
But I think your code can be even more easier because it is most likely not necessary to specify the *.apk file with full path.
if "%arg1%" == "aaa" (
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
adb.exe install sample_app-debug.apk
)
And perhaps also working is:
if "%arg1%" == "aaa" adb.exe install "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk\sample_app-debug.apk"
It is better to use here %~dp0 which references the drive and path of the batch file ending with a backslash instead of %~f0 which is the name of the batch file with file extension and full path.
For example with batch file being C:\Temp\Test\Install.bat the command line
cd "%~f0\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"
results in execution of
cd "C:\Temp\Test\Install.bat\..\..\apps\app_sample\samples\sample_app\build\outputs\apk"
and the current directory is C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk after this command with in real invalid directory name Install.bat in path which is not a directory.
The command line
cd "%~dp0..\apps\app_sample\samples\sample_app\build\outputs\apk"
is much better as it results in execution of
cd "C:\Temp\Test\..\apps\app_sample\samples\sample_app\build\outputs\apk"
which makes also C:\Temp\apps\app_sample\samples\sample_app\build\outputs\apk the current directory, but with a completely correct path in comparison to above cd command line.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... explains %~dp0 and %~f0.
cd /?
endlocal /?
goto /?
if /?
rem /?
set /? ... explains delayed environment variable expansion on an IF and a FOR example.
setlocal /?
I don't have adb installed. So I can only assume that it is an executable with file extension .exe and not a batch file with file extension .bat or .cmd which would require the usage of call adb.bat ... or call adb.cmd ... in this batch file.

Why are commands in batch script "not recognized" which are executed manually fine?

I am writing a batch script that installs some applications from MSI files from the same folder.
When I write those commands in command prompt window, all is fine and all the commands work properly.
But when I write them into the batch script, suddenly most of the commands such as XCOPY, msiexec, DISM result in an error message like:
'XCOPY' is not recognized as an internal or external command, operable program or batch file.
After googling it for a while, I saw a lot of comments related to the environment variable PATH which should contain C:\Windows\system32 and I made sure its included in the PATH. Also found a lot of answers about writing the full path which I already tried and it didn't work.
I'm working on Windows server 2012.
This is the code of my batch file:
#echo off
set path=C:\ rem default path
rem get the path as parameter to the script:
set argC=0
for %%x in (%*) do Set /A argC+=1
if %argC% gtr 0 (set path=%1%)
IF %ERRORLEVEL% NEQ 0 (
echo %me%: something went wrong with input directory
)
echo Destenation: %path%
SETLOCAL ENABLEEXTENSIONS
SET me=%~n0
SET parent=%~dp0
echo %me%: starting installation of Python 2.7 64bit and Apache 64 bit
REM install .net 3.5
DISM /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:installationMediaDrive:\sources\sxs
msiexec /i ".\py\python-2.7.amd64.msi" TARGETDIR=%path%/Python27 /passive /norestart ADDLOCAL=ALL
mkdir %path%\Apache24
XCOPY /e /Q ".\Apache24" %path%\Apache24
It looks like the batch file should support an optionally specified path to installation directory as first parameter. The code used to check for existence of this optional folder path is very confusing. There are definitely easier methods to check for an optional parameter as it can be seen below.
The main problem is redefining environment variable PATH which results in standard console applications of Windows stored in directory %SystemRoot\System32 and other standard Windows directories are not found anymore by command interpreter cmd.exe on execution of the batch file.
In general it is required to specify an application to execute with full path, file name and file extension enclosed in double quotes in case of this complete file specification string contains a space character or one of these characters &()[]{}^=;!'+,`~ as explained in last paragraph on last output help page on running in a command prompt window cmd /?.
But mainly for making it easier for human users to execute manually applications and scripts from within a command prompt window, the Windows command interpreter can also find the application or script to run by itself if specified without path and without file extension.
So if a user enters just xcopy or a batch file contains just xcopy, the Windows command interpreter searches for a file matching the pattern xcopy.* which has a file extension as defined in semicolon separated list of environment variable PATHEXT first in current directory and if no suitable file found next in all directories in semicolon separated list of environment variable PATH.
There are 3 environment variables PATH:
The system PATH as stored in Windows registry under:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
The folder paths in system PATH are used by default for all processes independent on used account.
The user PATH as stored in Windows registry under:
HKEY_LOCAL_MACHINE\Environment
The folder paths in user PATH are used by default only for all processes running using the account on which the user PATH was set.
The local PATH just hold in memory in currently active environment of running process.
The system and the user PATH are concatenated by Windows to a single local PATH for processes.
Every time a process starts a new process like Windows Explorer starting Windows command interpreter for execution of a batch file, a copy of the environment table of currently running process is created by Windows for the new process. So whatever a process changes on its own local copy of environment variables has no effect on all other already running processes. The local changes on the environment variables are effective only on own process and all processes started by the process modifying its variables.
On starting the batch file the variables PATH and PATHEXT have the values as displayed on running in a command prompt window opened under same user account as used on starting the batch file the command set PATH listing all variables starting with PATH case-insensitive in name.
Now let us look on the second line of the batch file:
set path=C:\ rem default path
This line redefines the local PATH environment variable. Therefore the environment variable PATH being effective for the command process executing the batch file and all applications started by this batch file does not contain anymore C:\Windows\System32;C:\Windows;..., but contains now just this very strange single folder path.
C:\ rem default path
rem is an internal command of cmd.exe and must be written on a separate line. There is no line comment possible in batch code like // in C++ or JavaScript. For help on this command run in a command prompt window rem /?.
On running the batch file without an installation folder path as first argument, the result is that Windows command interpreter searches for dism.*, msiexec.* and xcopy.* just in current directory as there is surely no directory with name rem default path with lots of spaces/tabs at beginning in root of drive C:.
Conclusion: It is no good idea to use path as variable name for the installation folder path.
Another mistake in batch code is using %1% to specify the first argument of the batch file. This is wrong as the arguments of the batch file are referenced with %1, %2, ... Run in a command prompt window call /? for help on referencing arguments of a batch file and which possibilities exist like %~dp0 used below to get drive and path of argument 0 which is the batch file name, i.e. the path of the folder containing the currently running batch file.
I suggest using this batch code:
#echo off
setlocal EnableExtensions
set "SourcePath=%~dp0"
set "BatchName=%~n0"
if "%~1" == "" (
echo %BatchName% started without an installation folder path.
set "InstallPath=C:\"
goto StartInstalls
)
rem Get installation folder path from first argument
rem of batch file without surrounding double quotes.
set "InstallPath=%~1"
rem Replace all forward slashes by backslashes in case of installation
rem path was passed to the batch file with wrong directory separator.
set "InstallPath=%InstallPath:/=\%"
rem Append a backslash on installation path
rem if not already ending with a backslash.
if not "%InstallPath:~-1%" == "\" set "InstallPath=%InstallPath%\"
:StartInstalls
echo %BatchName%: Installation folder: %InstallPath%
echo/
echo %BatchName%: Installing .NET 3.5 ...
DISM.exe /Online /Enable-Feature /FeatureName:NetFx3 /All /LimitAccess /Source:installationMediaDrive:\sources\sxs
echo/
echo %BatchName%: Installing Python 2.7 64-bit ...
%SystemRoot%\System32\msiexec.exe /i "%SourcePath%py\python-2.7.amd64.msi" TARGETDIR="%InstallPath%Python27" /passive /norestart ADDLOCAL=ALL
echo/
echo %BatchName%: Installing Apache 2.4 64-bit ...
mkdir "%InstallPath%Apache24"
%SystemRoot%\System32\xcopy.exe "%SourcePath%\Apache24" "%InstallPath%Apache24\" /C /E /H /I /K /Q /R /Y >nul
endlocal
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /? ... for explanation of %~dp0, %~n0 and %~1.
dism /?
echo /?
endlocal /?
goto /?
if /?
msiexec /?
rem /?
set /?
setlocal /?
xcopy /?
And read also
the Microsoft TechNet article Using command redirection operators,
the Microsoft support article Testing for a Specific Error Level in Batch Files,
the answer on change directory command cd ..not working in batch file after npm install and the answers referenced there for understanding how setlocal and endlocal really work and
the answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? for understanding why using set "variable=value".
And last take a look on:
SS64.com - A-Z index of the Windows CMD command line
Microsoft's command-line reference
Windows Environment Variables (Wikipedia article)
The administrator of a Windows server should twist everything written here and on the referenced pages round one's little finger.

Batch file does not follow through with launching program once given permission by the user account control

I have the following batch file:
for /f "delims=" %%x in (path.txt) do set path=%%x
set address=62.75.218.30:14567
start bf1942.exe
With path.txt file containing the path to the executable. Once I run the batch file I am prompted if I want to allow BF1942.exe to make changes to this computer (user account control). Once I select 'yes' nothing happens. Similarly when I launch BF1942.exe by double clicking on the icon I get the same prompt but the game launches after I give permission.
Edit: I did some investigation. When I moved the path.txt and the batch file into my Bf1942 folder and ran the batch file this worked. So the problem has something to do where the file is located.
You can either move your batch file to the same folder as the executable or specify the full path to the executable in the batch file.
For example:
start c:\Bf1942\bf1942.exe
Also, you shouldn't use path as a variable name. There is a system environment variable named path and your batch is overwriting it. Change yours to myPath or something else.
Change the working directory (do not change the system environment variable path) as follows:
for /f "delims=" %%x in (path.txt) do set "myPath=%%~x"
set "address=62.75.218.30:14567"
pushd "%myPath%"
start "" bf1942.exe
or
set "address=62.75.218.30:14567"
for /f "delims=" %%x in (path.txt) do (
pushd "%%~x"
start "" bf1942.exe
)
Resources (required reading):
(command reference) An A-Z Index of the Windows CMD command line
(additional particularities) Windows CMD Shell Command Line Syntax
(%~x etc. special page) Command Line arguments (Parameters)

commands after command "dx --dex --output=classes.dex FINAL.jar" are not executing in a batch file

.
I am trying to create a batch file to avoid a repetitive tasks,
but the commands after the command "dx --dex --output=classes.dex FINAL.jar" which i execute to create a .dex file from .jar file do not execute. The commands before "dx --dex --output=classes.dex FINAL.jar" command executes serially well, this command also executes successfully , it does not give me any error.
But after successful execution of this command command prompt exits automatically,and rest commands do not execute.
Following is my AUTO.bat file:
del Freedom_Plus_Img_Cust.apk_signed.apk
del classes.dex
dx --dex --output=classes.dex FINAL.jar
ping 1.1.1.1 -n 1 -w 30000
move /Y XXXX.apk XXXX.zip
7z d XXXX.zip classes.dex
7z a XXXX.zip classes.dex
move /Y XXXX.zip XXXX.apk
java -jar signapk.jar certificate.pem key.pk8 XXXX.apk
setlocal EnableDelayedExpansion
#ECHO off
set /p pathName=Enter The Value:
#echo %pathName%
So please can anyone tell me why this could happen.Any solution to this could appreaciable.
Thanx in advance .
Note: I am using Windows XP . .
I repeat the right answer from MC ND to remove this already answered question from the list of unanswered questions.
Command dx is not a command, nor is it a console application. It is another batch file.
A batch file must be executed from within a batch file using command CALL or cmd.exe continues batch execution on the other executed batch file without returning back to initial batch file.
For details about command CALL execute in a command prompt window call /?
This behavior can be verified easily with 2 batch files.
Batch file 1 with name test1.bat:
#echo off
test2.bat
echo End of test1.bat
pause
Batch file 2 with name test2.bat:
echo End of test2.bat
Pause
Execution of test1.bat results in output:
End of test2.bat
Press any key to continue . . .
The lines after start of test2.bat are not executed anymore by cmd.exe.
With changing in test1.bat the second line to call test2.bat the output on execution of test1.bat changes to:
End of test2.bat
Press any key to continue . . .
End of test1.bat
Press any key to continue . . .
Hint: In batch files it is advisable to call other files (EXE, COM, BAT, CMD, ...) always with their file extension. This makes the execution of the batch file a little bit faster as Windows do not need to search for name.* in current directory and in all directories of environment variable PATH to find the file which should be executed. It is also advisable to call other files with full path if the batch file must not work with paths relative to current working directory to avoid a dependency on directories in PATH.

How do I execute cmd commands through a batch file?

I want to write a batch file that will do following things in given order:
Open cmd
Run cmd command cd c:\Program files\IIS Express
Run cmd command iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
Open Internet Explorer 8 with URL= http://localhost:8088/default.aspx
Note: The cmd window should not be closed after executing the commands.
I tried start cmd.exe /k "cd\ & cd ProgramFiles\IIS Express", but it is not solving my purpose.
So, make an actual batch file: open up notepad, type the commands you want to run, and save as a .bat file. Then double click the .bat file to run it.
Try something like this for a start:
c:\
cd c:\Program files\IIS Express
start iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
start http://localhost:8088/default.aspx
pause
I think the correct syntax is:
cmd /k "cd c:\<folder name>"
This fixes some issues with Blorgbeard's answer (but is untested):
#echo off
cd /d "c:\Program files\IIS Express"
start "" iisexpress /path:"C:\FormsAdmin.Site" /port:8088 /clr:v2.0
timeout 10
start http://localhost:8088/default.aspx
pause
cmd /c "command" syntax works well. Also, if you want to include an executable that contains a space in the path, you will need two sets of quotes.
cmd /c ""path to executable""
and if your executable needs a file input with a space in the path a another set
cmd /c ""path to executable" -f "path to file""
#echo off
title Command Executer
color 1b
echo Command Executer by: YourNameHere
echo #################################
: execute
echo Please Type A Command Here:
set /p cmd=Command:
%cmd%
goto execute
start cmd /k "your cmd command1"
start cmd /k "your cmd command2"
It works in Windows server2012 while I use these command in one batch file.
cmd /k cd c:\
is the right answer
I was trying to run a couple of batch files parallely at startup, if a condition was true.
For this I made a parent batch file which should have checked for the condition and invoke the other child batch files if the condition was true.
I tried to achieve it via START but it gave me an empty black command prompt running in the directory of children batch files, instead of running the children batch files themselves
The thing which worked for me was by using a combination of START and CALL
As an example
condition ...
start call "C:\Users\Amd\conn\wsl_setup - conn1.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn2.bat"
start call "C:\Users\Amd\conn\wsl_setup - conn3.bat"
I know DOS and cmd prompt DOES NOT LIKE spaces in folder names. Your code starts with
cd c:\Program files\IIS Express
and it's trying to go to c:\Program in stead of C:\"Program Files"
Change the folder name and *.exe name. Hope this helps

Resources