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 17:57
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to preprocess user input? View 1,042 Replies 5
Original Poster Posted 2006-06-06 11:37 ·  中国 北京 联通
初级用户
Credits 124
Posts 34
Joined 2006-05-23 11:38
20-year member
UID 55845
Status Offline
Here is a requirement:

The user is asked to input a path, the batch program reads that path and concatenates it with the relative path defined in the batch, so problems like the following may be encountered:

@echo off
set log=\err.log
set /p installoc=please input the path of utility installation location:
pushd %installoc% 2>nul
popd
set a_path=%installoc%%log%
echo The path of log file is %a_path%

If the user inputs c:\utility\,
then a_path becomes c:\utility\\err.log. Please note that there will be two \\ here. How can this absolute path be normalized so that the output is c:\utility\err.log,

Thanks, still learning....
Floor 2 Posted 2006-06-06 12:23 ·  中国 北京 联通
银牌会员
★★★
DOS联盟捡破烂的
Credits 1,144
Posts 425
Joined 2005-10-20 00:00
20-year member
UID 43784
From 北京
Status Offline
The OP seems to have been a bit careless. Your first variable is defined as \err.log, and the path you input is c:\utility\, which already gives two \\ symbols, so of course the path displays as c:\utility\\err.log. If your first variable were ERR.LOG, or if the path you input were C:\UTILITY, then this \\ symbol probably would not appear .......
Floor 3 Posted 2006-06-06 18:35 ·  中国 山西 运城 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re wingofsea:

First, in CMD, “\\” is valid and is basically equivalent to “\”, so usually there is no need for “preprocessing”.

Second, if preprocessing is needed, you can use a for statement, as follows:

for %%p in ("%installoc%%log%") do set a_path=%%~p
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 4 Posted 2006-06-08 10:59 ·  中国 北京 联通
初级用户
Credits 124
Posts 34
Joined 2006-05-23 11:38
20-year member
UID 55845
Status Offline
to bagpipe:
Thanks.
First, c:\utility\ is entered by the user. The path input is controlled by the user; they can enter c:\utility\ or c:\utility, so preprocessing is needed.

to willsort:
Thanks. Because the program will output this a_path to the terminal, if the user sees c:\utility\\err.log, it will look a bit awkward, so I want to normalize it. But the statement you gave still doesn't seem to work:

@echo off
set log=\err.log
set /p installoc=please input the path of utility installation location:
pushd %installoc% 2>nul
popd
for %%p in ("%installoc%%log%") do set a_path=%%~p
echo %a_path%
pause
Floor 5 Posted 2006-06-08 11:41 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re wingofsea:

Sorry!

In the example code, the replaceable variable %%p was missing the modifier letter f, as follows:
for %%p in ("%installoc%%log%") do set a_path=%%~fp

Also, the following method can be used to only replace \\ with \ :
set a_path=%installoc%%log%
set a_path=%a_path:\\=\%

But path normalization is always more complicated than we imagine, especially when the path is freely entered by the user, because there can be many unexpected situations. For example, illegal characters may appear in the path, or the path may contain both spaces and quotation marks, and so on. This brings a lot of difficulty to our coding. If you are interested in going deeper, you can refer to the following information:

How to remove quotation marks from parameters passed in by the user?
http://www.cn-dos.net/forum/viewthread.php?tid=20838
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 6 Posted 2006-06-10 09:14 ·  中国 辽宁 大连 电信
初级用户
Credits 110
Posts 27
Joined 2006-06-03 17:25
20-year member
UID 56491
Status Offline
Just use if not exist "%a_path%\" to check whether the directory exists. If it doesn't, then the input was wrong, so jump back and have it re-entered.
xp,cmd
Forum Jump: