1

I have the batch file below and wish to convert it to a single line cmd /c command.

@ECHO OFF
SETLOCAL
FOR /F "TOKENS=2 DELIMS=:." %%I IN ('chcp') DO SET _codepage_=%%I
SET _codepage_=Cp%_codepage_: =%
ECHO %_codepage_%
ENDLOCAL

I tried:

cmd /c FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO @SET _codepage_=%I && @SET _codepage_=Cp%_codepage_: =% && @ECHO %_codepage_%

also tried:

cmd /v:on /c FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO @SET _codepage_=%I && @SET _codepage_=Cp%_codepage_: =% && @ECHO !_codepage_!

but neither approach works! Can anyone help me out here?

fubar
  • 338
  • 1
  • 6
  • 20

4 Answers4

3
cmd /q/c"for /f "tokens=2delims=:." %a in ('chcp')do for %b in (%a)do echo(Cp%b"

What did the original code do? split the output of chcp, remove the ending dot (I didn't knew it, thank you), storing the value inside a variable to remove a space from it and prefix it with Cp and then echo the variable

What does this code do? split the output of chcp, remove the ending dot, but instead of using a variable to remove the space, an aditional for loop is used over the value from the first loop, and, instead of prefixing the variable, the Cp is included in the data echoed.

MC ND
  • 69,615
  • 8
  • 84
  • 126
  • Can you explain a little about your answer? It seems I needed to quote my complete command like this: `cmd /v:on /q /c "FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO SET _codepage_=%I && SET _codepage_=Cp!_codepage_: =! && ECHO !_codepage_!"`. But I cannot understand your answer, especially the `do echo(Cp%b` part! – fubar Oct 31 '14 at 12:23
  • Also it seems that `delims` needs the extra `.` since in German locale `chcp` result is `Aktive Codepage: 850.` [see here](http://stackoverflow.com/a/1337150/2773737) – fubar Oct 31 '14 at 12:35
  • @fubar, first time a see the ending dot, thank you. Added how it works. – MC ND Oct 31 '14 at 12:35
  • I am accepting your answer, although I will also post how I finally did it and the purpose of this script. – fubar Oct 31 '14 at 12:47
  • don't you think the `(` is invalid in `echo(Cp%b`? Shouldn't it be `echo Cp%b`? – fubar Oct 31 '14 at 12:59
  • @fubar, no, it is not invalid. Have you problems with it? – MC ND Oct 31 '14 at 14:10
  • It works with `echo(Cp%b` and `echo Cp%b` both! Why do you need the `(` char? – fubar Oct 31 '14 at 14:22
  • It this invalid? `cmd /q /c "FOR /F "TOKENS=2 DELIMS=:." %a IN ('chcp') DO FOR %b IN (%a) DO ECHO Cp%b"` – fubar Oct 31 '14 at 14:25
  • 1
    @fubar, no, it is not invalid. The `echo(` is just an habit. See [here](http://www.dostips.com/forum/viewtopic.php?p=4554) for the full history. In this case, the bracket is not needed – MC ND Oct 31 '14 at 14:58
2

I would use the MC ND answer. But just for yucks, I created another solution.

cmd /v:on /q /c "(for /f "delims=." %a in ('chcp') do for %b in (%a) do set cp=Cp%b) & echo !cp!"

Note that for all solutions, you might consider adding the /D option to prevent autorun commands from running.

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • It took me much time digging inside windows batch files command and I have to admit I was frustrated! Any other solution is welcome, just for the fun of it! – fubar Oct 31 '14 at 13:30
1

It seems I was missing double quotes around the cmd command. This is how I finally did this:

cmd /v:on /q /c "FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO SET _codepage_=%I && SET _codepage_=Cp!_codepage_: =! && ECHO !_codepage_!"

also correct as MC ND pointed out above:

cmd /q /c "FOR /F "TOKENS=2 DELIMS=:." %a IN ('chcp') DO FOR %b IN (%a) DO ECHO Cp%b"

I use this single command in Java with a ProcessBuilder in order to get the console's codepage and pass it to the process's stdout and stderr streams like this:

String encoding = getConsoleEncoding(); // use the above cmd command
BufferedReader stdout = new BufferedReader( new InputStreamReader(p.getErrorStream(),encoding) );

So the result of system commands like e.g. date /t will display correctly in Java.

Community
  • 1
  • 1
fubar
  • 338
  • 1
  • 6
  • 20
  • 1
    +1, Instead of adding quotes, you could have escaped `&&` as `^&^&`. Actually, you only need a single `^&`. – dbenham Oct 31 '14 at 14:14
0

You can save above into a file, name it as my.bat. Then use:

cmd.exe /c "call my.bat"

For In one command line, not use batch file, you can:

FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO (SET _codepage1_=%I && SET _codepage_=Cp%_codepage1_% && ECHO %_codepage_%)
coo
  • 114
  • 9
  • I do not wish to create a batch file; not even a tmp one. I think it can be done with a single line cmd, but cannot get it right! – fubar Oct 31 '14 at 11:59