-2

Problem

I am writing some .cmd / .bat files on a windows machine that need to work on an sd card with variable parent directories. The sd card will likely change drive names (Drive A, Drive E, etc.) as it moves from device to device and I want to write cmd files that will anticipate that. I would like this to work with my linux steam deck if possible, but if not I understand.

Rom Location E:\Games\Nintendo\3DS\Games\Animal Crossing New Leaf.3ds

Core Location E:\Games\RetroArch\cores\citra_libretro.dll

3DS.cmd , currently works at this address

@echo off
echo Keeping Window Active for GOG Time Tracking

cd "E:\Games\RetroArch\"
"retroarch.exe" -L "cores\citra_libretro.dll" %1 -f

Animal Crossing New Leaf.cmd , currently works at this address

@echo off

call "3DS.cmd" "E:\Games\Nintendo\3DS\Games\Animal Crossing New Leaf.3ds"

Question

How would I write the code above as a windows file on any non-specific drive directory where the current Directory is on the Drive named E:\ ? (Ex: A:\ , or B:, and so on)

adamcapjones
  • 185
  • 1
  • 17

2 Answers2

1

There can be used the following lines in the batch file 3DS.cmd if this batch file is stored in root of the SD card and is executed from the SD card mounted as drive with a drive letter:

@echo off
echo Keeping window active for GOG time tracking

cd /D "%~d0\Games\RetroArch"
retroarch.exe -L cores\citra_libretro.dll %1 -f

The usage help of command CALL output on running call /? in a command prompt window explains how to reference the arguments of a batch file. There is always the argument 0 even on batch file is executed without any argument string passed to the batch file by a user or another process.

%0 references the string used to start the execution of the batch file. On double clicking on a batch file stored on an SD card mounted with a drive letter by Windows, %0 expands to the fully qualified file name of the batch file on the SD card enclosed in " because of the Windows File Explorer starts in background:

C:\WINDOWS\system32\cmd.exe /c ""Animal Crossing New Leaf.cmd" "

The usage help of the Windows Command Processor cmd.exe output on running cmd /? explains how the arguments are interpreted by cmd.exe in this case. The first and the last " are removed from the command line. The started cmd.exe executes therefore:

"E:\Animal Crossing New Leaf.cmd"

That string with the double quotes is argument 0 of the executed batch file.

%~d0 can be used in the batch file to reference just the drive letter and the colon of the currently running batch file respectively \\ if the batch file is stored on a network resource executed using its UNC path.

The code above works only for batch file being stored in root of a storage media mounted with a drive letter.

A code for 3DS.cmd working always independent on which storage media the batch file is stored and in which directory and how the batch file is started as long as the directory Games is a subdirectory of the directory containing the batch file is:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
if "%~1" == "" echo ERROR: %~nx0 called without game file name!& pause & exit /B
pushd "%~dp0Games\RetroArch"
echo Keeping window active for GOG time tracking
retroarch.exe -L cores\citra_libretro.dll %1 -f
popd
endlocal

%~dp0 expands to full path of the batch file always ending with a backslash.

See also: What is the reason for batch file path referenced with %~dp0 sometimes changes on changing directory? The bug of cmd.exe does not matter here because of %~dp0 is used before changing the current directory the first time with the command PUSHD.

The batch file Animal Crossing New Leaf.cmd stored in same directory as 3DS.cmd should contain only the single command line:

@call "%~dp03DS.cmd" "%~dp0Games\Nintendo\3DS\Games\Animal Crossing New Leaf.3ds"

The two batch files can be used with these improvements also on copying all directories and files on the SD card to a directory of user´s choice like %UserProfile%\RetroGames.

It is also possible to use only one batch file with name Animal Crossing New Leaf.cmd stored in the directory with the subdirectory Games and all the other directories and files with the following lines:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0Games\RetroArch" || (echo ERROR: Missing subdirectory "Games\RetroArch"& pause & exit /B)
echo Keeping window active for GOG time tracking
retroarch.exe -L cores\citra_libretro.dll "%~dp0Games\Nintendo\3DS\Games\Animal Crossing New Leaf.3ds" -f
popd
endlocal

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • cd /?
  • echo /?
  • endlocal /?
  • exit /?
  • if /?
  • pause /?
  • popd /?
  • pushd /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143
0

I used Chat GPT, it gave me this result that is now working.

3ds.cmd

@echo off
echo Keeping Window Active for GOG Time Tracking

set sd_dir=%cd:~0,1%

cd "%sd_dir%:\Games\RetroArch\"
"retroarch.exe" -L "cores\citra_libretro.dll" %1 -f

Animal Crossing New Leaf.cmd

@echo off

set sd_dir=%cd:~0,1%

call "3DS.cmd" "%sd_dir%:\Games\Nintendo\3DS\Games\Animal Crossing New Leaf.3ds"
adamcapjones
  • 185
  • 1
  • 17
  • 2
    You might be interested in the modifiers explained in `call /?` Within a batch file, `%0` is the full path of your file, `%~d0` is the drive, `%~p0` is the folder where your script resides (may be different from the "current working folder"), `%~n0` the filename without extension, etc. You can combine them like `echo My folder is %~dp0` and my name is %~nx0`. – Stephan Jan 15 '23 at 10:14