中国DOS联盟论坛

China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --
Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
Guest | Log in | Register | Members | Search | China DOS Union
中国DOS联盟论坛
The time now is 2026-09-23 12:56
47,812 topics / 349,917 posts / today 0 new / 48,273 members
DOS批处理 & 脚本技术(批处理室) » How to ignore the spaces before and after the path entered by the user?
Printable Version  13,501 / 6
Floor1 wingofsea Posted 2006-11-25 11:46
初级用户 Posts 34 Credits 124
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.
Floor2 wingofsea Posted 2006-11-28 06:49
初级用户 Posts 34 Credits 124
Searched around, it seems there's no solution in this regard. Asking everyone for help, thanks.
Floor3 NaturalJ0 Posted 2006-11-28 21:30
银牌会员 Posts 533 Credits 1,181
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.
Floor4 redtek Posted 2006-11-29 02:17
金牌会员 Posts 1,147 Credits 2,902
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)
Floor5 redtek Posted 2006-11-29 02:45
金牌会员 Posts 1,147 Credits 2,902
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
Floor6 namejm Posted 2006-11-29 07:45
荣誉版主 Posts 1,737 Credits 5,226 From 成都
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.
Floor7 asdfq Posted 2020-05-08 18:09
初级用户 Posts 15 Credits 34
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]]
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023