[comp.sys.ibm.pc.programmer] Current directory from batch file

MCCABE@MTUS5.BITNET (Jim McCabe) (05/03/90)

I'm sure this is a simple question, but I've only recently started writing
batch programs that had any complexity at all.  I would have avoided it
if possible, but my job demands it.  :(

Is there any way to find out what the current directory (and current drive
for that matter) is from within a batch file?  I am working on a program
that changes directories all over the place, and it would be very nice
to be able to place the user back in the original directory before terminating.

In Unix there is an environment variable that always holds the current
working directory, but the manual doesn't seem to say anything about it.
Can anyone help me out at all on this one?  I'd rather keep it all
within a single program, if possible.  We're using DOS 3.30 here.

Thanks!


                                                Jim McCabe
                                                MCCABE @ MTUS5.BITNET

lubkt@vax1.cc.lehigh.edu (05/04/90)

In article <90122.175401MCCABE@MTUS5.BITNET>, MCCABE@MTUS5.BITNET 
(Jim McCabe) writes: 

> I'm sure this is a simple question, but I've only recently started writing
> batch programs that had any complexity at all.  I would have avoided it
> if possible, but my job demands it.  :(
> 
> Is there any way to find out what the current directory (and current drive
> for that matter) is from within a batch file?  I am working on a program
> that changes directories all over the place, and it would be very nice
> to be able to place the user back in the original directory before
> terminating. 
> 
> In Unix there is an environment variable that always holds the current
> working directory, but the manual doesn't seem to say anything about it.
> Can anyone help me out at all on this one?  I'd rather keep it all
> within a single program, if possible.  We're using DOS 3.30 here.
> 
> Thanks!
>                                                 Jim McCabe
>                                                 MCCABE @ MTUS5.BITNET

You can use the "apply" command. I know for sure that "apply" is
available on MS-DOS V3.2 and V3.21, but as I recall it is no longer
supported. I might be mistaken, so check it out.

The syntax of "apply" is

  	apply <filename> "command" [/S]

If an  input file, say INPUT.FIL, contains  the text  "C:\DOSDIR", the
following  command will execute  the command equivalent to  typing "cd
c:\dosdir":

	apply input.fil "cd %"

If you are including  the above command in a  batch-file,  you have to
put and extra `%' character, as is shown below for the above command:

 	apply input.fil "cd %%"

You can use the "cd > input.fil" or the "cd d: > input.fil" command to
store the path name in input.fil.

P.S.: If your version does  not have the  "apply" command, try getting
it from somewhere. It is an external command and does not complain too
much about versions.

-Binod.
-------------------
Binod Taterway, User Consultant,                      
Lehigh University Computing Center    BT00@lehigh.BITNET 
Bethlehem, PA 18015                   LUBKT@vax1.cc.lehigh.EDU (Internet)
Disclaimer: I disclaim nothing; I think I ought to be responsible for
whatever I say.

grimlok@hubcap.clemson.edu (Mike Percy) (05/04/90)

From article <90122.175401MCCABE@MTUS5.BITNET>, by MCCABE@MTUS5.BITNET (Jim McCabe):
> Is there any way to find out what the current directory (and current drive
> for that matter) is from within a batch file?  I am working on a program
> that changes directories all over the place, and it would be very nice
> to be able to place the user back in the original directory before terminating.
 
I am assuming you want to do something like
cd \somedir\someotherdir
program.exe
go back to first directory?

There are the pushdir and popdir functions I have seen on the net and in
various DOS utility books (e.g. PC World's Power DOS Tools), which work
well:
pushdir  /* save current dir */
cd \dir\dir   
program.exe
popdir   /* go back */
 
But the pushdir/popdir functions I have seen are either TSR's or use a
file somewher to store the directory stack.  I'd rather not do either.

DOS has a function worth thinking about - subst
subst t: \dir\dir
t:
program.exe
c:        
subst t:
 
This works well for me, since I use c: as my "home" directory, and I
seldom would have a pushdir stack more than one level deep.

Mike Percy

cca10@cl.cam.ac.uk (Christopher Anderton) (05/05/90)

The solution I use for this is as follows ...

PATH >c:\savepath.bat
...
...  code here
...
call c:\savepath
del c:\savepath.bat

I use DOS V4.00 on PS2/50Zs.

Chris Anderton
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Chris Anderton :: CCA10@UK.AC.CAM.PHX  "Meaningless, Meaningless", said the
               :: CCA10@UK.AC.CAM.CL   Teacher. "Utterly Meaningless! Everything
The above are JANET addresses.         is Meaningless."

sun@me.utoronto.ca (Andy Sun Anu-guest) (05/07/90)

In article <1873@gannet.cl.cam.ac.uk> cca10@cl.cam.ac.uk (Christopher Anderton) writes:
>The solution I use for this is as follows ...
>
>PATH >c:\savepath.bat
>...
>...  code here
>...
>call c:\savepath
>del c:\savepath.bat
>
>I use DOS V4.00 on PS2/50Zs.
>
>Chris Anderton
>-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
>Chris Anderton :: CCA10@UK.AC.CAM.PHX  "Meaningless, Meaningless", said the
>               :: CCA10@UK.AC.CAM.CL   Teacher. "Utterly Meaningless! Everything
>The above are JANET addresses.         is Meaningless."

I thought the above will only reset the path, but will not bring you
back to the right directory, regardless of your hardware and software.
I thought what the original poster asked was:

(1) say, you are in C:\DIR1
(2) you execute a batch file which have a lot of CDs so that at the end
    of the batch file, you end up in, say, C:\DIR2\DIR3\DIR4.
(3) upon terminate of the batch file, you need to go back to C:\DIR1
    automatically.

I don't think what you said above can accomplish this task.

I couldn't think of any combination of genuine DOS commands that can 
accomplish that task. However, if you have one of those public domain
PC U*IX "pwd" and "sed" programs handy, then you can do something similar 
to the above, like:

pwd | sed -s 's/^/cd /' > c:\savedir.bat
...
... code here
...
c:\savedir.bat
del c:\savedir.bat

I don't like this either because it's like I am talking about a different
operating system (someone might say, if you use pwd and sed, why not
use pushd and popd...). 

If the original poster doesn't mind a bit of programming, a few (< 10) lines
of C code can get the current directory and construct and echo the string
"CD <current path and directory>". Say, if you call it SHOWDIR.COM, in
your batch file, you can simply do

showdir > c:\savedir.bat
...
... code here
...
c:\savedir.bat
del c:\savedir.bat

If you will change to directories in a different drive, then the above
won't work either. You'll have to grab and display the drive name in your
program also, something like:

<drive name>:
CD <current path and directory>

Hope this helps and sorry for this long message.

Andy

raymond@pepto-bismol.berkeley.edu (Raymond Chen) (05/08/90)

In article <90May7.072047edt.19230@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>I couldn't think of any combination of genuine DOS commands that can 
>accomplish that task.

Closing my eyes and thinking real hard, I came up with this:

Create a file that contains three characters. "c" "d" <space>.
No trailing CR+LF!  Put it in \usr\lib\cdspace

Your batch file does this:

	copy \usr\lib\cdspace \tmp\cdspace.bat
	cd >>\tmp\cdspace.bat
	... do whatever you want ...
	\tmp\cdspace

Doesn't use the "call" command, so should even work on DOS 2.0.
If you're clever, you can even teach cdspace.bat to erase itself when
it's done, but it's no big deal leaving it in the \tmp directory,
since everybody has a "del \tmp\*.*" in their autoexec.bat, right? :-)

sun@me.utoronto.ca (Andy Sun Anu-guest) (05/08/90)

In article <1990May8.003238.6922@agate.berkeley.edu> raymond@pepto-bismol.UUCP (Raymond Chen) writes:
>In article <90May7.072047edt.19230@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>>I couldn't think of any combination of genuine DOS commands that can 
>>accomplish that task.
>
>Closing my eyes and thinking real hard, I came up with this:
>
>Create a file that contains three characters. "c" "d" <space>.
>No trailing CR+LF!  Put it in \usr\lib\cdspace
>
>Your batch file does this:
>
>	copy \usr\lib\cdspace \tmp\cdspace.bat
>	cd >>\tmp\cdspace.bat
>	... do whatever you want ...
>	\tmp\cdspace
>
>Doesn't use the "call" command, so should even work on DOS 2.0.
>If you're clever, you can even teach cdspace.bat to erase itself when
>it's done, but it's no big deal leaving it in the \tmp directory,
>since everybody has a "del \tmp\*.*" in their autoexec.bat, right? :-)

You've told half of the story, but what about the other half? I am
interested in how you create that \usr\lib\cdspace file. I don't know
of any smart way to get this done except by a line of C code:

	main() {printf("cd ");}

Is there editor/word processor out there that can kill CR+LF?

BTW, "cd>>\tmp\cdspace.bat" was neat. I didn't know you can do it this way
before.

Andy

dmurdoch@watstat.uwaterloo.ca (Duncan Murdoch) (05/08/90)

In article <90May7.215707edt.19841@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>
>You've told half of the story, but what about the other half? I am
>interested in how you create that \usr\lib\cdspace file. I don't know
>of any smart way to get this done except by a line of C code:
>
>	main() {printf("cd ");}


It's occasionally handy (as here) to be able to echo text without appending
a CR/LF, so a few years ago I wrote a tiny 23 byte assembler program to do it.
The .DOC file is attached below.

Duncan Murdoch

-----------------------------

EchoN - Echo command line without going to a new line.
  by D.J. Murdoch, 13 Nov 1987
 
This handy little program works like ECHO, but doesn't do a new line at the end.  
I use it in batch files to save a directory: 
 ECHON cd >\olddir.bat 
 CD      >>\olddir.bat
This puts the "cd " into the file olddir.bat in the root directory, then lists 
the current directory into that file on the same line.  Executing OLDDIR any 
time after that puts you back into the original directory.  Unlike PUSHDIR and 
POPDIR, this lasts after you reboot your computer.
 
Here's source code, that could be assembled using DEBUG if ECHON.COM is missing: 
MOV     CL,[0080]    ; Put line length into CL
SUB     CX,+01       ; Subtract 1 to get rid of initial blank
JL      0115         ; Quit if there were no characters
MOV     BX,0001      ; Output to Standard Output...
MOV     DX,0082      ; from command line, starting at the 2nd char...
MOV     AH,40        ; using the write service...
INT     21           ; do it!
INT     20           ; quit

richard@calvin.spp.cornell.edu (Richard Brittain) (05/08/90)

In article <90May7.215707edt.19841@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>
>You've told half of the story, but what about the other half? I am
>interested in how you create that \usr\lib\cdspace file. I don't know
>of any smart way to get this done except by a line of C code:
>
>	main() {printf("cd ");}

Freemacs has no problem creating files with no trailing CRLF.          

Richard Brittain,                   School of Elect. Eng.,  Upson Hall   
                                    Cornell University, Ithaca, NY 14853
ARPA: richard@calvin.spp.cornell.edu	
UUCP: {uunet,uw-beaver,rochester,cmcl2}!cornell!calvin!richard

wilber@nunki.usc.edu (John Wilber) (05/08/90)

In article <90May7.215707edt.19841@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun
Anu-guest) writes:

>Is there editor/word processor out there that can kill CR+LF?

Sure.  Piece of cake.  I would suggest the Epsilon text editor by Lugaru
Software, the Norton Editor by Peter Norton Computing, or any disk
sector editor, like the Norton Utilities (look at the sector with the
file on it, and change the C-LF sequence to spaces, or delete them).

/***********************************************************************\
* John J. Wilber           * "Im Himmel gibts kein Bier zum trinken wir *
* wilber@nunki.usc.edu     *  es hier"                  -German Proverb *
* Student, partier, beer   * "In heaven there's no beer, so we might as *
* drinker, fun-loving guy. *  well drink it here"          -Translation *
*************************************************************************
* "I woke up this morning and I got myself a beer"           -The Doors *
\***********************************************************************/

>Andy

draper@cpsin1.uucp (Patrick J Draper) (05/08/90)

>You've told half of the story, but what about the other half? I am
>interested in how you create that \usr\lib\cdspace file. I don't know
>of any smart way to get this done except by a line of C code:
>
>	main() {printf("cd ");}
>
>Is there editor/word processor out there that can kill CR+LF?
>
>BTW, "cd>>\tmp\cdspace.bat" was neat. I didn't know you can do it this way
>before.
>
>Andy

I noticed that some others suggested using Norton's editor or some other
package, but if you don't have those, you can use the dos copy con:
command.

I tried this and it didn't put a CR+LF into the file.

ex.
-> copy con: testfile.txt
-> cd  <press F6 key here, don't press return>

I got a file with a length of 3, with cd <space>.

Patrick Draper   --- Michigan State University

tjh@lance.hss.bu.oz.au (Tim Hudson) (05/09/90)

In article <90May7.072047edt.19230@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
>I thought what the original poster asked was:
>(1) say, you are in C:\DIR1
>(2) you execute a batch file which have a lot of CDs so that at the end
>    of the batch file, you end up in, say, C:\DIR2\DIR3\DIR4.
>(3) upon terminate of the batch file, you need to go back to C:\DIR1
>    automatically.
>I couldn't think of any combination of genuine DOS commands that can 
>accomplish that task. 

	Well, how about this sort of trick:

1. 	Have a file with just a "cd " in it - NO newline on the end (must
	make sure of this)
	call this file \cd.dat

2.	in your batch file do the following

	copy \cd.dat \tmpname.bat
	cd >> \tmpname.bat
	.
	.
	do all your other things
	.
	.
	\tmpname

	That works perfectly under MS-DOS 3.30 at least.

	Basically, cd with no args gives current working directory - make
a batchfile that will take you back there - hence need a cd with no newline.
The files should be in a fixed location as you need to know where they are.
Similar things can be done to save the path etc. Using this you can
have a one level pushdir and popdir written as batchfiles.

Tim Hudson
--
Language Centre              internet    : tjh@lance.hss.bu.oz{.au}
Bond University              JANET       : tjh%lance.hss.bu.oz@uk.ac.ukc
Gold Coast, Qld 4229         ARPA, bitnet: tjh%lance.hss.bu.oz.au@uunet.uu.net
Australia                    UUCP        : ..!uunet!munnari!lance.hss.bu.oz!tjh
-- 

svirsky@ttidca.TTI.COM (Bill Svirsky) (05/09/90)

In article <90May7.072047edt.19230@me.utoronto.ca> sun@me.utoronto.ca (Andy Sun Anu-guest) writes:
+I thought what the original poster asked was:
+
+(1) say, you are in C:\DIR1
+(2) you execute a batch file which have a lot of CDs so that at the end
+    of the batch file, you end up in, say, C:\DIR2\DIR3\DIR4.
+(3) upon terminate of the batch file, you need to go back to C:\DIR1
+    automatically.
+
+I couldn't think of any combination of genuine DOS commands that can 
+accomplish that task.

Suppose you have a file, call it 'cd.cmd', that contains just 'cd ' (no
end-of-line). Then you could do something like:
copy c:\cd.cmd c:\savedir.bat
cd >> c:\savedir.bat
...
... batch commands here
...
c:\savedir.bat

To get a file, such as 'cd.cmd', that doesn't contain an end-of-line, use:
copy con c:\cd.cmd
cd ^Z<enter>

-- 
Bill Svirsky, Citicorp+TTI, 3100 Ocean Park Blvd., Santa Monica, CA 90405
Work phone: 213-450-9111 x2597
svirsky@ttidca.tti.com | ...!{csun,psivax,rdlvax,retix}!ttidca!svirsky

bwilliam%peruvian.utah.edu@cs.utah.edu (Bruce Williams) (05/10/90)

   There have been many postings of how to do this using simple,
unadulteraed DOS.  Well, as clever as they have been, I personally
believe in getting the right tools for the job.

   There's a program (actually, there are several around) which lets you
"push" the current directory on a stack and pop it off when you want to
return to it again... many nested levels are allowed.  Here's an
example: 

    [Batch file]   pushd             // push current path
                   cd c:\games       // go elsewhere...
                   .                 // run whatever..
                   .
                   popd              // return to original path

   The version I have is called "push-pop"--about 16K zipped--
written by Paul Roub who includes the C sources and asks no money.

   If this can't be found archived somewhere, I'd be glad to email
to any intersted party, (or post it if needed).

----
Bruce R. Williams              "Computer Science is not about computers, 
University of Utah              any more than astronomy is about telescopes"
Salt Lake City                          - Edgar Dijkstra