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.
Related
Lets say I want to copy a file or run a program, but the file or the location is not found. How can I let my computer pause the command window when these kind of problems occure? I made a batch file to copy files to another location, but also to run programs.
Any help will be appreciated.
In your batch script, use xcopy to do the actual copying, rather than the copy command -
xcopy, unlike copy, returns error codes depending on the result of the copy, which are documented here in the remarks section.
As Aleksandr mentioned, you can use the error codes as part of your script to pause on error.
Assuming you would like to pause on all errors, you could do script this as below:
xcopy /HECY <source> <destination>
if %ERRORLEVEL% NEQ 0 pause
Obviously you'll need to replace and in the above with the locations you are copying from and to respectively.
The /HECY switches are just an example, but in this case could be used to instruct xcopy to copy hidden files, recurse directories, and automatically overwrite files in the destination if they exist. You can tweak these to your specific needs.
Check the error code and pause if it is non zero
I am copying a large number of files during an ftp session from a remote host to my local machine. I need to save the files in a local directory tree. My problem is that a particular directory may not exist before the session. The way I have handled this in the past is to do set up my script by first creating the directory tree and not even worrying if the directory exists and then from within the same batch file (I create the batch file using Python) I start my session and use lcd to change to the correct directory
md c:\123
md c:\234
md c:\234\2009
loginname
password
cd remotedirectory
lcd c:\123
get somefile.txt
So all of the above is written out to one batch file and I start it to run. If the directory exists when I try to create it then I see a message in the terminal window that the directory exists and since nothing bad happens I have not worried about it.
What I would really like to do is check the existence of the local directory when I am ready to move to that directory and if it does not exist it gets created but I have not found out how to do this without closing the session and restarting it so I go back to the shell.
Is there a way to do this during the ftp session while maintaining the connection with the host?
You can use the ! command prefix to execute CMD MD command in order to make sure a local directory is already exist. e.g.:
! md "c:\my data\download" will create c:\data\download directory if it's not yet exist (and assuming you have the required permission). If it's already exist, it'll just display a harmless error. You can also use ! md "c:\my data\download" > nul to omit the error message. Or perform other commands.
Basically, everything after the ! ftp command prefix, will be passed to CMD.EXE as cmd /c {your command(s)}. If ! is used alone, it'll escape to CMD session prompt and wait for user input. The CMD's EXIT command will close the CMD session and return to the FTP prompt.
Right now i have a batch job I wrote that calls another file and passes in the variables that executable needs to run (password and filename).
Ex:
> cd f:\test\utils
> admin import-xml -Dimport.file=f:\DB\file1.xml -Dadmin.db.password=test123
I wrote a job that does this, but found out that there would be multiple files.
The username and password never change but the filename differs for like 15 different xml files--with maybe more coming soon.
The files will always be located in the same folder. Instead of ending up with like 15-20 jobs (one for each file), can I write something that will process each file located in this directory. And either wait till one is completed before the next or I can add a 3 min sleep before it starts the next file.
pushd C:\test\utils
for %%F in (F:\DB\*.xml) do (
admin import-xml "-Dimport.file=%%~dpnxF" -Dadmin.db.password=test123
)
popd
The %%~dpnxF expands to drive, path, basename and extension of the current file.
If you intend to set and use environment variables (%foo%) in that loop, read help set first before you get into trouble.
You can use the for command. Something like this in a batch file:
for %%f in (*.xml) do call myotherbatch.bat %%f
Assuming that the admin command you are running doesn't return until the job is finished, the above loop would process them sequentially.
If you run the command at the prompt (as opposed to in a batch file), only use a single %.
for file in f:\DB\*
do
admin import-xml -Dimport.file="$file" -Dadmin.db.password=test123
done
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
i tried looking at the other questions regarding this, but no go. i've tried a straight call to the other bat file ("otherBat.bat," for instance), a "call" command, and even a "start" command. all of these are failing though, and i'm at a loss as to why. both .bat files are within the same folder, and i'm not changing directories, so i don't know what the problem is...
any help on this would be much appreciated ^_^
edit: sorry, here's the code :)
primary.bat:
echo Test run...enter variable1
set /p var1=:
echo Test run...enter variable2
set /p var2=:
call other.bat %var1% %var2%
pause
other.bat:
echo Working!
pause
You should either cd to the current directory in your first batch file or call the second batch file by full path.
Is the second .bat file in your path? What happens if you change your first .bat file to call it using an absolute path?
use the absolute path :
::prototype
CALL [drive:][path]filename [parameters]
::example
call C:\Users\theUserName\path-to-your-file\the-file-name.bat %your-variables-to-pass%
cf the call documentation from ss64.com
Absolute path may be replaced with %~dp0\, which means use current script's path instead of runtime context; in your case call %~dp0\other.bat %var1% %var2%.
Had same issue when running as administrator from context menu – call tried to execute batch file in System32 instead of parent BAT folder.