DOSBox 中的当前目录 [可选:使用 TURBO C]

Current directory in DOSBox [Optional: Using TURBO C]

我想 运行 特定目录中的命令然后 return 返回。 (这是有原因的[参数的有效性...])。

我尝试在 DOSBox 的批处理文件中进行...

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

%CD%%dIFOR 循环在 DOSBox 中无效...

我写了一个 C 程序,但找不到 returnTURBO C 16 位当前目录的函数...

有人可以帮我解决这个问题吗?

%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

  • 准备一个只包含 @set cd= 且没有任何换行符的文件。它可以在 DOS 中通过按 Ctrl+Z 然后 Enter 而 运行ning 创建COPY CON。我们将其命名为 init.txt
  • 然后每次都想获取当前目录运行

    cd >cd.txt
    copy init.txt+cd.txt setcd.bat
    setcd
    
  • 最后一条命令会将当前目录保存到%CD%变量中

要从 Turbo C 以编程方式获取当前目录,您需要阅读 current directory structure (CDS)。当前目录是包含空终止字符串的第一个 67 字节字段

要获取第一个 CDS 的地址,请使用 DOS int 21h 的 52h 函数(设置 AH=52h)。可以通过在首地址上添加偏移量来获得以下 CDS。欲了解更多信息,请阅读

  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.)

这里是示例 C90 代码,可用于在 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); 

}