Page 1 of 1

Can't change directory using cmd script

Posted: 08 Dec 2022 02:08
by Quisquose
I can change directory fine if I manually type cd into cmd window, but I cannot get any cd command to work from a cmd file.

I know that I must be overlooking something basic and glaringly obvious, but I can't figure out what's going on.

For example:

If I type

Code: Select all

cd /d "C:\Windows"
at the command prompt, it goes to the Windows folder on C:. However, If I put the same command in a cmd file and run it, then instead of changing directory I get an error message saying:

Code: Select all

 '■@' is not recognized as an internal or external command,
operable program or batch file.
Every guide that I've looked up says that you change directories in scripts and batch files the same way that you do manually at the command prompt (i.e. using cd) so I therefore assumed that the problem must be something specific to my system (maybe a weird setting somewhere). To see if this was the case I tried to do the same thing in a virtual machine using a clean installed OS, but the same thing happened.

I use custom Start Menu shortcuts to launch certain console sessions, and I specify startup paths in the shortcut's Target field using a cd command and it works fine for all of them (even using variables such as: cd /D %userprofile% ) so I don't know why the exact same thing won't work from within a .cmd file.

Anyone have any suggestions for what the issue might be? (other than PBKAC).

Some info that may be of use:

Code: Select all

Date/Time format       :  (dd/mm/yy)  08/12/2022   8:05:13.29
__APPDIR__             :  C:\Windows\system32\
ComSpec                :  C:\Windows\system32\cmd.exe
PathExt                :  .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
Extensions             :  system: Enabled   user: Enabled 
Delayed expansion      :  system: Disabled  user: Disabled
Locale name            :  en-GB       Code Pages: OEM  850    ANSI 1252
DIR  format            :  08/12/2022  03:19    17,093,369,856 pagefile.sys
Permissions            :  Elevated Admin=Yes, Admin group=Yes

                          Missing from the tool collection:  debug

Re: Can't change directory using cmd script

Posted: 08 Dec 2022 02:25
by ShadowThief
That error you're getting means that your script is not saved with the correct encoding. Save your script as ANSI (or UTF-8 if you absolutely have to, but ANSI works best).

Re: Can't change directory using cmd script

Posted: 10 Dec 2022 04:58
by Quisquose
Thank you!

I switched to ANSI and it seems to have done the trick. I'm not sure how it got changed (because I've been using that script for a while before it broke).

Anyway, now that I can run cd commands again, I can get back to trying to deal with the issue that I was trying to resolve in the first place.

As I mentioned previously, I use Start Menu shortcuts to configure console sessions for different uses. In this particular case I am using:

Code: Select all

%windir%\system32\cmd.exe /k title  Terminal2 & pushD "U:\path\to\a\script\folder\" & script.cmd & cd /D %userprofile%
It works fine, but when I try to run batch commands from within that session it stays stuck on the home folder path and won't respond to cd commands (unless they are typed in manually). I'm guessing that it may be something to do with the %userprofile% that's included when starting up that particular console session, but I don't know for sure. Maybe there are some other possible reasons for it.

How can I use batch files containing cd path change commands from within a session that's been started using the above command? (if that is indeed what is causing the problem).

Thanks.