Multiple commands from cmd.exe /k with & does not work - batch-file

Apologies if this is answered somewhere, been searching for 30+ mins to no avail.
So I have a batch file, from within it I call:
%comspec% /K "CD ..\..\test\java_6_86 & "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java"
However that does not work.
What I am trying to do is:
(Within a batch file)
Open a new command prompt
Change the Current Directory to the directory where Test1.java lives
Then call the java compiler and have it compile Test1.java
Also, is it possible to tell command where to start, instead of having to do a CD as the fist command?
Thanks,
DoW

I do not know what your %comspec% is but suppose it is 'cmd' this should do the trick (the cmd /K has to be in front of the javac call)
cd ..\..\test\java_6_86 & cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
By the way, a two liner would be much more readable
cd ..\..\test\java_6_86
cmd /K "C:\Program Files (x86)\Java\jdk1.6.0_38\bin\javac.exe" -classpath TestLib.jar Test1.java
You can of course use absolute pathes to specify your classpath and java Source file and skip the directory change.

To answer your second question, you could use pushd/popd. As name suggests, those allow to store current directory, then change to given path (pushd path) and then popd pops/restores the original current dir. It's useful if you need to temporarily change current directory to do some processing or you want to shield your code from unwanted directory changes (eg. if you call another batch). It's also handy with network paths as it auto-creates a drive letter for it. Help pushd will give you full info.

Related

Start a batch file in a certain location

I want to start a batch file in a certain location. I tried start /d C:\Windows C:\Windows\branding\readWin..bat The batch file which launches this in on my desktop. Any way to do this?
Thanks.
I'm not sure exactly what you mean. You shouldn't need to change the current/start-in directory before invoking a script. If that script needs to define its working directory it should do so within.
If that's the case then just enter the full or relative batch file name:
"C:\Windows\branding\readWin..bat"
If the batch file you're wanting to invoke does not define its own current directory and you feel it's necessary then you could be sure by defining it yourself first:
CD /D "C:\Windows"
Or:
PushD "C:\Windows"
After that just run your batch file using its full or relative path as previously mentioned.
Are you running this from command prompt?
start /d "path" file.bat
The quotes around the file path are important, and just the file name follows the path.

Why does 'Run as administrator' change (sometimes) batch file's current directory?

I have a batch file that is in the same directory as the file I want to xcopy. But for some reason the file is not being found.
I thought that current directory was always where the batch file was located.
I run batch file as administrator. This occurs on a Windows 7 64-bit desktop computer.
Batch file:
#ECHO OFF
XCOPY /y "File1.txt" "File2.txt"
PAUSE
Error:
File not found - File1.txt
0 File(s) copied
Which directory is current working directory on starting a batch file with context menu item Run as administrator depends on User Account Control (UAC) setting for the current user.
This can be demonstrated with following small batch file C:\Temp\Test.bat:
#echo Current directory is: %CD%
#pause
With having selected in User Account Control Settings
Default - Notify me only when programs try to make changes to my computer
Don't notify me when I make changes to Windows settings
and using Run as administrator, Windows uses registry key
HKEY_CLASSES_ROOT\batfile\shell\runasuser\command
This registry key does not contain a default string for executing the batch file. Instead there is the string value DelegateExecute with the CLSID {ea72d00e-4960-42fa-ba92-7792a7944c1d}.
The result is opening a dialog window with title User Account Control and text:
Do you want to allow the following program to make changes to this computer?
Program name: Windows Command Processor
Verified publisher: Microsoft Windows
After confirmation by the user, Windows opens temporarily a new user session like when using on command line RunAs.
In this new user session the current working directory is %SystemRoot%\System32 on executing now the command defined in Windows registry with default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
which is:
%SystemRoot%\System32\cmd.exe /C "%1" %*
Therefore a console window is opened with title C:\Windows\System32\cmd.exe and the 2 lines:
Current directory is: C:\Windows\System32
Press any key to continue . . .
After hitting any key, batch execution finishes which results in closing cmd.exe which results in closing the user session.
But with having selected in User Account Control Settings
Never notify me when
Programs try to install software or make changes to my computer
I make changes to Windows settings
the behavior is different as the user has already elevated privileges.
Now Windows uses directly the command
%SystemRoot%\System32\cmd.exe /C "%1" %*
according to default string of key
HKEY_CLASSES_ROOT\batfile\shell\runas\command
in current user session.
The result is opening a console window also with title C:\Windows\System32\cmd.exe, but displayed in window is:
Current directory is: C:\Temp
Press any key to continue . . .
The current working directory of the parent process (Windows Explorer as desktop) is used for executing of the batch file because no switch to a different user session was necessary in this case.
PA has posted already 2 possible solutions in his answer which I replicate here with a small improvement (pushd with directory in double quotes) and with adding a third one.
Change current directory to directory of batch file using pushd and popd:
pushd "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
popd
This works also for UNC paths. Run in a command prompt window pushd /? for an explanation why this also works for UNC paths.
Use directory of batch file in source and destination specifications:
%SystemRoot%\System32\xcopy.exe "%~dp0File1.txt" "%~dp0File2.txt" /Y
Change working directory to directory of batch file using cd:
cd /D "%~dp0"
%SystemRoot%\System32\xcopy.exe "File1.txt" "File2.txt" /Y
This does not work for UNC paths because command interpreter cmd does not support a UNC path as current directory by default, see for example CMD does not support UNC paths as current directories for details.
The error message is very self explanatory. The file file1.txt is not found.
Because the file name does not include an absolute path, the system tries to find it on the current directory. Your current directory does not contain this file.
Your misconception is that the current directory is not the directory that contains the bat file. Those are two unrelated concepts.
You can easily check by adding this two commands in your bat file
echo BAT directory is %~dp0
echo Current directory is %CD%
you can notice they are different, and that there is a subtle difference in the way the last backslash is appended or not.
So, there are esentially two ways to cope with this problem
either change the current directory to match the expected one
pushd %~dp0
XCOPY /y "File1.txt" "File2.txt"
popd
or specify the full path in the command
XCOPY /y "%~dp0File1.txt" "%~dp0File2.txt"
For the sake of completeness and obscurity, I add another workaround, confirmed as working under Windows 8.1 and expected to work elsewhere, as it relies on documented functionality:
You can change the runas command definition keys
HKEY_CLASSES_ROOT\batfile\shell\runas\command and
HKEY_CLASSES_ROOT\cmdfile\shell\runas\command into
%SystemRoot%\System32\cmd.exe /S /C "(for %%G in (%1) do cd /D "%%~dpG") & "%1"" %*
Which results in the bat or cmd file starting in its containing directory when started using the runas verb, respectively the "Run as Administrator" menu entry.
What the additions to the original command exactly do:
cmd /S strips away first and last (double) quote in command string after /C
for %%G in (%1) do enumerates its single entry, the %1 argument,
making it available for expansion as %%G in the loop body; the letter is arbitrary but some may be "reserved"
%%~dpG expands to the drive and path of %%G, the ~ tilde stripping away quotes if present, which is why we add them back explicitly
cd /D changes both the drive and directory to its argument, and finally
& runs the second command "%1" %* regardless of success of the first one.
You can use pushd which will even support UNC paths, but a stray popd would land any script in the system32 directory, not a behavior I would be fond of.
It should be possible to do this for the exefile entry as well, but frankly, I'd rather live with the inconsistency than to attempt this on my system, as any error there could break a lot.
Enjoy defeating the security mechanics of your operating system :)

Create a batch file to run an .exe with an additional parameter

I need a batch file which will do the following:
1. Open CMD and navigate to a location C:/Users/...../program.exe
2. Run the program.exe with an additional command to point it to a config file:
e.g. "program.exe C:/Users/..../configFile.bgi"
How can I do this?
I tried this but with no luck:
start "C:\Users\Ben\Desktop\BGInfo\bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi"
pause
Update
I've used the solution provided by Ganesh (below) and came up with this:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe C:\Users\Ben\Desktop\BGInfo\dc_bginfo.bgi
I've tested it on a local machine (changing the directories) but on the server (with the directory above) it does not work...
The folder directory with batch file:
The error
in batch file abc.bat
cd c:\user\ben_dchost\documents\
executible.exe -flag1 -flag2 -flag3
I am assuming that your executible.exe is present in c:\user\ben_dchost\documents\
I am also assuming that the parameters it takes are -flag1 -flag2 -flag3
Edited:
For the command you say you want to execute, do:
cd C:\Users\Ben\Desktop\BGInfo\
bginfo.exe dc_bginfo.bgi
pause
Hope this helps
You can use
start "" "%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
start "" /D "%USERPROFILE%\Desktop\BGInfo" bginfo.exe dc_bginfo.bgi
or
"%USERPROFILE%\Desktop\BGInfo\bginfo.exe" "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi"
or
cd /D "%USERPROFILE%\Desktop\BGInfo"
bginfo.exe dc_bginfo.bgi
Help on commands start and cd is output by executing in a command prompt window help start or start /? and help cd or cd /?.
But I do not understand why you need a batch file at all for starting the application with the additional parameter. Create a shortcut (*.lnk) on your desktop for this application. Then right click on the shortcut, left click on Properties and append after a space character "%USERPROFILE%\Desktop\BGInfo\dc_bginfo.bgi" as parameter.
Found another solution for the same. It will be more helpful.
START C:\"Program Files (x86)"\Test\"Test Automation"\finger.exe ConfigFile="C:\Users\PCName\Desktop\Automation\Documents\Validation_ZoneWise_Default.finger.Config"
finger.exe is a parent program that is calling config solution.
Note: if your path folder name consists of spaces, then do not forget to add "".

My Batch File keeps looping, but why?

I have written a batch file from a VB.NET program I'm creating.
When I double click on the file in Windows XP it brings up a Command Prompt and appears to be running over and over again.
My batch file is as follows
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename\Command" /ve /t REG_SZ /d "P:\Misc\Rename v2.0\Rename v2.0\bin\Debug\Rename v2.0.exe ""%1""" /f
EXIT
I can't understan what I've done wrong, but if I open a command prompt and run it from there, it runs once.
Any help would be gratly appreciated!
Thanks
In windows, if you have a command line executable with the same name of your bat filename, and the batch file contains this command, the batch file keeps looping.
Example:
Create the file net.bat on your desktop.
In your file write this text: net
Double click the file and it will keep looping.
The cause of this behaviour is the order of execution of the commands. The command you want to execute is in one of the folders in your path. But the batch file is in your current folder so it gets executed first, causing the loop.
make sure:
your script is not named like a build-in command or programm
make sure the scripts your script calls are not named like a build-in command or programm
e.g. if your script is called: reeeeeboooot.bat that calls shutdown -t 10 -r, but in the SAME FOLDER resides a shutdown.cmd
reeeeeboooot.bat will actually call shutdown.cmd INSTEAD of the build-in command.
sometimes the easiest things are the hardest. (pretty often actually :-D)
In windows command line if you want to execute a command or a set of commands then we usually put those commands into a .bat file to execute them at any point of time but we must follow some guidelines before doing so.
The file Name and the command name should not be same else it would loop forever.
There should be no file in that directory having same name as the command name.
In Windows Terminal and DOS, to start a program, you only have to specify the filename without extension (such as .bat, .exe, .cmd, .com). Also, it is case-insensitive.
So if you create a batch file and execute REG, the system will first look in the current directory for something like reg.exe or reg.bat (or another executable with that name). Casing is ignored, so it will include REG.exe. If it doesn't find it, then it will look in the directories specified in the %PATH% environment variable.
In your case, you (assumingly) have an exetable named reg.bat in which you specify that it should call REG. So it will try to call itself, because it will first look in the current directory, in which it will find itself with that name.
The easiest fix is to use the full filename+extension instead. So you can simply change
REG ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f
to
REG.exe ADD "HKCU\Software\Classes\*\shell\Open Folder In Rename" /ve /t REG_SZ /d "Open With Rename" /f

Execute multi command in batch

I tried to run multi-windows commands inside one opened window from a batch file.
I want the opened command window to perform two things in sequence:
Switch volume
Direct to a directory in that volume.
Here's what I wrote:
start cmd /k C: && cd 'C:\Program Files (x86)\aaa\'
However, this only switches volume. The second thing is not executed.
Can anyone please show me the way?
Well, you have at least 2 options...:
1st, make sure your && is passed to new cmd...
start cmd /k "C: && CD c:\temp"
2nd, use /d switch on cd to "get there" in one step...
start cmd /k cd /d c:\temp
KR
Bartek
What don't you just open your cmd at the needed directory? Like^
start /dc:\temp cmd
If you want to change directory to another drive you can use
cd /d C:\
but if your changing directory within the same drive you shouldn't need to switch drives, just change to that directory:
cd "C:\Program Files (x86)\aaa"
Remember to put quotes around paths with spaces, possibly why your command didn't work earlier.
Also, you shouldn't really need start and cmd. What you doing doesn't really need to be threaded as such. If it's a batch file you can just use pause at the end instead of using cmd /k.
Your complete batch file would then look like this:
cd "C:\Program Files (x86)\aaa"
pause >nul
or using cmd /k for one line (in case of command line use):
cmd /k cd "C:\Program Files (x86)\aaa"
Hope this helps!

Resources