DOS Batch : remove characters from string in a FOR loop

南笙酒味 提交于 2019-12-25 16:09:12

问题


I have a TXT file that contains :

C086002-B3116
C086014-T1234  
C086014-T1325
C086014-T1375" 
C086014-T1374"  

These strings include both trailing whitespaces and double quotes.

I want to remove these using a FOR loop :

for /f %%a in (file.txt) do (
    set str=%%a
    set str=%str: =%
    set str=%str:"=%
)

The Shell windows is opening and closing down immediately and nothing is done on the strings... Thanks for your help on this operation.


回答1:


You have to activate the delayed expansion for multiple change of a variable in a FORloop

Like this :

@echo off
setlocal enabledelayedexpansion
for /f  "delims=" %%a in (file.txt) do (
    set str=%%a
    set str=!str: =!
    set str=!str:"=!
    echo !str!
)

If you need the ouput in a file :

@echo off
setlocal enabledelayedexpansion
(for /f  "delims=" %%a in (file.txt) do (
    set str=%%a
    set str=!str: =!
    set str=!str:"=!
    echo !str!
)) >output.txt



回答2:


set str=    C086014-T1374" 
echo.%str%
set str=%str:"= %
echo.%str%.
set str=%str: =%
echo.%str%.

This is an example what you must put in the loop body. set str=%str:"= % replaces " with and set str=%str: =% removes spaces.

You can find more string manipulations here



来源:https://stackoverflow.com/questions/26711720/dos-batch-remove-characters-from-string-in-a-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!