开发者

Applying substring operation to FOR token value (batch file)

开发者 https://www.devze.com 2023-03-07 07:14 出处:网络
(Edited question for clarity) Applying substring operation to a token value in a FOR does not work: for /F \"tokens=1 delims=\" %%G in (\"tokenvalue \") do (

(Edited question for clarity)

Applying substring operation to a token value in a FOR does not work:

for /F "tokens=1 delims=" %%G in ("tokenvalue ") do (
    Echo %%G:~-1
)

The substring operation does not delete the space at the end. Instead, :~-1 gets开发者_如何学编程 appended to the echoed result to produce:

tokenvalue :~-1


I cannot reproduce your problem here. Only when I append a space to the input file it also appears in the output file.

If you do

echo %%G >> D:\newfile.txt

then a space gets appended, obviously. This might be the case if you simplified your code before posting here.

If you indeed start out with a space in the input, then use the following:

setlocal enabledelayedexpansion
for /f "tokens=1 delims=" %%G in (D:\originalFile.txt) do (
    set "line=%%G"
    echo !line:~-1!>>D:\newfile.txt
)

You apply substring operations only to environment variables as the help already states.

In any case, if you're sure that the input file does not contain the trailing space, then you don't actually need the loop. A simple

type D:\originalFile.txt >> D:\newfile.txt

should suffice.

0

精彩评论

暂无评论...
验证码 换一张
取 消