如何删除字符串中的空格并将其替换为(;)批处理

时间:2015-03-27 23:27:04

标签: batch-file

如何从此字符串中删除空格或用批处理中的;替换空格?

ECHO    V00000023903507 92  30573185    E28FC1  2015079 18-04-2015  27-04-2015  >>  %EXT%

我希望它看起来像这样:

ECHO V00000017495345;90;30485020;FD2ECC;2015079;08-04-2015;19-04-2015 >> %EXT%

1 个答案:

答案 0 :(得分:0)

您可以使用延迟展开和for循环。

@echo off
setlocal enabledelayedexpansion

set "str=    V00000023903507 92  30573185    E28FC1  2015079 18-04-2015  27-04-2015  "

set "fixed="
for %%I in (%str%) do (
    if not defined fixed (set "fixed=%%I") else set "fixed=!fixed!;%%I"
)

>>outfile.txt echo %fixed%

或者您可以使用variable substring substitution

@echo off
setlocal

set "str=    V00000023903507 92  30573185    E28FC1  2015079 18-04-2015  27-04-2015  "

rem // replace tab with space (the blank character after : is a tab)
set "str=%str:  = %"

rem // replace all multiple spaces with single
set "str=%str:  = %"

rem // replace all spaces with ;
set "str=%str: =;%"

rem // remove first and last character
set "str=%str:~1,-1%"

>> outfile echo %str%