简体   繁体   English

如何将Windows批处理文件转换为签名行“ cmd / c”命令?

[英]How to convert a windows batch file into a signle line “cmd /c” command?

I have the batch file below and wish to convert it to a single line cmd /c command. 我在下面有该批处理文件,希望将其转换为单行cmd / c命令。

@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? 有人可以帮我从这里出去吗?

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 分割chcp的输出,删除结束点(我不知道,谢谢),将值存储在变量中以从中删除空格,并以Cp前缀,然后回显该变量

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. 分割chcp的输出,删除结尾的点,但是不使用变量来删除空格,而是对第一个循环中的值使用aditional for循环,并且Cp包含在Cp ,而不是对变量加上前缀。数据回显。

I would use the MC ND answer . 我会使用MC ND答案 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. 请注意,对于所有解决方案,您都可以考虑添加/D选项以阻止自动运行命令运行。

It seems I was missing double quotes around the cmd command. 看来我在cmd命令周围缺少双引号。 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: 也如MC ND指出的那样正确:

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: 我将Java中的单个命令与ProcessBuilder一起使用,以获取控制台的代码页,并将其传递给流程的stdout和stderr流,如下所示:

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

So the result of system commands like eg date /t will display correctly in Java. 因此,诸如date / t之类的系统命令的结果将在Java中正确显示。

You can save above into a file, name it as my.bat. 您可以将上面的内容保存到文件中,将其命名为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_%)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM