Problem with variable Drive setup for XP/W98SE

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
ispy
Posts: 16
Joined: 25 Dec 2020 15:36

Problem with variable Drive setup for XP/W98SE

#1 Post by ispy » 03 Feb 2021 05:54

Hi everyone :D ,
Pc SPEC:
Current OS WinXPSP3 working within CMD.EXE & I have also tried to use Command.com from CMD window. PC is Small-Form Factor Intel core i7 vPro with 8 gig ram usb non-wireless mouse setup on dual boot "grub for dos" non-bootsector dual boot mechanism with Q4OS Linux OS. I have portable Vbox installed off usb drive with Win98SE & ME

- I have tried googling for a similar problem without much luck
- I have not yet tried within W98SE Vbox but first it needs to work within XP first (possibly Win3.1?)
- I have tried to examine the script with echo on instead of echo off
- I have tried to break the script down into stages & stopped the script from deleting the temp files it makes to see what is within these temp bat files.
- Have tried to echo the variable upon completion to see if it is defined & the answer is no

Can someone shed some light on what I am doing wrong with a small elementary batch script that I am trying to compile. I'm sure it is something simple that I am missing & will KICK myself when I discover what but the problem has got me confounded.

The batch script (IF possible needs to work in XP as well as Win98SE [& a luxury would be Win3.1]) Here is the batch script with comments to try to explain what I am trying to do with the script?

@ECHO ON (Used to see whats going on with the batch script will @echo off when fixed)
SET DRV= (Used to clear any previous Drive: Vars so starting from fresh each time)
ECO.COM SET DRV=>T1.BAT (ECO.COM is a debug echo script that permits not having CRLF)
ECHO PROMPT $N:>T2.BAT ($N: is a Prompt for Drive: not $p$g standard prompt redirected to T2 temp bat
COPY T1.BAT+T2.BAT T3.BAT>NUL (Joins 2No temp bats to form third temp bat with copy)
CALL T3.BAT (Calls T3 temp bat with one line of SET code)
:: DEL T?.BAT (Deletes all three temp bats to cleanup)
ECHO Your Current Drive is %DRV% Tests if the variable is defined the answer is no)
pause (Pause is used to retain info on the screen when script completes)

T1.BAT has the following text therein SET DRV=
T2.BAT has the following text therein PROMPT $N:
T3.BAT lastly has this text before being called SET DRV=PROMPT $N:
Is the newline carriage return square box thingy on next line

Within the CMD window I get the following text:
Your Current Drive is <Nothing>
Press any key to continue . . . <cursor>

What this script is supposed to do is get the Drive: prompt into a variable e.g. "C:" (without quotes) but it ain't happening at my end? Some help would be appreciated, thanks in advance

P.S. Oh by the way & as feedback in respect of "How to get help for a batch script - quickly!" the links within the post do not work for me I am using Palemoon which is firefox browser on XP. Could someone check to see if its just me with my browser or are the links not going anywhere?

P.S2 - I have tried to establish this script from the command prompt. If for instance I type at the command prompt "PROMPT $N:" & hit the <ENTER> key this works manually typing it in manually however within batch it is failing, any ideas Please? I have also tried Echoing prompt & using a key-stuffer to simulate the <ENTER> but again this fails?

Regards
ispy

pieh-ejdsch
Posts: 240
Joined: 04 Mar 2014 11:14
Location: germany

Re: Problem with variable Drive setup for XP/W98SE

#2 Post by pieh-ejdsch » 07 Feb 2021 06:13

Hi,
I think its so (untestet)

Code: Select all

echo exit |command /k prompt set drive=$N |find "set" >Tempdrv.bat
echo.>>Tempdrv.bat
Call Tempdrv
Echo %drive%
Phil

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Problem with variable Drive setup for XP/W98SE

#3 Post by Samir » 10 Feb 2021 11:19

Someone correct me if I'm wrong, but you can use

Code: Select all

%~d0
to get the drive letter the batch is currently being run from. I can't recall if this worked this way in 95/98/ME, but I don't think it worked this way in DOS 6.0 and earlier.

So in your batch this line

Code: Select all

ECHO PROMPT $N:>T2.BAT ($N: is a Prompt for Drive: not $p$g standard prompt redirected to T2 temp bat
would change to

Code: Select all

ECHO %~d0>T2.BAT
Try it and let me know what happens. I am only an amateur compared to the pros here, but I've been working with DOS since the DOS days cutting my teeth on DOS 3.3 so hopefully I remember this right!

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Problem with variable Drive setup for XP/W98SE

#4 Post by penpen » 10 Feb 2021 18:18

Samir wrote:I can't recall if this worked this way in 95/98/ME
The command extension "%~d0"-syntax exclusively works for "cmd.exe" and doesn't work under "command.com", so it won't work under Win9x.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Problem with variable Drive setup for XP/W98SE

#5 Post by Samir » 10 Feb 2021 18:42

penpen wrote:
10 Feb 2021 18:18
Samir wrote:I can't recall if this worked this way in 95/98/ME
The command extension "%~d0"-syntax exclusively works for "cmd.exe" and doesn't work under "command.com", so it won't work under Win9x.
Thank you for the correction penpen! There were certainly more limits back in the day, that's for sure.

Samir
Posts: 384
Joined: 16 Jul 2013 12:00
Location: HSV
Contact:

Re: Problem with variable Drive setup for XP/W98SE

#6 Post by Samir » 11 Feb 2021 14:45

I remembered another way to get the current drive and path--and it was simple and I used to use it so much back in the day

Code: Select all

CD
Without any options it will simply display the present working drive with full path.

miskox
Posts: 633
Joined: 28 Jun 2010 03:46

Re: Problem with variable Drive setup for XP/W98SE

#7 Post by miskox » 12 Feb 2021 07:02

Maybe this could help:

https://www.robvanderwoude.com/batchfiles.php

https://www.robvanderwoude.com/batexamples.php

https://www.robvanderwoude.com/files/bootdriv_dos.txt

Code: Select all

@ECHO OFF
:: BOOTDRV.BAT
:: Places the boot drive letter in environment variable BOOTDRV
::
:: Written by Rob van der Woude
:: http://www.xs4all.nl/~robw/rob/
::
:: Limitation: Uses COMSPEC to determine boot drive, so if a different
::             command processor was specified by either the SHELL
::             command or by a SET COMSPEC=... command, this batch file
::             will be fooled and display the wrong drive.
::
SET BOOTDRV=
ECHO %COMSPEC% | CHOICE /C:ABCDEFGHIJKLMNOPQRSTUVWXYZ > NUL
IF ERRORLEVEL  1 SET BOOTDRV=A
IF ERRORLEVEL  2 SET BOOTDRV=B
IF ERRORLEVEL  3 SET BOOTDRV=C
IF ERRORLEVEL  4 SET BOOTDRV=D
IF ERRORLEVEL  5 SET BOOTDRV=E
IF ERRORLEVEL  6 SET BOOTDRV=F
IF ERRORLEVEL  7 SET BOOTDRV=G
IF ERRORLEVEL  8 SET BOOTDRV=H
IF ERRORLEVEL  9 SET BOOTDRV=I
IF ERRORLEVEL 10 SET BOOTDRV=J
IF ERRORLEVEL 11 SET BOOTDRV=K
IF ERRORLEVEL 12 SET BOOTDRV=L
IF ERRORLEVEL 13 SET BOOTDRV=M
IF ERRORLEVEL 14 SET BOOTDRV=N
IF ERRORLEVEL 15 SET BOOTDRV=O
IF ERRORLEVEL 16 SET BOOTDRV=P
IF ERRORLEVEL 17 SET BOOTDRV=Q
IF ERRORLEVEL 18 SET BOOTDRV=R
IF ERRORLEVEL 19 SET BOOTDRV=S
IF ERRORLEVEL 20 SET BOOTDRV=T
IF ERRORLEVEL 21 SET BOOTDRV=U
IF ERRORLEVEL 22 SET BOOTDRV=V
IF ERRORLEVEL 23 SET BOOTDRV=W
IF ERRORLEVEL 24 SET BOOTDRV=X
IF ERRORLEVEL 25 SET BOOTDRV=Y
IF ERRORLEVEL 26 SET BOOTDRV=Z
IF     "%BOOTDRV%"=="" ECHO Error checking boot drive
IF NOT "%BOOTDRV%"=="" ECHO Boot drive is %BOOTDRV%:

https://www.robvanderwoude.com/amb_cdrom.php

https://www.robvanderwoude.com/bootdrive.php

https://www.robvanderwoude.com/sourceco ... rdrive_dos

10 or 15 years ago there was a site maintained by Frank Peter Schultze (fpschultze.de ?) . Looks like this might be a copy: fpschultze.blogspot.com (didn't check it).

Saso

ispy
Posts: 16
Joined: 25 Dec 2020 15:36

Re: Problem with variable Drive setup for XP/W98SE

#8 Post by ispy » 15 Feb 2021 06:34

Hello all just Posting feedback on the replies to my initial post many thanks for taking the time and trouble to post your replies, it is appreciated!,

@pieh-ejdsch(Phil) - Tried your script albeit within Vbox/Win98SE & the script did not work, all that was returned was a flashing cursor at the command prompt no temp were generated Tempdrv.bat file etc.
@Samir %~d0 does not work in Win9X as Penpen has stated (Shame it doesn't though CMD.exe only) Also in respect of CD command that is good for Drive+Path but would need further steps to process the resultant redirected text file so as to only include the Drive letter.
@miskox - Tried your script within Win9x & can confirm it works (there is the dependency of Choice & quite a lot of code to get the desired result) It will not work in XP probably due to %COMSPEC% & choice but is the best working answer so far.

I think this is a prime example of what you hope for and what you get are 2 different things in a practical sense. I have been under the mistaken impression that whatever you type at the command line could be translated into a batch-file. Typing PROMPT $N: at the command prompt then hitting the <enter> key permits getting the Drive Letter: but simulating the <enter> depress key (with a key-stuffer for e.g) & then > redirecting the Stdout into a appended text file is a bridge too far. Its a shame there is not a small debug/ASM script that would undertake this feature but would be a further dependency again.

Best regards
Ispy

miskox
Posts: 633
Joined: 28 Jun 2010 03:46

Re: Problem with variable Drive setup for XP/W98SE

#9 Post by miskox » 16 Feb 2021 02:43

Choice.exe is not present in Windows XP. So there must be another solution to solve this issue. I might try it but currently I don't have access to XP computer. Did you check other links? I think there should be a solution already posted that would do the job.

Saso

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Problem with variable Drive setup for XP/W98SE

#10 Post by penpen » 16 Feb 2021 05:34

ispy wrote:
15 Feb 2021 06:34
@pieh-ejdsch(Phil) - Tried your script albeit within Vbox/Win98SE & the script did not work, all that was returned was a flashing cursor at the command prompt no temp were generated Tempdrv.bat file etc.
I thought that should work pretty well, so i tested that script:
On Win98SE (no matter if you are using Vbox or native) it should work pretty well, except that it should set the environment variable "drive" to "<correct drivename> exit".

There are two minor bugs in the script from Phil (a missing $_ to put exit into the next line and instead of using "command" using %comspec% to enable that script to work on all windows versions):

Code: Select all

echo exit | %ComSpec% /k prompt set drive=$N$_ |find "set" >Tempdrv.bat
echo.>>Tempdrv.bat
Call Tempdrv
Echo %drive%
However, the script sets up the environment variable "drive" and echoes it, so even if you double-click it only (you might miss the output and can't access the variable after that) you should have the file "Tempdrv.bat" created in the actual directory unless your current directory is write protected. In that case, you might want to create and "%temp%\Tempdrv.bat" instead.



penpen

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Problem with variable Drive setup for XP/W98SE

#11 Post by jeb » 16 Feb 2021 06:00

You could try this one, it works on win98 (tested) and should work on XP, too

Code: Select all

@echo off
if "%1" == "" goto :main
goto %1

:main
set drv=unknown
%COMSPEC% /c %0 :getdrv > drvtmp.bat
call drvtmp.bat
echo my drive is: %drv%
goto :eof

:getdrv
prompt set drv=$N
echo on
%=must be empty=%
@echo off

:eof

ispy
Posts: 16
Joined: 25 Dec 2020 15:36

Re: Problem with variable Drive setup for XP/W98SE

#12 Post by ispy » 16 Feb 2021 17:26

Hi Folks :D ,

I think I may have a solution to this problem I have adapted a script which I have found on PC-Mag article which I think was in the main for 9X but it seems to work reasonably well in XP as well even though it uses command.com rather than native CMD (& one other caveat it reports back in SFN format also). Maybe some experts could perfect the script even further to iron out the wrinkles as I am not really competent enough in batch/CMD to undertake this but here is the basic skeleton. What the batch file does is sets both your current drive and your current path to variables here it is in raw format: It works in XP as well as Windows98SE Dos/CMD.

Code: Select all

@ECHO OFF
IF NOT "%2"=="" GOTO :TWOTime
ECHO @PROMPT %0 $n: $p > GD$1.BAT
COMMAND /E:2048 /C GD$1.BAT > GD$2.BAT
GD$2.BAT

:TWOTime
DEL GD$?.BAT
SET Drv=%1
SET CDir=%2
ECHO YOUR DRIVE IS %Drv%
ECHO YOUR CURRENT PATH IS %CDir%
pause
From the batch file you could comment out the DEL GD$?.BAT like this :: DEL GD$?.BAT if you want to see what is inside the temp bat files GD$1 & GD$2.BAT files for a better understanding of the mechanics of the resultant script

For me GD$1.bat temporary bat contains the following text yours will be different depending on your paths etc

Code: Select all

@PROMPT "C:\Documents and Settings\ADMIN\Desktop\RECENT\DCD.BAT" $n: $p
And GD$2.BAT temporary bat contains the following text:

Code: Select all

"C:\Documents and Settings\ADMIN\Desktop\RECENT\DCD.BAT" C: C:\DOCUME~1\ADMIN\DESKTOP\RECENT
As you can see SFN's are the order of the day. I tried using %COMSPEC% instead of command.com but it crashed in XP. I have only put the pause command to view the contents of the variables by holding the Dos Prompt & CMD window open.

Many Thanks Penpen & Jeb for your batch script contributions, I will try your scripts tomorrow and get back to you with any observations that may arise within 9X & XP, to me the more variations the better particularly from the perspective of others who may visit the site looking for a similar solution. To me it is useful to setup your drive and current path as variables, very useful but for me not so easy to setup though.
Edit P.S. - Following a comment by Samir @ Post #6, One other thing I was experimenting with was the CD command as that will on it's own with a redirect give you your path something like CD>T1.BAT So I experimented with CD\ CDBackslash with a redirect like CD\>T1.BAT as CD Backslash gives you your root Drive but the introduction of the back slash causes the script to fail for some reason. However I can report that CD\ works in both Win XP & Windows98SE manually typed in. It seems to fail in batch script with a reported error in respect of the backslash possibly some other batch code is required?
Best Regards,

Ispy

miskox
Posts: 633
Joined: 28 Jun 2010 03:46

Re: Problem with variable Drive setup for XP/W98SE

#13 Post by miskox » 17 Feb 2021 02:57

@ispy:

did you try (in 9X, Xp...) this:

Code: Select all

echo %cd% >temp.file
instead of

Code: Select all

cd>temp.file
(can't test this right now)

Saso

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Problem with variable Drive setup for XP/W98SE

#14 Post by jeb » 17 Feb 2021 03:30

Hi miskox,

Code: Select all

echo %cd%
tested, it works in XP, but not in w98.

That was expected, because XP has nearly the same features and bugs like W10.

It could be a good solution to write different code for XP and W98 and detect which part has to be used.

Something like

Code: Select all

@echo off
>NUL echo dummy & goto :XP
REM Code for W95
...

:XP

ispy
Posts: 16
Joined: 25 Dec 2020 15:36

Re: Problem with variable Drive setup for XP/W98SE

#15 Post by ispy » 17 Feb 2021 07:11

Just reporting back with feedback :D
@ Saso As Jeb has stated %CD% is affiliated to Win-XP as is "cd /d" also (without quotes & just for info) these nuances are a little bit confusing, when attempting to write batch files for NT & Dos.
Anyway - @ Penpen tried your script in XP it very briefly opened the command prompt then disappeared even with a pause command within it as you mentioned. Then as you stated, tried it with %temp%\ as well same result. Tried enclosing in "" quotes same result.
Then I went to 98SE and it worked with the double quotes and the %temp%\ addition with and without quotes. It reported back: Your drive is C: press any key to continue... So I then removed the %temp%\ & quotes it still works in 98SE. If you are wondering about the colon after "C" I added it just as a test within "prompt set drive=$N:$_" line. You could also add the colon after the echo Your Drive is %drive%: line but the variable would read "C" only so its a visual not actual effect.
@ Jeb - First tried within Win XP & it kinda works, the reported message was My drive is: unknown press any key to continue... However when I used within Win98SE it worked reporting Your drive is C: press any key to continue... I also looked within the Tempdrv.bat file set drive=C: It seems it is experiencing trouble setting the variable within XP CMD for some reason but works well in 98SE?
If anyone knows of a workaround for CD redirect to bat file and CD\ redirect to bat file these could be used to glean root drive and current path but again could result in writing many lines of code to work around the back slash error that XP reports? (CD & CD\ work both in XP & 98SE)

Best Regards
Ispy :D

Post Reply