Click here for important information about Computing.net.

Advice PLEASE? getting CurrentDIR into Variable in batch

December 17, 2020 at 14:35:13
Specs: Dos 7.1, I7 with 4 gig of ram
I am trying unsuccessfully I might add, in trying to get (in batch) to get my current-DIR path into a variable somewhat like %~dp0 for example in NT & then inject that path into a variable but this does not work for DOS 7.1.

I am trying to achieve this preferably in one line for example in NT again (won't work in Dos7.1)
(The echo %CurrDirName% statement below is merely for testing purposes only)

for %%I in (.) do set CurDirName=%%~nxI
echo %CurDirName%
pause

Another related question would I also need to specify LFNFOR "ON" for the benefit of the internal For command in Dos7.1 for it to work? And just to clarify it does not matter if the trailing backslash like in the example/instance of "%~dp0" for NT is included or not

Thanks in advance

David


See More: Advice PLEASE? getting CurrentDIR into Variable in batch


#1
December 18, 2020 at 22:50:25
Another question probably best solved in the programming forum. I'll try to get it moved.

Reply ↓  Report •

#2
December 19, 2020 at 05:08:34
set z=%CD%


=====================

M2


Reply ↓  Report •

#3
December 19, 2020 at 13:23:51
Hi DAVEINCAPS & Mechanix2Go,

Firstly @DAVEINCAPS my apologies I didn't really know where to post this question as my examples of my dilema are illustrated in NT CMD but what I am trying to achieve is in Dos7.1 the impression I am getting is that anything above Dos6.22 is frowned upon within the Dos forums which I think is a shame but that is my singular opinion. My intention is not to cause any problems as life is too short for that again I can only express my apologies.

Secondly at Mechanix2go can I extend my thanks for taking the time to answer my question however on several dos forums they have stated that the %CD% variable does not work on 9X dos varients although I must admit I personally have never tried it. Here is a brief extract from one of those sites below & I have just recently found this example, that is after submitting my first post:

Quote - In Windows 9x, the %CD% pseudo-variable does not exist. To get the current directory, use the following trick:

CD | Time | Find ":\" > Temp1.Bat
Echo Set CD=%%4 %%5 %%6 %%7 %%8 %%9 > Enter.Bat
Call Temp1.Bat
Del Temp1.Bat
Del Enter.Bat

The end result is a variable %CD% been returned to the batch processing session which contains the current working directory. UnQuote

Mind you the above Dos syntax is unclear to me as Find is used I do not understand the relevance of "TIME" as Time is piped through Find to create a temp bat file. Do you think this is a workable script? Any advice would be appreciated.

Best Regards,

David

message edited by jackngill


Reply ↓  Report •

Related Solutions

#4
December 19, 2020 at 14:47:14
The forums here kind of overlap anyway. I'm sure the programming people here can help you.

Reply ↓  Report •

#5
December 19, 2020 at 19:06:55
[1] Don't waste time with the jerks

[2] My windows box has UEFI BIOS and I can't get it to boot in DOS.

=====================

M2


Reply ↓  Report •

#6
December 20, 2020 at 02:43:36
@DAVEINCAPS - what do you think about the idea that I issue a terminate message in these posts, effectively end the discussion & repost my question in the Coding/programming forum and insert a link both ways so that if someone lands here they will be redirected to the programming forum & ViceVersa? I have managed to get hold of some ASM (assembly/debug code) that may help but I have no idea with ASM. But someone in the programming forum may shed some light possibly? I could delete the whole post but would need instruction & permission to do so I will co-operate in any way you want.

@Mechanix2Go - Many thanks for your reply then the code and %CD% is bogus then the %CD% does exist I will investigate further.
item [2] In respect of UEFI BIOS "No worries" DAVEINCAPS is right I may be able to get some programming code that will undertake this Batch routine which would be the Rolls Royce/best option if they see the need etc? Just as a suggestion if you want to run Dos for this or any other reasons of testing may I suggest Bochs for windows. I have just downloaded it as it is a PC emulator in an attempt to load Dos7.1 must confess not yet got it configured but onward & upward as they say

As an additional note I have found this Dos syntax but have not tried yet. It is a little bit like Mechanix2go's code SET Z=%CD% but I suspect a varient:
SET CurDir=CD %0\.. (Not sure if you can enclose this code in double quotes to circumvent spaces in possible paths like this SET Curdir=CD "%0\.." & yes after the backslash there are two full stops or dots. If you are using NT CMD then it can be adapted to CD /d %0\.. but 9x apparently does not recognise the /d switch. edging ever closer to a solution.
::Change to the batch file's drive
%0\
::Change to the batch file's directory
cd %0\..

Best Regards
David

message edited by jackngill


Reply ↓  Report •

#7
December 20, 2020 at 21:10:23
mebbe this help:
The "%CD%" is not necessarily the location of the operative script or program. So you have two different things you want. %cd% is obviously "where you are now" in the tree. The path of a script/program may or may not be the same. To determine the latter, in older versions, I think would have to first, see if %0 is in the current location (directory), then descend through the default path looking for %0. Since %0 (invocation might be filename only!) could be .bat OR .com OR .exe maybe even .vbs, maybe others, you would have to know which one you need to search for.
I guess the big question is why your program needs to know it's own origin. I would simply make sure there's only one origin, or iteration, of the program in the default path-stream. Then, there's no question, problem solved.

message edited by nbrane


Reply ↓  Report •

#8
December 20, 2020 at 21:21:28
This thread has already been moved from the DOS forum to programming so there's no need to repost.

Isn't dos 7.1 just the contents of the windows\command folder in windows 98?

I'm not a batch file person but it may be best to experiment with what you've found so far. Post back what you try and the results.


Reply ↓  Report •

#9
December 21, 2020 at 14:26:37
Hi folks,
I have managed to setup Win98SE in Vbox albeit without guest additions which is proving problematic currently as I have no way of accessing a shared folder between host and guest OS's (BTW Host OS is XP). Anyway's less of the hurdles and to the point.

I have managed to get 1 working batchfile to work with the following syntax & it works set within the root of C:\ within 98SE from the dos prompt and here it is (I might add I am NOT the author of this code but it works) it uses the prompt and temp bat files:

@echo off
REM PWD is a term used in UNix to achieve the same result CurrPath2VAR
echo @prompt set PWD=$p$_ > _temp_A.bat
%comspec% /e:2048 /c _temp_A.bat > _temp_B.bat
call _temp_B.bat
Echo Your Path is %PWD%
del _temp_?.bat
pause
goto End
:End
@Exit

I also deliberately selected "C:\Program files" as the second test as there is a 1 char space between Program & Files. It found the path and inserted within a Variable. The Echo statement and pause additions are for testing. One thing though, no good for read only media, needs read write access due to temp bat files, however it is a start. To my first attempt I can report this batch setup does NOT work but it could be due to my inadequate batch skills e.g operator error & the code it is as follows:
For /F %%a in ("%0\..") do Set HIAM="%0\.." (It simply finds the name of the bat file in this case test.bat\ & fails to set the var. So %0\.. finds the name of the bat not the path unless my code is wrong.

Anyway I will continue to test some variations I have found and report back.

One further discovery Is I have found a program called SET2VAR.EXE located within a network bootdisk which could be another way:
Mentioned Here:
https://www.dostips.com/forum/viewt...
& Found Here
http://netbootdisk.com/ (Within the network bootdisk needs extraction)
::==============================::
SET2VAR v0.2 - Freeware - By Brad Driver - Google 'SET2VAR.EXE' for help!

For helping with piping the output of a program to an environment variable
eg: >SomeApp.exe|set2var.exe VarName>TempBatchFile.bat
::=============================::
So all that would be needed would be within the operating batchfile you are running some find-myself code & pipe that through SET2VAR.EXE another avenue of possibility. I will test some more For-in-do options to replicate as near as possible NT "%~dp0"
@Nbrane -you are absolutely right I had not appreciated the diff between %CD% & %~dp0 in NT
& I guess the big question is why your program needs to know it's own origin. Ans To facilitate and share the very useful attributes that NT %~dp0 provides within backwards compatability of 9Xdos & maybe just maybe optionally through a menu or errorlevel test be backward compatable to Dos 6.22 I dunno just spitballing here
@Mechanix2go Tried to get the %CD% variable to work in 9XDos but could not get it to work

Best Regards,

David

message edited by jackngill


Reply ↓  Report •

#10
December 22, 2020 at 14:17:36
Part 2 folks,

I have now found & tested this batch file that also works but relies on find this one is for Mechanix2go as it gets the current DIr into %CD% into a variable temp bat created again No good for read only media:

:: Get Current Directory into %CD% This Batch works Win9X needs FIND
@echo off
echo.exit|%comspec% /K prompt set CD=$P$_|FIND " " > %temp%.\t1.bat
for %%? in (call del) do %%? %temp%.\t1.bat
echo CD=%CD%
pause
goto end
:end
@exit
Again echo & pause are there for testing purposes so you could say done in two lines of batch code.
Looking at it from another angle I have found a program called GETHOME.COM allegedly very fast written in assembler written about in the PcMag magazene 17th december 1991. "This little utility redirected to gohome.bat brings you right back to where you started. Here is an example of how you may use it.

@echo off
GETHOME > F:\Gohome.bat
Rem * Move to another drive & directory and do something
F:\GOHOME

If anyone is interested there is a Debug script GETHOME.SCR that will compile GETHOME.COM like this
DEBUG < GETHOME.SCR
If you type GETHOME @ the commandline you will see one line containing the current drive another & another containing the command to change to the current Directory. Redirect both of these lines to a temporary batchfile at the beginning of your batch program, then call on that temporary batchfile to get back to the place you started from. Another potential workaround if anyone wants it, let me know. Would need to set up at the beginning of the batch file to reference the starting point of the batch script.

And yet another discovery could the best yet as it works in All dos versions reputedly but again needs a temp file to operate. The program is called Lmod.com from Horst Schaeffer. This method will work under all DOS versions, including Windows NT and 2000, which do not allow the manipulation of the COMMAND environment to set variables (as done by tools like NSET).

Example: CD | LMOD set currdir=[] > %temp%\temp.bat ([] is double bracket [ ] bracket with space)
Call %temp%\temp.bat.
Del %temp%\temp.bat.

Or if using NSET again Horst Schaeffers (Not for NT though 9Xdos but Lmod is NT as well as 9XDos)
Using STDIN, examples:

by PIPE: CD | NSET CDIR=$1 (CDIR=current directory)
But as nbrane has pointed out %CD% is not the same as "%~dp0" Equiv in NT.
Was hoping for a For-In-Do one liner will keep Lukin.

Best Regards
David


Reply ↓  Report •

#11
December 24, 2020 at 07:01:22
@Mechanix2Go,

Just as a sideline & it is indirectly related to the current issue can I draw your attention to another posting on computing.net here: (& ask you some questions please)
https://www.computing.net/answers/p...
The post is in connection with CR/LF (Carriage returns and Line Feeds).
You mention using Debug I assume to remove CR/LFs & the script you use states in the description
Quote - Use DEBUG and shorten it by 2. then this
::============START=============::
@echo off > d.d

>> d.d echo n myfile
>> d.d echo l
>> d.d echo rcx
>> d.d echo 40
>> d.d echo w
>> d.d echo q

debug < d.d
::=============END==============::
I assume the quote - "use debug & shorten by 2" is in respect of the specific original OPs requirements & the Debug script removes CR/LFs to a given text file which seems to be compiled from a batchfile with Dbl appends etc. In respect of "n myfile", you would transpose the name of the text file you wish to remove the CR/LFs from? The name the CR/LF debug program you have called d.d as in debug < d.d

I apologise for asking all these ?'s but may have a use for this script specifically for the 9X scenario when getting the CWDir into a VAR. I am not really good with the usage of Debug as there seems to be ASM code therein. The second route allegedly, you can remove CR/LFs via a combination of keystrokes on your keyboard as follows. This comes from Tom Lavedas when saving the file (txt) make sure the last line doesn't end with return (CR) This is accomplished in notepad by moving the curser to just after the equal sign, (presumably a txt file with SET VAR= one liner) Holding SHIFT & then pressing Control-end, followed by DELETE & then immediately saving the result which avoids the (CR) after the "=" sign.

Your method to me seems a lot better than setting up a text file with the above mentioned keystrokes that could be subsequently be fouled up or corrupted somehow.

I can only find your reference to the debug way via the post link above that removes CR/LFs other than third party unix type ported utils. e.g. SED, VIM, TR, unix2Dos or shareware dos progs etc

& from Here useful advice using Debug
https://pement.org/sed/bat_env.htm
Solution (c): Use DEBUG to create a temporary file.
The following solution requires no user intervention, nor does it require single linefeeds in a DOS batch file. DEBUG.EXE is included in all standard distributions of MS-DOS and is in Windows 95/98 in C:\WINDOWS\COMMAND, so you can use it unless someone has deleted it.

@echo off
:: assume that the datafile exists already in ANSWER.DAT
echo e 100 "set myvar=" > TEMP.FIL
echo n PREFIX.DAT >> TEMP.FIL
for %%i in (rcx a w q) do echo %%i >> TEMP.FIL
debug < TEMP.FIL
copy PREFIX.DAT+ANSWER.DAT VARIAB.BAT
call VARIAB.BAT
echo Success! The value of myvar is: [%myvar%].
:: erase temp files ...
for %%f in (PREFIX.DAT ANSWER.DAT VARIAB.BAT TEMP.FIL) do del %%f >NUL
Many people are skittish about using DEBUG and rightfully so, since DEBUG can crash a hard drive or delete files and directories. Therefore, here is an explanation of what the previous commands actually do. I obtained these commands from Dan Gookin's Guide to Underground DOS 6.0 (Bantam Books, 1993), pp. 187-190, which tells quite a lot about using DEBUG. For the cautious user, here's what these commands mean to DEBUG:

e 100 "set myvar=" ; at base address 100, Enter this "string"
n PREFIX.DAT ; "n" means set a filename called PREFIX.DAT
rcx ; in Register CX, set the size of the file
a ; filesize A hex (10 bytes) put in register CX
w ; Write the file set by "n", i.e., PREFIX.DAT
q ; Quit DEBUG

Any advice would be appreciated ?

Best Regards,
David

message edited by jackngill


Reply ↓  Report •

#12
December 25, 2020 at 03:10:51
Hi Folks,

Just for the record MERRY CHRISTMAS & A HAPPY NEW YEAR

Best Regards,

David


Reply ↓  Report •

Ask Question