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.
cd %pathname%
.. better still, trycd /d %pathname%
.. the /d allows it to switch between drives too.!
by%
it works well, except for the line "%@InDrive%\" that should be just%@InDrive%
.!
or%
problem. I tried it with no variables at all (just batch file with cd). It also does not work.