How do I run a BAT script without changing directories?
I am in ./a and the script cd's into ./a/bc. If I need to terminate my script for whatever reason I am now in bc instead of a. How do I run the script and not have my folder change?
Also, I don't like how it asks me if I'd like to terminate my script. Can I disable that and let it terminate?
The setlocal command is useful for this. Any directory changes after setlocal are just local to the batch script. BTW this also applies to any environment variables (set commands).
For example, after running this batch script:
cd /d c:\temp
setlocal
cd /d c:\windows
the directory will be c:\temp since the second cd in the script is just local to the script.
If you don't need to propagate environment variable changes into the current environment and cannot touch the batch file (to use the pushd/popd variant which I usually use), you can still spawn a new instance of cmd:
cmd /c myBatch.cmd arg1 arg2 ...
Also has the nice property of leaving your original batch file running even if the called batch throws up errors. I do that in my batch unit testing framework, for example, to ensure that a failing batch file won't stop the tests from executing.
You can run your script with start yourscript.bat. This makes it run in a new command window, and therefore does not affect the working directory of the command prompt that started the script.
Another possibility is to not use cd and use absolute paths instead.
The first question is: why do you need to change the directory? Can you simply work with paths relative to the one of your batch file? (e.g. using %~dp0\a\bc to reference the directory)
But if you really, really need to do that, you can do the following:
REM change the current directory
pushd ..\a\bc
.. do your stuff here
REM restore the old "current directory"
popd
Related
I want to run octave-gui (Octave 5.1 installed with installer and "C:\Octave\mingw64\bin" is in path variable) scripts run by Windows Task Scheduler. I have to run octave-gui since I want to use the qt toolkit for plotting that octave (without gui) does not support. Therefore I normally use simple bat files like "octave-gui --no-gui c:\path\myfile.m".
But the problem is that I cannot run this bat file by clicking in the Windows Explorer or running from command line. Even the most simple bat file with the content "octave-gui --no-gui" gives me the following error:
But the funny part is that I can make it work somehow:
open command line
run "octave" or "octave-gui" and close/quit it
then I can the bat file from the command line
But this could not be the solution, could it? This only works in the (interactive) command line. How does it work in the Task Scheduler?
So, do you have a solution to run either batch files using octave-gui or octave with qt toolkit.
Here is the workaround with "where" as asked by Gerhard:
The command octave is technically incorrect.
It works only from your Command Prompt window because its extension .bat is listed within the values assigned to an unmodified environment variable %PATHEXT%. It also assumes that there are no other files named octave.com or octave.exe, anywhere within the any of the directories listed under your environment variable %PATH%. Additionally it also assumes that there is not an executable file named octave with any extension listed under %PATHEXT% in the current directory when invoked.
You should, for safety, use octave.bat instead.
octave.bat
Octave.bat will parse any input arguments, set up the required environment, and then run either start octave-gui.exe --gui %* or octave-cli.exe %* if it detected --no-gui as one of the input arguments.
Additionally when running a batch file from another, (in this case start_my_octave_script.bat), you should Call it if you're wanting control to return to it afterwards, which will almost certainly be the case.
call octave.bat <command line options>
If you're satisfied that your %PATHEXT% environment variable is unmodified or at least holds the default values, you can omit the .batextension, but please bear in mind the previous advice.
call octave <command line options>
I made a workaround thanks to the hints made by Compo. It seems to me that a solution must be done in the "octave.bat" and so I did. I made a copy and named it "octave-gui-nogui-withqt.bat" and removed all the gui checking stuff and only run "octave-gui.exe --no-gui" (scroll down):
:; # if running from bash, recall using cmd.exe
:; cmd.exe //c "$0" "$#"; exit $?
#echo off
Rem Find Octave's install directory through cmd.exe variables.
Rem This batch file should reside in Octaves installation bin dir!
Rem
Rem This trick finds the location where the batch file resides.
Rem Note: the result ends with a backslash.
set OCT_HOME=%~dp0\.\..\
Rem Convert to 8.3 format so we don't have to worry about spaces.
for %%I in ("%OCT_HOME%") do set OCT_HOME=%%~sI
Rem Set up PATH. Make sure the octave bin dir comes first.
set PATH=%OCT_HOME%qt5\bin;%OCT_HOME%bin;%PATH%
Rem Set up any environment vars we may need.
set TERM=cygwin
set GNUTERM=wxt
set GS=gs.exe
Rem QT_PLUGIN_PATH must be set to avoid segfault (bug #53419).
IF EXIST "%OCT_HOME%\qt5\bin\" (
set QT_PLUGIN_PATH=%OCT_HOME%\qt5\plugins
) ELSE (
set QT_PLUGIN_PATH=%OCT_HOME%\plugins
)
Rem set home if not already set
if "%HOME%"=="" set HOME=%USERPROFILE%
if "%HOME%"=="" set HOME=%HOMEDRIVE%%HOMEPATH%
Rem set HOME to 8.3 format
for %%I in ("%HOME%") do set HOME=%%~sI
Rem Start Octave (this detaches and immediately returns).
Rem make this call in order to have qt on the cli
octave-gui.exe --no-gui %*
Is this the most elegant one? I guess that upstream Octave should allow a new option like "--no-gui-but-use-qt" or similar. What do you think?
It still confuses me that "octave-cli.exe" and "octave-gui.exe" have more differences besides the visible gui.
I'm working in Windows and would like to know if there is a way to remove the current working directory from the path? I understand that this is the default behavior in PowerShell, but I need it to work in batch or at the Windows command-line.
In UNIX, I would just make sure that my $PATH variable not contain .. Is there any way to accomplish this in batch? This is the current behavior:
H:\tmp>dir
Volume in drive H has no label.
Volume Serial Number is E29C-7B61
Directory of H:\tmp
04/27/2018 10:39 AM <DIR> .
04/27/2018 10:39 AM <DIR> ..
04/27/2018 10:40 AM 37 dwk.bat
1 File(s) 37 bytes
2 Dir(s) 987,995,770,880 bytes free
H:\tmp>dwk.bat
dwk.bat has been run.
H:\tmp>
This is the desired behavior:
H:\tmp>dwk.bat
'dwk.bat' is not recognized as an internal or external command,
operable program or batch file.
H:\tmp>.\dwk.bat
dwk.bat has been run.
H:\tmp>
Thanks.
I recommend first reading the answers on Stack Overflow questions:
Where is "START" searching for executables?
What is the reason for '...' is not recognized as an internal or external command, operable program or batch file?
Many thanks to eryksun because of this answer would not exist without his comment on above referenced answer.
Next I recommend reading the Microsoft Developer Network (MSDN) articles:
Naming Files, Paths, and Namespaces
NeedCurrentDirectoryForExePath function
The question can be answered with: Yes, it is possible for desktop applications and batch files on
Windows Vista and all later Windows client versions and
Windows Server 2003 and all later Windows Server versions.
An environment variable with name NoDefaultCurrentDirectoryInExePath must be defined with any value to prevent execution of a script (.bat, .cmd, .vbs, ...) or an application (.com, .exe) stored in current directory without explicitly using .\ as required on Unix/Linux.
The environment variable NoDefaultCurrentDirectoryInExePath can be defined as system variable to turn off searching in current directory for a script or application for all accounts on this machine. But this is surely no good idea as it will result definitely in many applications including installers and uninstallers won't work anymore correct.
The environment variable NoDefaultCurrentDirectoryInExePath can be defined as user variable to turn off searching in current directory for a script or application for processes using this account. But this is surely also no good idea.
But it can make sense to set the environment variable NoDefaultCurrentDirectoryInExePath as local variable in some use cases to turn off searching in current directory for a script or application without explicitly using .\ on Windows versions with kernel function NeedCurrentDirectoryForExePath which cmd.exe calls before searching for a script file or application not containing a backslash \ (or a forward slash /) in file name string.
Example:
#echo off
pushd "%TEMP%"
set "NoDefaultCurrentDirectoryInExePath=0"
echo #echo %%0 executed successfully.>Test1.bat
echo Calling Test1.bat ...
call Test1.bat
echo Calling .\Test1.bat ...
call .\Test1.bat
echo Starting Test1.bat ...
start /wait Test1.bat ^& timeout 5
set "NoDefaultCurrentDirectoryInExePath="
echo Calling again Test1.bat ...
call Test1.bat
del Test1.bat
popd
pause
This batch file executed from within a command prompt window results in output of current console window:
Calling Test1.bat ...
'Test1.bat' is not recognized as an internal or external command,
operable program or batch file.
Calling .\Test1.bat ...
.\Test1.bat executed successfully.
Starting Test1.bat ...
Calling again Test1.bat ...
Test1.bat executed successfully.
Press any key to continue . . .
And during execution of this batch file a second console window is opened with output:
"%TEMP%\Test1.bat" executed successfully.
This second console window is closed automatically after 5 seconds.
The environment variable NoDefaultCurrentDirectoryInExePath is defined with value 0 after setting directory for temporary files as current directory with pushing current directory path on stack. The variable value does not matter because of evaluated is only existence of environment variable and not its value.
Next another batch file with name Test1.bat is created in directory for temporary files which is usually not write-protected for current user as this would cause lots of troubles.
The first approach to call Test1.bat without any path fails because of environment variable NoDefaultCurrentDirectoryInExePath is defined in local environment.
The second call of Test1.bat with relative path .\ is successful despite existence of the environment variable.
The command START ignores NoDefaultCurrentDirectoryInExePath as proven by this batch file.
Then the environment variable NoDefaultCurrentDirectoryInExePath is deleted to restore original Windows behavior.
The second approach to call Test1.bat without any path is successful now.
Finally the created Test1.bat is deleted and initial current directory is restored as current directory.
It is of course not possible to prevent execution of command DIR which is not a script file or an executable. It is an internal command of cmd.exe – Windows Command Processor – respectively of powershell.exe – Windows PowerShell.
I have wrote a batch file that i want to run another program with nircmd.exe. But the problem is i can't run it? The batch file(Matrix.bat) runs correctlyby double-click it. But when i trying to open it with nircmd.exe, it doesn't run? why?
i tried two method:
RunMethod1.bat (for runing another batch file)
SET INSTALLPATH=d:\atlantic
start %INSTALLPATH%\nircmd exec show %INSTALLPATH%\Matrix.bat
RunMethod1.bat (for runing another batch file)
SET INSTALLPATH=d:\atlantic
%INSTALLPATH%\nircmd exec show %INSTALLPATH%\Matrix.bat
The exec command in nircmd does not run batch files but executable files. Change your code to
start "" "%INSTALLPATH%\nircmd.exe" exec show "%comspec%" "%INSTALLPATH%\Matrix.bat"
Now, nircmd executes a cmd instance that will handle the batch file execution
The problem was in path of nircmd.exe. I set path of nircmd but i didn't know why it isn't work correctly? with "pushd" command i set the path of cmd into where nircmd.exe exist. and Bow!!! everything works cerrectly. Maybe a syntax problem. If everyone know that say it here.
SET INSTALLPATH=d:\atlantic
pushd %INSTALLPATH%
nircmd exec show Matrix.bat
I want to launch some .cmd file and to remain in the same directory.
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
The problem is that while executing the setDomainEnv.cmd wit about 10 other cmd files called from it, I am left in totally another directory. And I want to be where I have started. As I always start in the same directory, I am adding a cd line:
Echo on
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Echo on
cd /d C:\workspaces\DS8\swprods\dist-4.1.8-local-devel\
No effect! Again I am left in
C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server>
And I never even see the 3,4 lines to appear in the console. I could understand all that if some of the inner skripts ended with error, but they finish OK, without errors.
It would be understandable if some of the scripts changed the dir to another disk and back change would need /d key. But all is done on the same C: disk.
All operations in the script are conducted in the same source directory. The directory is changed after leaving the batch file.
....................................................
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if "Oracle" == "Apple" (set MEM
_ARGS=-Xms2048m -Xmx2048m -XX:CompileThreshold=8000 -XX:PermSize=128m -XX:MaxPermSize=256m -XX:MaxPermSize=256m )
C:\workspaces\DS8\swprods\dist-4.1.8-local-devel>if exist C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd (call "C:\Oracle\MIDDLE~1\ORACLE~1\USER_P~1\domains\WL_SER~1\bin\setStartupEnv.cmd" )
^this is the last run line of the script
"Why?" and sometimes I thought, "Wherefore?" and sometimes I thought, "Inasmuch as which? And how can I change the directory back?
Use call:
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
Call will invoke another script and after it gets completed - it will returns to your script.
If you do not use call - your execution flow goes to that another script but does not return back.
Use pushd to save the current directory then use popd to restore it after you run the other cmd file.
pushd .
call C:\Oracle\Middleware\Oracle_Home\user_projects\domains\wl_server\bin\setDomainEnv.cmd
popd
Good thing about pushd/popd is they work even if the other batch file changes the working directory to a directory on another drive.
I am trying to create a Jenkins build on a windows box, and am running into a situation where I am completely unsure of what to do.
I have the following shell script which I need to convert to a windows batch script for when the build runs. I have I believe gotten the set correct for the variables, just not sure what to do with the rest of it:
#set POWERUSER=DTCURTISS
#set POWERPASS=password
set POWERUSER=auto
set POWERPASS=password
set TAG=$BUILD_NUMBER
set PATH=.env/bin:$PATH
set SAUCEUSER=DarthOpto
set ACCESSKEY=accesskey
set IGNOREPAGEHEADERLOCATION=True
set MAINMENUCLICKTILES=False
set LOGDIR=logs
source ../.env/bin/activate
cd trunk/automation/selenium/src
#pip install -r pip-requires.txt
rm -rf logs
nosetests --nocapture --with-xunitmp -a valallmod --processes=$THREADS --process- timeout=5000
You are not going to get far if you don't know batch-file. Besides, simply "translating" bash to batch is not going to magically make it work on Windows.
Variables are referenced as %VARNAME%, not $VARNAME. Fix that first.
Windows uses \ as path separator, not /. Change all your paths to use \
When you want to append to PATH variable in Windows, you've got to make sure you don't destroy the existing PATH. You do this through set PATH=%PATH%;C:\whatever\yourpath\
Something similar to source would be batch's call followed by another batch file name. However you can't just pass it ../.env/bin/activate as that is not a batch file. You would need to convert that file to batch as well. And don't forget to convert the path separator to \. This is also where my second point comes into play. The file you pasted is rather simple. I've got no idea what's inside that other file or whether it can be "translated".
The # is not a valid comment in batch, you need to use REM or better yet ::
rm and it's flags is not a Windows command. An equivalent would be rmdir /s /q
Finally, nosetests is neither bash nor batch. It's an external program. You've got to make sure it is available in Windows. As a pre-emptive step before your next question, read this: 'nosetests' not recognized on Windows after being installed and added to PATH