I have an old 386 computer (without windows) which has MSDOS 6.22. So I cannot use any solution that built on cmd.exe (part of windows)
I want to pass current bat script path and name to another program within this bat code.
I try to use %CD%
but it looks like works only with cmd.exe
I try to use %0
argument, but it hold only the name of bat instead of name with full path
@echo off
set myPath=%cd%
myprogram.exe %myPath%\%0
It doesn't work. The passed parameter is \mybat.bat
when I started the bat program from it's directory with full name.
After the variables resolved, I want to something like this:
@echo off
myprogram C:\BATCH\MYBAT.BAT
Where the bat placed in c:\batch
directory, and it name is mybat.bat
How can I do this?
The solution of @Stephan is good, but requires a predefined helper file.
But you can also build a solution which doesn't need any predefined files.
@echo off
>temp1.bat echo @PROMPT SET _CD=$P
>temp2.bat command /c temp1.bat
call temp2.bat
del temp1.bat
del temp2.bat
echo currentDir=%_CD%
This can be used to get the current directory $P
, time $T
or date $D
, as these values are supported by the PROMPT command.
The trick is to create a secondary batch file with just one command: set VARIABLE=somestring
. We achieve that by concatenating a prepared file with set VARIABLE=
(important: without a line feed) and another file with the desired value.
@echo off
cd>temp.txt
copy init.txt+temp.txt setvar.bat
call setvar.bat
myprogram.exe "%VARIABLE%\mybat.bat"
To create init.txt
(you need to do that only one time (reusable file)):
Type COPY CON INIT.TXT
on the command prompt.
Enter @SET VARIABLE=^Z
(press Ctrl-Z to generate ^Z
)
Sadly I don't know a way to create a line without a line feed with code in DOS)
Jeb answered with an interesting solution to get the working folder, but this one is more generic; it works with the output of any command (as long as it's one line only).
Just for example, replace cd>temp.txt
with dir | find "bytes free" > temp.txt
in above code.
(answer derived from an old answer of mine)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With