0

What I want:

cd !pathname!

This should change directory to the path. Of course, on other drives this does not work in one line without cd /D !pathname!.

My test/debug code looks as follows:

cls
set @InDrive=%~d0

echo /D K:\
echo /D !@InDrive!\
echo !@InDrive!\

cd /D K:\
cd /D !@InDrive!\
!@InDrive!\
cd /D K:\

The @ sign is required for the line set @InDrive=%~d0\

Without it, the variable would not even be set.

Result of my code is as follows:

/D K:\
/D K:\
K:\
Der Befehl "K:\" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.

The syntax error comes from the line !@InDrive!

But when I echo the line, and copy the output of the line as is, it changes the directory. It is strange, that entering the exact output by hand works, but inside the batch script it does not work. What is going on? Btw: I ran the script and the command, that lies in the variable !@InDrive! in the same cmd.exe window. So nothing changed in the background. (Remember: !@InDrive! equals K:\ so cd /D K:\ should work as it does, when writing it manually)

What is wrong with it? Is this one of the hundrets of errors from the windows 10 updates recently?

I could swear, that I used a command like this years ago with succes. But I do not have the scripts anymore.

The search did not yield any result unfortunately. Anyone knows the source of the error? Goal is a script that works with relative paths, so I need to get a working CD. (I will use set @InPath=%~dp0input\, which does echo out as expected when using echo !@InPath!)

I also tried pushd and it works, written by hand, but not in batch script. So this is not the answer unfortunately.

Important Edit:

When I disable SETLOCAL EnableDelayedExpansion it works! But I am absolutely required to use SETLOCAL EnableDelayedExpansion

What could be a clean workaround? When I CD before SETLOCAL EnableDelayedExpansion it works, but I want to cd inside the loop.

8
  • The ! marks are only used for delayed expansion. What you are looking for is probably %. Try cd %pathname%.. better still, try cd /d %pathname% .. the /d allows it to switch between drives too. Aug 13, 2020 at 16:40
  • The !var! is intentional. I am also using delayedexpansion. I also tried %var%, but this also does not work at all. (I did not want to write an even larger post, since I cannot see a source of error here.). Nontheless, it has to work either way. Aug 13, 2020 at 16:48
  • Lol, the rest of my script already is done. Just this "simple" thing, that fails. Hard to imagine sometimes..... I got a loop, that scrubs through the folders and subfolders. It records the relative paths of files of a type that is listed in fileformat.cfg (each line = 1 file type = 1 loop). This gets fed into ffmpeg than. Just need the ability to use this script on relative paths without manual CD, which would make it almost useless without. Aug 13, 2020 at 17:51
  • Show us a better script. When replacing ! by % it works well, except for the line "%@InDrive%\" that should be just %@InDrive%.
    – harrymc
    Aug 13, 2020 at 18:07
  • Please look at the code. I also tried it without a variable - string only. This also does not work. So it is not a ! or % problem. I tried it with no variables at all (just batch file with cd). It also does not work. Aug 13, 2020 at 18:16

1 Answer 1

1

The answer is simple, but complicated. SETLOCAL EnableDelayedExpansion will basically "Setlocal" anything. That means, that it does not forward the CD to the CMD.exe anymore. Only after ENDLOCAL this works. However it cannot be used inside a loop.

Code has to be rewritten to CD before SETLOCAL EnableDelayedExpansion or after ENDLOCAL. This topic answers, why this problem exists, quite well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.