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-08-01 10:20
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to ignore the spaces before and after the path entered by the user? View 13,403 Replies 6
Original Poster Posted 2006-11-25 11:46 ·  中国 北京 联通
初级用户
Credits 124
Posts 34
Joined 2006-05-23 11:38
20-year member
UID 55845
Status Offline
To remove the leading and trailing spaces from the user input, you can use the `for /f` command to trim the whitespace. Modify the code as follows:

```batch
set /p installoc=
for /f "tokens=* delims=" %%a in ("%installoc%") do set installoc=%%a
set installoc=%installoc:"=%
if NOT EXIST "%installoc%\key.txt" (
echo.
echo."%installoc%" you specified is invalid.
echo.
goto GetPath
)
```

This code uses `for /f` with the `tokens=* delims=` options to trim any leading or trailing spaces from the `installoc` variable.
Floor 2 Posted 2006-11-28 06:49 ·  中国 北京 联通
初级用户
Credits 124
Posts 34
Joined 2006-05-23 11:38
20-year member
UID 55845
Status Offline
Searched around, it seems there's no solution in this regard. Asking everyone for help, thanks.
Floor 3 Posted 2006-11-28 21:30 ·  中国 江苏 苏州 联通
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
Removing leading spaces can be done in one step with "for /f "tokens=1*"", but I haven't thought of a way to remove trailing spaces without using a loop. Looking forward to a great idea.
Recent Ratings for This Post ( 1 in total) Click for details
RaterScoreTime
redtek +3 2006-11-29 02:18
Floor 4 Posted 2006-11-29 02:17 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Brother wingofsea's line of code in post #1, "set /p installoc=", is Windows CMD Shell code, which is not purely DOS content. So those who often go to "DOS Batch & Script Technology (Batch Room)" to exchange "various script writing and related technologies under the WIN prompt..." didn't notice, fortunately Brother NaturalJ0 noticed that the post you sent is related to Windows CMD Shell batch processing content ~ : )

DOS Learning Introduction & Wonderful Articles (Teaching Room)
http://www.cn-dos.net/forum/forumdisplay.php?fid=12
(Everyone can learn, exchange and share basic knowledge and experiences about DOS introduction and installation here,)
(Develop novices into various DOS experts to achieve the common promotion and development of DOS.)



DOS Batch & Script Technology (Batch Room)
http://www.cn-dos.net/forum/forumdisplay.php?fid=23
(Everyone can discuss issues related to batch processing under DOS, various script writing under DOS and WIN prompts and related technologies here)
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 5 Posted 2006-11-29 02:45 ·  中国 北京 联通
金牌会员
★★★★
Credits 2,902
Posts 1,147
Joined 2006-09-21 12:00
19-year member
UID 63324
Gender Male
Status Offline
Because it's a pure DOS discussion area, so the implementation method of the Windows CMD Shell is posted to:


http://www.cn-dos.net/forum/viewthread.php?tid=25172&sid=mD79Lj
    Redtek,一个永远在网上流浪的人……

_.,-*~'`^`'~*-,.__.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._,_.,-*~'`^`'~*-,._
Floor 6 Posted 2006-11-29 07:45 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
Originally posted by NaturalJ0 at 2006-11-28 08:30:
To remove leading spaces, you can do it in one step with "for /f "tokens=1*"", but I haven't thought of a way to remove trailing spaces in one go without a loop. Looking forward to a great idea.

  If there are still spaces in the middle of the string, this solution will remove all the spaces encountered the second time.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 7 Posted 2020-05-08 18:09 ·  中国 宁夏 移动
初级用户
Credits 34
Posts 15
Joined 2018-12-28 19:36
7-year member
UID 183309
Gender Male
Status Offline
Create a batch file a.bat

set a=%1 %2 %3 %4 %5 %6 %7 %8 %9

Original batch file:
...
call a i Hate Space But "I don't think so".
echo %a%
rem i Hate Space But "I don't think so".
...
The link on the 5th floor is broken, and I can't even take a look.
If there are people who enter paths like this, there's no need to be too hard on yourself; that's their problem.

[[i] Last edited by asdfq on 2020-5-8 at 18:15 [/i]]
Forum Jump: