0

When I try to get the output of a command in a batch file by using single quotes I am getting the following info:

C:>for /f "tokens=2 delims=:." %a in ('chcp') do (echo %a)

'chcp' is not recognized as an internal or external command, operable program or batch file.

This command can be run from the same location directly:

C:>chcp

Active code page: 437

I have already checked env variables and they seem to be set correctly. Above problem occurs also when I use 'dir' instead of 'chcp'.

Any ideas what can be the reason and how to fix that?

OS: WIN 10

9
  • 2
    Not that it helps, but it works fine for me. Have a look at stackoverflow.com/questions/8142058/… For me, chcp is in c:\windows\system32 Commented Aug 1, 2019 at 9:42
  • C:\>where chcp C:\Windows\System32\chcp.com C:\Windows\SysWOW64\chcp.com Seems like it's in a proper place. I was able to run this command directly from command like with no issues but for some reason it doesn't work from bat file. Also, my PATH includes C:\Windows\system32
    – Matt
    Commented Aug 1, 2019 at 10:14
  • Welcome to SuperUser. Can you edit your question to include the complete contents of your batch file?
    – Doug Deden
    Commented Aug 1, 2019 at 13:11
  • Hi Doug. The for loop I pasted in the original question is indeed the content of the batch file. After some search I see that the problem I'm having looks identical to the one described in the following thread: stackoverflow.com/questions/9668011/command-line-for-f-fails but unfortunately, provided solutions did not work in my case.
    – Matt
    Commented Aug 1, 2019 at 13:47
  • What if you reduce the batch file down to just "chcp". That'll narrow down the problem to one of not finding chcp, thus probably a path issue, or something odd with the For construct.
    – Doug Deden
    Commented Aug 1, 2019 at 13:49

1 Answer 1

1

As you found, your ComSpec environment variable contained two values: C:\Windows\system32\cmd.exe;C:\Windows\SysWOW64\cmd.exe. This causes the for /f command to choke when it tries to find out how to find chcp.

Changing your ComSpec environment variable to the typical value of C:\Windows\system32\cmd.exe will make the for / mechanism work.

You are not alone:

  • According to this question, you aren't the only one to get both the System32 and SysWOW64 versions of cmd.exe listed in ComSpec.
  • And in this question, a differently malformed ComSpec caused the same "... is not recognized as an internal or external command..." error.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .