0

I am programming a DOS-like shell, but it starts in %UserProfile% instead of %UserProfile%\Desktop\Coding projects\CM-DOS. The actual shell, kernel.bat's code is:

@echo off
type INFO.txt
pause

INFO.txt is in the same directory as kernel.bat (CM-DOS). It says:

CM-DOS is a FREE, open-source Disk Operating System by Tristan Carpenter. If anyone trys to sell you a paid copy of CM-DOS, send me an e-mail at tristan4@mail.com. Please use this DOS shell kindly. This shell is licensed under the MIT License. Do whatever you want with the shell, create closed-source versions, fork it, I don't really care.

The script tries to type "%UserProfile%\INFO.txt", which doesn't exist. How can I start the file in it's actual directory, instead of %UserProfile%.

Squashman
  • 13,649
  • 5
  • 27
  • 36
Trisiegt
  • 12
  • 1
  • 1
    According to the wording, you might be trying to create own OS. However it'd be at best a script ran within an OS (Windows or DOS or anything capable of running Batchfile). Writing an OS is a different beast to tame. You might want to check [OSDev wiki](https://wiki.osdev.org) and also [this article](https://medium.com/@andrewimm/writing-a-dos-clone-in-2019-70eac97ec3e1) seems to be going in the right direction as well. – Peter Badida Feb 05 '22 at 19:35
  • 1
    Use in the batch file `type "%~dp0INFO.txt"` to reference the text file with the fully qualified file name using the full path of the batch file (argument 0) always ending with a backslash. There is never the guarantee that the directory containing the executed batch file is the current directory of `cmd.exe` processing the batch file. – Mofi Feb 05 '22 at 20:21
  • 1
    The process starting `cmd.exe` to process the batch file determines the current directory on calling the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) which has the function parameter `lpCurrentDirectory` which is either a null pointer or a pointer to the string with the path of the current directory. So `cmd.exe` itself does not set the current directory for itself, it is done by the process starting `cmd.exe` like `explorer.exe` or `schtasks.exe`. – Mofi Feb 05 '22 at 20:23
  • Example: [Why does 'Run as administrator' change (sometimes) batch file's current directory?](https://stackoverflow.com/a/31655249/3074564) – Mofi Feb 05 '22 at 20:25

2 Answers2

1

If you run the script (clicking or navigating (cd <folder>) and .\kernel.bat) and the current folder isn't, well, the current folder, then something is off with either your console setup or there is an additional code executed in the background. Try to echo %CD% to see what the folder is and check the prompt string usually in the shape of:

C:\Users\Username>

which is the default path if you run cmd.exe as a user or:

C:\Windows\System32>

if you run as an Administrator / user with elevated privileges.

This can be changed though with regedit.exe.

However what you might be seeing is that you run your code (kernel.bat or however you name it) which might display the first path before execution or after the last command you might be dropped into a shell. Neither should be the case according to the code, yet it can happen if you started cmd.exe, then wrote cmd.exe (i.e. spawned another shell) and then ran your script.

What I suspect is that you're running the code like this:

C:\Users\You> .\Desktop\Coding projects\CM-DOS\kernel.bat

in which case the %CD% would be C:\Users\You and type command would be looking for C:\Users\You\INFO.txt. A better solution is simply creating a variable for the folder where your script is located - which is provided automatically, it's %~dp0 (echo %~dp0) and with that you can do:

echo off
type "%~dp0INFO.txt"
pause

to make it location independent, or better said, to access relatively according to the kernel.bat (the current script)'s location.

Peter Badida
  • 11,310
  • 10
  • 44
  • 90
-2

You can add cd %CD%
and %CD% stands for the current direction.
the final code would be

@echo off
cd %CD%
type INFO.txt
pause
manhat
  • 1
  • 3
  • I'm sorry, but I still get "The system can't find the file specified." error. – Trisiegt Feb 05 '22 at 19:19
  • try this `@echo off cd %CD% & type INFO.txt pause` – manhat Feb 05 '22 at 19:25
  • 4
    `cd %CD%` is completely useless as it changes the current directory to current directory and not to the directory containing the batch file. Try it out. Create a batch file with the two lines `@echo Current directory is: "%CD%"` and `@echo Batch file directory is: "%~dp0"` and save the batch file with name `Demo.cmd` on your `Desktop` or in your `Documents` folder. Then open a command prompt window and run first `cd /D "%USERPROFILE%"` and next `"%USERPROFILE%\Desktop\Demo.cmd"` or `"%USERPROFILE%\Documents\Demo.cmd"`. You see the difference between current and batch file directory. – Mofi Feb 05 '22 at 20:30
  • 1
    Use `cd /D "%~dp0."` instead of `cd %CD%`. Next time please try your code yourself before you post an answer! – aschipfl Feb 06 '22 at 13:00