![]() |
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-08-01 19:02 |
48,037 topics / 350,122 posts / today 2 new / 48,249 members |
| DOS批处理 & 脚本技术(批处理室) » How to preprocess user input? |
| Printable Version 1,043 / 5 |
| Floor1 wingofsea | Posted 2006-06-06 11:37 |
| 初级用户 Posts 34 Credits 124 | |
|
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.... |
|
| Floor2 bagpipe | Posted 2006-06-06 12:23 |
| 银牌会员 Posts 425 Credits 1,144 From 北京 | |
|
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 .......
|
|
| Floor3 willsort | Posted 2006-06-06 18:35 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 |
|
| Floor4 wingofsea | Posted 2006-06-08 10:59 |
| 初级用户 Posts 34 Credits 124 | |
|
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 |
|
| Floor5 willsort | Posted 2006-06-08 11:41 |
| 元老会员 Posts 1,512 Credits 4,432 | |
|
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 |
|
| Floor6 wentimao | Posted 2006-06-10 09:14 |
| 初级用户 Posts 27 Credits 110 | |
|
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.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |