2

If I understand correctly this batch script

for /f "usebackq tokens=*" %%i in (`time /t`) do (
  echo "%%i"
)
echo "done"

should just print the time and then "done"

Instead though on my machine, Windows 10 Pro x64 it prints

>test.bat
>for /F "usebackq tokens=*" %i in (`time /t`) do (echo "%i" )
>(echo "Active code page: 65001" )
"Active code page: 65001"
>(echo "17:48" )
"17:48"
>echo "done"
"done"

Why is it doing this and how do I fix it so it just gets the time and not this "Active code page: 65001" result. This is breaking build scripts as it doesn't matter what command goes in the FOR command the first line is always "Active code page: 65001"

To be clear I'm looking for an OS setting fix, not a fix to the batch file. The real batch files I'm trying to run are from build scripts in open source projects and they are failing because of this issue.

gman
  • 100,619
  • 31
  • 269
  • 393
  • this looks strange. What if you set `chcp 437` at the begining of your script? – npocmaka Jan 10 '18 at 09:00
  • same results: https://pastebin.com/37QsMyGD :( – gman Jan 10 '18 at 09:04
  • 3
    Check if the registry keys `\Software\Microsoft\Command Processor\AutoRun` under `HKLM` or `HKCU` are configured to run anything when the `cmd` instances are created. – MC ND Jan 10 '18 at 09:08
  • Also try to check your date format settings in the control panel or the registry. the output you are giving is usually produced by `CHCP` command. – npocmaka Jan 10 '18 at 09:13
  • that was it! under HKLM ... ¯\\_(ツ)_/¯ – gman Jan 10 '18 at 09:13
  • same result can be achieved with `echo %time:~0,5%` (if `time /t` isn't just a placeholder for some other command) – Stephan Jan 10 '18 at 15:01
  • @MCND, please post your comment as the answer so we can get this thread off of the unanswered list. – jwdonahue Jan 11 '18 at 06:12
  • @jwdonahue, done, but I'm not sure this has not been answered before. – MC ND Jan 11 '18 at 09:49
  • If you find a dupe please mark as dupe. I have no idea how that got in my registry but @MCND thank's so much you saved my life :P – gman Jan 11 '18 at 10:21

2 Answers2

4

When we run a command and we get the correct output, but it is preceded by the output of another command, somehow this unexpected command is run before our intended command.

The usual suspect is a batch file created with the same name that the command we are calling, but in this case, being time an internal command, this can not be the case, internal commands have precedence over external commands (default behaviour).

If time is an internal command, how can another command being called?

When for /f needs to process the output of a command a separate cmd instance is created to execute that command. The output generated in this separate instance is processed by the code in the do clause of the for /f command.

The creation of this separate instance can be the source of the unexpected output.

Reading the help information shown by cmd /? we can see that there are two registry keys

HKEY_LOCAL_MACHINE\Software\Microsoft\Command Processor\AutoRun
HKEY_CURRENT_USER\Software\Microsoft\Command Processor\AutoRun

that can be configured to run commands every time a new cmd instance is created (unless the /D switch is used when starting the cmd instance).

This is the most probable place where to find why you get the Active code page: 65001 output.

MC ND
  • 69,615
  • 8
  • 84
  • 126
1

Instead of changing the registry you can control the code page simply by creating a file %HOMEPATH%\init.cmd.
Mine says:

@ECHO OFF
CHCP 65001 > nul

You need to suppress the output from CHCP to avoid the undesired output.

user333869
  • 549
  • 1
  • 4
  • 13