问题
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 FOR
loop
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