关于批处理文件:DOSBox中的当前目录[可选:使用TURBO C]

Current directory in DOSBox [Optional: Using TURBO C]

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

我尝试在DOSBox的批处理文件中执行此操作...

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

%CD%%dIFOR循环在DOSBox中不起作用...

我编写了一个C程序,但是找不到返回TURBO C 16位当前目录的函数...

有人可以帮我吗?


%CD%是Windows cmd中的变量,因此您不能在MS-DOS中使用它。您可以通过以下方式解决此问题:将cd命令的当前目录输出不带任何参数存储到变量中,方法是将命令输出重定向到文件,然后从disk

中读取文件

  • 准备一个仅包含@set cd=且没有换行符的文件。可以在DOS中通过在运行COPY CON的同时按Ctrl Z然后Enter来创建它。让我们将其命名为init.txt
  • 然后每次您要运行当前目录时

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

Get current dir


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

要获取第一个CDS的地址,请使用DOS int 21h的52h函数(设置AH = 52h)。通过向第一个地址添加偏移量可以获得后续的CDS。有关更多信息,请阅读

  • 当前目录结构(CDS)的格式(数组,LASTDRIVE条目)
  • PC Mag 1991年8月
  • PC Mag 1993年11月9日
  • PC Mag 1991年6月25日

  • The command method (@phuclv's first answer) (Drawback: A permanent file needs to be maintained)

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

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

  • 这是示例C90代码,可用于在TURBOC3中获取并执行一些操作:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    #include<stdio.h>
    //#include<string.h>

    void main()
    {

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

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

      //printf(path);

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

    }