How to convert a Windows batch file to the string "cmd / c"?

I have a batch file below and you want to convert it to a single cmd / c line .

@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 no approach works! Can anyone help me here?

0
windows cmd batch-file
source share
4 answers
 cmd /q/c"for /f "tokens=2delims=:." %a in ('chcp')do for %b in (%a)do echo(Cp%b" 

What did the source code do? split the output of chcp , delete the endpoint (I didn’t know this, thanks), keeping the value inside the variable, to remove the space from it and prefix it with Cp , and then repeat the variable

What does this code do? split the output of chcp , delete the endpoint, but instead of using the variable to remove the space, an additional for loop is used above the value from the first loop, and instead of the prefix, the Cp variable is included in the echo data.

+3
source share

I would use MC ND's answer . But only 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 can add the /D to prevent autorun commands from starting.

+2
source share

It seems I lacked double quotes around the cmd command. Here is how I finally did it:

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

also true as MC ND 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 ProcessBuilder to get the console code page and pass it to the stdout process and String Streams:

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

Thus, the result of system commands, for example, date / t, will correctly display in Java.

+1
source share

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

 cmd.exe /c "call my.bat" 

On one command line, without using a batch file, you can:

 FOR /F "TOKENS=2 DELIMS=:." %I IN ('chcp') DO (SET _codepage1_=%I && SET _codepage_=Cp%_codepage1_% && ECHO %_codepage_%) 
0
source share

All Articles