Current directory in DOSBox [Optional: Using TURBO C]

831 Views Asked by At

I want to run a command in a specific directory and then return back. (There is a reason for it [validity of parameters...]).

I tried doing it in batch file for DOSBox...

@echo off
cd>cd.cd
cd %mypath%
dosomething 1 2 3
::I am not sure....
cd (type cd.cd) 

%CD%, %dI, FOR loop nothing works in DOSBox...

I wrote a C program but couldn't find a function that returns the current directory for TURBO C 16-bit...

Can someone please help me out with this?

3

There are 3 best solutions below

9
phuclv On BEST ANSWER

%CD% is a variable in Windows cmd so you can't use it in MS-DOS. You can work around that by storing the current directory output from the cd command without any parameters into a variable by redirecting command output to file then read the file from disk

  • Prepare a file containing only @set cd= without any newlines. It can be created in DOS by pressing Ctrl+Z then Enter while running COPY CON. Let's name it init.txt
  • Then everytime you want to get the current directory run

    cd >cd.txt
    copy init.txt+cd.txt setcd.bat
    setcd
    
  • The last command will save the current directory into the %CD% variable

Get current dir

0
phuclv On

To get the current directory programmatically from Turbo C you need to read the current directory structure (CDS). The current directory is the first 67-byte field containing a null-terminated string

To get the address of the first CDS you use the 52h function of DOS int 21h (set AH=52h). Following CDS can be obtained by adding an offset to the first address. For more information read

2
Nephew of Stackoverflow On
  1. The command method (@phuclv's first answer) (Drawback: A permanent file needs to be maintained)

  2. The assembly method (@phuclv's first answer) (Drawback: I can't really find any way to perform system calls in assembly, it would be great if someone could provide a example and ask some privileged user to edit this answer to remove this info)

  3. The TURBOC method (Since I was anyways writing C90 code, I just used the way I anyways was going to.)

Here's the sample C90 Code which can be used to get and perform some operation on in TURBOC3:

#include<stdio.h>
//#include<string.h>

void main()
{

  char path[128];
  system("cd>__p_");
  fscanf(fopen("__p_","r"),"%[^\n]",path);
  remove("__p_");

  //path variable/array/pointer contains your current path.

  //printf(path);

  //strcat(command,path); //char command[128]="cd ";
  //system(command); 

}