Image Map Image Map
Results 1 to 6 of 6

Thread: Batch script: save current directory, change directory, restore first directory

  1. #1

    Arrow Batch script: save current directory, change directory, restore first directory

    When installing a fresh DOS computer, I usually create scripts and place them in the C:\BIN directory, and add it to the PATH.

    So for instance, let's say I want to run Norton Commander easily (it could be anything else, any program or game that requires certain files) then I create the script nc.bat:

    edit C:\bin\nc.bat

    C:
    cd C:\nc
    nc

    Now this always will change my directory to the directory where NC is, but I'd like to keep the current working directory, and to achieve that, I need the help of some batch scripting wizards (you).

    I know little about batch scripting, here's an idea:

    Code:
    :: Save the current directory to a file:
    cd > c:\temp\dir
    :: Run NC
    c:
    cd c:\nc
    nc
    ::  Make the variable %dir% from the contents of C:\temp\dir (I don't know how to do it):
    ??????????
    :: change directory to the initial directory
    cd %dir%
    Another idea would be changing the PATH according to the program/game in question, so that it could find its files while preserving the current directory, but it hasn't worked for me.

    I have been trying to solve this since a long time ago, but never managed to figure how to do it.

  2. #2

    Default

    1) You could use a second batch file containing 'call C:\nc\nc.bat' and place it in the path and move the nc.bat to the C:\nc DIR after editing to to just 'nc'.

    2) You might be able to utilize pushdir/popdir in your original) batch file. It's been quite a while since I've done this so I'll need to refresh to be sure.
    ☀☀☀ Visit Take Another Step for both computer and non-computer related discussions. ☀☀☀

    If you're looking for DS/DD or DS/HD 3" or 5" floppy disks, PM me. I've got some new, used, and factory over-labeled disks for sale.

    There is a crack in everything. That's how the light gets in. -- Leonard Cohen

  3. #3

    Thumbs up

    Hi Stone thanks for the suggestions.

    1) You could use a second batch file containing 'call C:\nc\nc.bat' and place it in the path and move the nc.bat to the C:\nc DIR after editing to to just 'nc'.
    It seems like a good idea but it doesn't work, actually it is the same as just doing C:\nc\nc.exe
    You need to change the directory for it to find its files, and if you change the directory, how do you get back to the previous directory?
    I'm not talking about this particular program, but about all complex programs.
    I actually tried it, and even tried changing the PATH to the program's working directory, in vain.

    2) You might be able to utilize pushdir/popdir in your original) batch file. It's been quite a while since I've done this so I'll need to refresh to be sure.
    I wish this existed in command.com, but it doesn't (it was introduced with cmd.exe)

    I have read that 4DOS includes pushd/popd, maybe a TSR? or a specific version of doskey too? I only know this by paulhoule which is the one I use.

    I'm pretty sure that an "old wizard of DOS" can absolutely manage to do this with the standard 6.22 command.com
    Otherwise we'll figure it out!

  4. #4

    Default

    PUSHDIR.COM and POPDIR.COM are DOS programs and will do exactly what you want where PUSHDIR is the first line in the BATCH file and POPDIR is the last.

    http://ftp.mpoli.fi/pub/unpacked/sof...ip/pushdir.doc

    http://ftp.mpoli.fi/pub/unpacked/sof...g/vol5n10.zip/
    ☀☀☀ Visit Take Another Step for both computer and non-computer related discussions. ☀☀☀

    If you're looking for DS/DD or DS/HD 3" or 5" floppy disks, PM me. I've got some new, used, and factory over-labeled disks for sale.

    There is a crack in everything. That's how the light gets in. -- Leonard Cohen

  5. #5
    Join Date
    Jan 2007
    Location
    Pacific Northwest, USA
    Posts
    27,890
    Blog Entries
    20

    Default

    I use a little batch script that preserves the variable between boots. It's nothing complex:

    Code:
    @echo off
    if X%1X==XX goto setvar
    echo set dest=%1>c:\bin\dpath.bat
    goto setit
    :setvar
    if X%dest%X==XX goto needpath
    :setit
    c:\bin\dpath
    goto quit
    :needpath
    if not exist c:\bin\dpath.bat goto errnone
    c:\bin\dpath
    goto quit
    :errnone
    echo You need a path specified--one has not been set!
    :quit
    In this case, the script is for an environment variable called "dest". Running the script with no arguments changes the current directory to whatever was last specified. If an argument is specified, it's the path to a new directory.

    Strictly speaking the "goto" after the "dpath" calls isn't necessary, but I put it there for ease of reading. DOS batch scripts don't nest unless invoked via CALL.

  6. #6

    Default

    For a pure batch file solution, you could append the current path to a file which has no terminating newline.

    Example NC.BAT:
    Code:
    @echo off
    if x%1==x! goto doit
    copy c:\bin\nc.bat c:\bin\$$$.bat>nul
    cd>>c:\bin\$$$.bat
    c:\bin\$$$.bat !
    rem the copied file is invoked without call, so we don't return here
    
    :doit
    rem launch program
    c:
    cd \nc
    nc.exe
    cd
    The last line should have one blank space after the "cd", and no CR+LF (some editors always insert one, but e.g. NCEDIT doesn't)
    Also Norton Commander should run fine from any directory, unless you are using DOS 2.x. But there are certainly other programs that this could be useful for.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •