5

cmd.exe我在 Windows (至少到 XP)中有一个绝妙的技巧来模拟没有换行符的 UNIX 回显的行为, echo -n. 例如,命令:

<nul: set /p junk= xyzzy

将导致正好输出六个字符,前导空格和字符串“xyzzy”,仅此而已。

如果您对它的工作原理感兴趣它实际上是一个输入命令,它作为提示输出,然后在将该输入分配给环境变量" xyzzy"之前等待用户输入。junk在这种特殊情况下,它不会等待用户输入,因为它会从nul设备中获取输入。

cmd当(例如)在循环中处理文件时(每个文件一次迭代),您希望每行列出多个文件,它在脚本中非常有用。通过使用这个技巧,您可以简单地输出每个文件名后跟一个空格并且没有换行符,然后在循环之后输出一个换行符以完成:

Processing files:
    file1.txt file42.txt p0rn.zip

现在我发现,在 Windows 7 下,空格不再输出,所以我得到的是:

Processing files:
file1.txtfile42.txtp0rn.zip

有没有一种方法可以让set /p我再次开始尊重我的空间,或者在 Win7 中是否有另一种方法可以达到相同的效果?

我尝试过引用、使用.(在 中有效echo)甚至用 转义字符串^,但它们似乎都不起作用:

C:\Pax> <nul: set /p junk= xyzzy
xyzzy

C:\Pax> <nul: set /p junk=" xyzzy"
xyzzy

C:\Pax> <nul: set /p junk=' xyzzy'
' xyzzy'

C:\Pax> <nul: set /p junk=. xyzzy
. xyzzy

C:\Pax> <nul: set /p junk=^ xyzzy
xyzzy

我需要的是:

C:\Pax> some_magical_command_with_an_argument xyzzy
 xyzzy

这将在开始时给我空格,在结尾没有换行符。

4

4 回答 4

1

这与 paxdiablo 的答案非常相似,除了我使用混合 JScript/批处理文件而不是临时 VBScript 文件。

我的脚本被调用jEval.bat- 它只是评估任何有效的 JScript 表达式并将结果写入标准输出,可选地使用尾随换行符。愚蠢的小脚本对于批处理编程非常有用。

假设jEval.bat在您当前的文件夹中,或者在您的 PATH 中的某个位置,那么您可以简单地执行以下操作:

call jeval "' xyzzy'"

这是脚本。这真的很简单。大多数代码与文档、错误处理和内置帮助系统有关。

@if (@X)==(@Y) @end /* harmless hybrid line that begins a JScrpt comment

::************ Documentation ***********
:::
:::jEval  JScriptExpression  [/N]
:::jEval  /?
:::
:::  Evaluates a JScript expression and writes the result to stdout.
:::
:::  A newline (CR/LF) is not appended to the result unless the /N
:::  option is used.
:::
:::  The JScript expression should be enclosed in double quotes.
:::
:::  JScript string literals within the expression should be enclosed
:::  in single quotes.
:::
:::  Example:
:::
:::    call jEval "'5/4 = ' + 5/4"
:::
:::  Output:
:::
:::    5/4 = 1.25
:::

::************ Batch portion ***********
@echo off

if "%~1" equ "" (
  call :err "Insufficient arguments"
  exit /b
)
if "%~2" neq "" if /i "%~2" neq "/N" (
  call :err "Invalid option"
  exit /b
)
if "%~1" equ "/?" (
  setlocal enableDelayedExpansion
  for /f "delims=" %%A in ('findstr "^:::" "%~f0"') do (
    set "ln=%%A"
    echo(!ln:~3!
  )
  exit /b
)
cscript //E:JScript //nologo "%~f0" %*
exit /b

:err
>&2 echo ERROR: %~1. Use jeval /? to get help.
exit /b 1


************ JScript portion ***********/
if (WScript.Arguments.Named.Exists("n")) {
  WScript.StdOut.WriteLine(eval(WScript.Arguments.Unnamed(0)));
} else {
  WScript.StdOut.Write(eval(WScript.Arguments.Unnamed(0)));
}
于 2013-04-07T00:46:36.530 回答
0

这不是使用set或其他cmd内部的东西,但您可以使用cscript(也包括在标准 Windows 安装中)来做类似的事情。您甚至可以cmd通过创建临时文件从文件本身(无需维护单独的文件)来控制它vbs

将此代码放在您的cmd文件中:

rem Create the VBS file to output spaces and a word.

echo.for i = 1 to WScript.Arguments.Item(0) >spacetext.vbs
echo.     WScript.StdOut.Write ^" ^" >>spacetext.vbs
echo.next >>spacetext.vbs
echo.WScript.StdOut.Write WScript.Arguments.Item(1) >>spacetext.vbs

rem Do this once per word you want output (eg, in a loop).

cscript /nologo spacetext.vbs 0 Hello,
cscript /nologo spacetext.vbs 1 my
cscript /nologo spacetext.vbs 1 name
cscript /nologo spacetext.vbs 1 is
cscript /nologo spacetext.vbs 4 Pax.

rem Do this at the end to add newline and kill temp file.

echo.
del spacetext.vbs

这是您所期望的输出:

Hello, my name is    Pax.
于 2013-04-05T03:51:48.357 回答
0

好的,现在有正确的答案:你不能<space>在 Win7 中使用set /p. Windows版本之间存在差异:

Syntax                |  XP                                 |  Vista and Windows 7
----------------------+-------------------------------------+-------------------------------------
<nul set /p =!msg!    |- If 1st char is quote, then trims   |- Trims leading white space chars.   
       or             |  that quote, and if at least one    |- If 1st non white space char is     
<nul set /p "=!msg!"  |  additional quote, than trims last  |  quote, then that quote is treated  
                      |  quote and all remaining chars.     |  as white space and trimmed, and if
                      |- If 1st non trimmed char is =, then |  at least one additional quote, then
                      |  syntax error.                      |  trims last quote and all remaining
                      |                                     |  chars.
                      |                                     |- If 1st non trimmed char is =, then
                      |                                     |  syntax error.
----------------------+-------------------------------------+-------------------------------------
<nul set /p ="!msg!"  |- Trims leading control chars and    |- Trims leading white space chars.
       or             |  spaces.                            |- If 1st non trimmed char is =, then 
<nul set /p "="!msg!""|- If 1st non trimmed char is =, then |  syntax error.
                      |  syntax error.                      |
----------------------+-------------------------------------+-------------------------------------

On Vista and Windows 7, the trimmed leading white space chars are:
    9  0x09  Horizontal Tab
   10  0x0A  New Line
   11  0x0B  Vertical Tab
   12  0x0C  Form Feed
   13  0x0D  Carriage Return
   32  0x20  Space
  255  0xFF  Non-breaking Space

来源

有关在纯批处理中获得前导空格的另一种技术,请参见此处

于 2013-04-05T04:26:45.517 回答
0

你的意思是这样吗?

@ECHO OFF
SETLOCAL
FOR /l %%i IN (1,1,4) DO <NUL SET /p var="item %%i "

结果:

第 1 项 第 2 项 第 3 项 第 4 项

还是您绝对一开始就坚持一个空格?

于 2013-04-05T06:27:37.073 回答