1

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?

phuclv
  • 37,963
  • 15
  • 156
  • 475

3 Answers3

2

%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

phuclv
  • 37,963
  • 15
  • 156
  • 475
  • Is there any way to append a string on the same line DOS? – Nephew of Stackoverflow Feb 13 '20 at 13:25
  • Actually my DOSBox hangs on `copy con ` for some reason. – Nephew of Stackoverflow Feb 13 '20 at 13:27
  • @NephewofStackoverflow it's not hanging. It's waiting for you to enter the content because [`CON` is the keyboard](https://en.wikipedia.org/wiki/Device_file#DOS,_TOS,_OS/2,_and_Windows). See [copy con](https://www.computerhope.com/jargon/c/copycon.htm). That's the reason why you can't [create a folder named “CON” in Windows (easily)](https://superuser.com/q/129141/241386) – phuclv Feb 13 '20 at 13:31
  • Oh..That was pretty unexpected . Is there any way by which we can write on the same line instead with two different command? – Nephew of Stackoverflow Feb 13 '20 at 13:35
  • something like `@echo set cd=(somespecialcharacter)>mp` and then `@cd>>mp` – Nephew of Stackoverflow Feb 13 '20 at 13:37
  • @NephewofStackoverflow you just prepare init.txt once and use it forever. No need to care about that file. `echo` writes a new line so you can't use echo to do that – phuclv Feb 13 '20 at 13:49
  • Is there any way by which I can create a script that runs after every cd or drive change? – Nephew of Stackoverflow Feb 13 '20 at 14:18
  • probably make a batch file that calls CD and does everything you want, and avoid using the original CD completely. But it's outside the scope of this question. You should ask a different one – phuclv Feb 13 '20 at 14:20
  • @phuclv: That sounds somehow [familiar](https://stackoverflow.com/a/31210059/2152082) – Stephan Feb 13 '20 at 20:02
1

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

phuclv
  • 37,963
  • 15
  • 156
  • 475
-1
  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); 

}
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    Can't you use Turbo-C's `getcwd` function to get the current working directory? http://www.x-hacker.org/ng/borcpp/ng2e153.html – Michael Petch Feb 13 '20 at 19:01
  • You can just use [`intdos()`](https://www.itlnet.net/programming/program/Reference/tc/ng30db2.html) to run the interrupt, no need to use inline assembly. But even if you want to use inline assembly just `mov ah, 52h; int 21h` after saving necessary registers. But otherwise use [`getcurdir`](https://www.itlnet.net/programming/program/Reference/tc/ng2b451.html) or `getcwd` – phuclv Feb 14 '20 at 01:59