China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-07 01:56
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to delete all spaces at the beginning of each line in a batch file? View 2,137 Replies 7
Original Poster Posted 2007-10-14 18:48 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 116
Posts 42
Joined 2007-07-29 17:20
18-year member
UID 94255
Gender Male
Status Offline
I have read this post http://www.cn-dos.net/forum/viewthread.php?tid=25172

The method in it can handle ordinary documents, but there will be problems when used to handle batch files. I want to ask if it can be done through findstr (intercepting from the first non - space character of each line to the last character).
Floor 2 Posted 2007-10-14 19:51 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
```@echo off
for /f "tokens=*" %%a in (test.txt) do echo %%a
pause
```
Floor 3 Posted 2007-10-14 20:09 ·  中国 辽宁 本溪 联通
银牌会员
★★★
Credits 1,212
Posts 464
Joined 2006-12-13 21:11
19-year member
UID 73417
Gender Male
Status Offline
VBS function - LTRIM is easier...
Floor 4 Posted 2007-10-14 21:36 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 116
Posts 42
Joined 2007-07-29 17:20
18-year member
UID 94255
Gender Male
Status Offline
Originally posted by lxmxn at 2007-10-14 07:51 PM:
@echo off
for /f "tokens=*" %%a in (test.txt) do echo %%a
pause

Is this???


This is in that post...
No...

When encountering such a statement, the output changes
echo           ------------------------------
echo ^| Invalid Program ^|
echo ------------------------------

After output

echo ------------------------------
echo | Invalid Program |
echo ------------------------------


As a result, when running, the vertical bar "|" will be treated as a pipe, and the result will be wrong...
Floor 5 Posted 2007-10-14 21:53 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline
Please make the problem clearer, I don't quite understand.

I tested and there are no problems.
Floor 6 Posted 2007-10-14 22:17 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 116
Posts 42
Joined 2007-07-29 17:20
18-year member
UID 94255
Gender Male
Status Offline
It is the text in the original file:
echo ------------------------------
echo ^| ^|
echo ^| 无效程序 ^|
echo ------------------------------
After the processing of removing spaces, it becomes like this:
echo ------------------------------
echo ^| ^|
echo | 无效程序 |
echo ------------------------------
Only when there are Chinese characters will this happen. In the second line of echo, it is still output as originally, but in the third line, the original ^| is output as |, and in this way, executing the program after removing spaces will cause errors...
Floor 7 Posted 2007-10-14 23:54 ·  中国 湖北 武汉 电信
版主
★★★★★
Credits 11,386
Posts 4,938
Joined 2006-07-23 17:10
19-year member
UID 59080
Status Offline

# type test.txt
echo ------------------------------
echo ^| ^|
echo ^| Invalid Program ^|
echo ------------------------------

# type Deal_test.bat
@echo off
for /f "tokens=*" %%a in (test.txt) do echo %%a
pause

# Deal_test.bat
echo ------------------------------
echo ^| ^|
echo ^| Invalid Program ^|
echo ------------------------------
Press any key to continue. . .

#
Floor 8 Posted 2007-10-15 22:08 ·  IANA 局域网IP(Private-Use)
初级用户
Credits 116
Posts 42
Joined 2007-07-29 17:20
18-year member
UID 94255
Gender Male
Status Offline
Hehe... I tested it and it really works... I used the same command... Why was it like that that day...
Sweat --||
Maybe it's a problem with the machine...
By the way, post a small program of the streamlined batch file I made. It has only 3 lines. Don't laugh at me oh...

(Function: Delete comments, delete blank lines and spaces at the beginning of each line)
@echo off
if %1.==. (set /p file=input:) else (set file=%1)
for /f "delims=" %%i in ("%file%") do (set file="%%~i"&set file0=%%~dpi&set file1=%%~nxi)
for /f "tokens=*" %%a in ('findstr /v "REM ::" %file%') do echo %%a>>"%file0%Deal_%file1%"
Forum Jump: