没有空格的Echo命令

时间:2015-08-11 18:09:48

标签: batch-file cmd echo

我正在尝试输出一些变量但是在字符之间会出现不需要的空格。例如,如果变量1 = 3,则变量2 = 5,变量3 = 0。

当我跑步时:

echo %1%%2%%3%

我应该得到" 350"而不是" 3 5 0"。我怎样才能得到" 350"结果而不是带空格的结果。感谢

编辑: 我的脚本中的一个部分(请注意" set char"仅用于此示例,并从实际脚本中的变量分配):

set char=3

if "%char%" == "1" echo %char% >> %userProfile%\test2.txt & set c=1 & goto Character4
if "%char%" == "2" echo %char% >> %userProfile%\test2.txt & set c=2 & goto Character4
if "%char%" == "3" echo %char% >> %userProfile%\test2.txt & set c=3 & goto Character4
if "%char%" == "4" echo %char% >> %userProfile%\test2.txt & set c=4 & goto Character4
if "%char%" == "5" echo %char% >> %userProfile%\test2.txt & set c=5 & goto Character4
if "%char%" == "6" echo %char% >> %userProfile%\test2.txt & set c=6 & goto Character4
if "%char%" == "7" echo %char% >> %userProfile%\test2.txt & set c=7 & goto Character4
if "%char%" == "8" echo %char% >> %userProfile%\test2.txt & set c=8 & goto Character4
if "%char%" == "9" echo %char% >> %userProfile%\test2.txt & set c=9 & goto Character4
if "%char%" == "0" echo %char% >> %userProfile%\test2.txt & set c=0 & goto Character4


:character4
set char=4

if "%char%" == "1" echo %char% >> %userProfile%\test2.txt & set d=1 & goto check
if "%char%" == "2" echo %char% >> %userProfile%\test2.txt & set d=2 & goto check
if "%char%" == "3" echo %char% >> %userProfile%\test2.txt & set d=3 & goto check
if "%char%" == "4" echo %char% >> %userProfile%\test2.txt & set d=4 & goto check
if "%char%" == "5" echo %char% >> %userProfile%\test2.txt & set d=5 & goto check
if "%char%" == "6" echo %char% >> %userProfile%\test2.txt & set d=6 & goto check
if "%char%" == "7" echo %char% >> %userProfile%\test2.txt & set d=7 & goto check
if "%char%" == "8" echo %char% >> %userProfile%\test2.txt & set d=8 & goto check
if "%char%" == "9" echo %char% >> %userProfile%\test2.txt & set d=9 & goto check
if "%char%" == "0" echo %char% >> %userProfile%\test2.txt & set d=0 & goto check

:check
echo %c%%d%

这将返回" 3 4"而不是" 34"

3 个答案:

答案 0 :(得分:3)

这不是问题的根源,但是你应该在SET语句中使用引号来防止意外尾随空格 - 当引号在变量名之前时,忽略最后一个引号之后的所有内容:

set "var=value"  This text after last quote is ignored

您的文件输出中存在相同的不需要的空间问题。您的问题是<<&之前的不需要的空间 - 空间包含在输出中。但你不能简单地删除空格,因为那时你有3>...,这意味着重定向文件句柄3.解决方案是将重定向放在前面:

set "char=3"

if "%char%" == "1" >>"%userProfile%\test2.txt" echo %char%& set c=1& goto Character4
if "%char%" == "2" >>"%userProfile%\test2.txt" echo %char%& set c=2& goto Character4
if "%char%" == "3" >>"%userProfile%\test2.txt" echo %char%& set c=3& goto Character4
if "%char%" == "4" >>"%userProfile%\test2.txt" echo %char%& set c=4& goto Character4
if "%char%" == "5" >>"%userProfile%\test2.txt" echo %char%& set c=5& goto Character4
if "%char%" == "6" >>"%userProfile%\test2.txt" echo %char%& set c=6& goto Character4
if "%char%" == "7" >>"%userProfile%\test2.txt" echo %char%& set c=7& goto Character4
if "%char%" == "8" >>"%userProfile%\test2.txt" echo %char%& set c=8& goto Character4
if "%char%" == "9" >>"%userProfile%\test2.txt" echo %char%& set c=9& goto Character4
if "%char%" == "0" >>"%userProfile%\test2.txt" echo %char%& set c=0& goto Character4


:character4
set "char=4"

if "%char%" == "1" >>"%userProfile%\test2.txt" echo %char%& set d=1& goto check
if "%char%" == "2" >>"%userProfile%\test2.txt" echo %char%& set d=2& goto check
if "%char%" == "3" >>"%userProfile%\test2.txt" echo %char%& set d=3& goto check
if "%char%" == "4" >>"%userProfile%\test2.txt" echo %char%& set d=4& goto check
if "%char%" == "5" >>"%userProfile%\test2.txt" echo %char%& set d=5& goto check
if "%char%" == "6" >>"%userProfile%\test2.txt" echo %char%& set d=6& goto check
if "%char%" == "7" >>"%userProfile%\test2.txt" echo %char%& set d=7& goto check
if "%char%" == "8" >>"%userProfile%\test2.txt" echo %char%& set d=8& goto check
if "%char%" == "9" >>"%userProfile%\test2.txt" echo %char%& set d=9& goto check
if "%char%" == "0" >>"%userProfile%\test2.txt" echo %char%& set d=0& goto check

:check
echo %c%%d%

但是有更好的方法可以做到: - )

您可以使用FOR / F来验证字符 - 如果字符包含非数字,则FOR循环将触发,因此循环退出成功,因此||之后的错误处理代码将不会触发。如果字符仅包含数字,则循环不会触发,因此它会以错误退出,并执行错误处理代码。不再需要GOTO。奇怪的(call )只是一个神秘的语法,除了清除ERRORLEVEL(将其设置为0)之外什么都不做 - 它基本上是一个无操作。

set "char=3"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call ))||>>"%userProfile%\test2.txt" echo %char%&set "c=%char%"

set "char=4"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call ))||>>"%userProfile%\test2.txt" echo %char%&set "d=%char%"

echo %c%%d%

我会按如下方式编写代码 - 更容易阅读:

set "char=3"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call )) || (
  >>"%userProfile%\test2.txt" echo %char%
  set "c=%char%"
)

set "char=4"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call )) || (
  >>"%userProfile%\test2.txt" echo %char%
  set "d=%char%"
)

echo %c%%d%

您可以添加其他代码来处理无效(非数字)字符:

set "char=3"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call )) && (
  REM Handle invalid character here
) || (
  >>"%userProfile%\test2.txt" echo %char%
  set "c=%char%"
)

set "char=4"
(for /f "eol=1 delims=0123456789" %%C in ("%char%") do (call )) && (
  REM Handle invalid character here
) || (
  >>"%userProfile%\test2.txt" echo %char%
  set "d=%char%"
)

echo %c%%d%

答案 1 :(得分:1)

正如我在评论中所说,避免分配空格的方法是将变量名称及其值括在引号中;并且为了避免重定向中的空格,不要在>>个字符之前插入任何空格。但是,如果>>之前的字符是数字,则此方法失败,因为它被视为标准句柄号,因此在这种情况下,重定向应移动到命令的开头。这些更改包含在下面的代码中,可以获得与原始代码相同的结果,但更简单:

@echo off
setlocal EnableDelayedExpansion

rem Initialize "digits" variable
set "digits=/1/2/3/4/5/6/7/8/9/0/"

set char=3

rem If when try to delete "/%char%/" from "digits" variable, it change...
if "!digits:/%char%/=!" neq "%digits%" (
   rem then "char" variable contain a digit
   >> %userProfile%\test2.txt echo %char%& set "c=%char%" & goto Character4
)

:character4
set char=4

if "!digits:/%char%/=!" neq "%digits%" (
   >> %userProfile%\test2.txt echo %char%& set "d=%char%" & goto check
)

:check
echo %c%%d%

答案 2 :(得分:0)

如果您要分配算术变量(例如3),我建议您(例如set /a variable 1=3),这将删除指定值前/后的空格。基本上set /aset在首次分配时并没有太大的差异。但请记住,set被编程为分配字符串变量,因此将包含空格。