中国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-08-10 17:00
47,811 topics / 349,897 posts / today 0 new / 48,256 members
DOS批处理 & 脚本技术(批处理室) » Batch script to judge whether the input file name is legal
Printable Version  1,980 / 5
Floor1 namejm Posted 2007-05-28 22:08
荣誉版主 Posts 1,737 Credits 5,226 From 成都
In many cases, we need to manually enter a file name in the CMD window to create a file. For example, the File Backup Tool I once made. At this time, it is often necessary to detect illegal characters in the input string. However, there are so many characters that cannot be used as file names: various control symbols, /, \, >, <, ", system device reserved characters (such as nul, com1, etc.)... If we use the exhaustive method, regardless of how painstaking the processing of CMD special characters is, just the large number of illegal characters, I'm afraid no one can easily list them all. Due to the headache of special characters and the worry of missing something, I haven't found a suitable solution before. Now I have time, I wrote a piece of code, used to demonstrate whether the string entered as a file name contains illegal characters, and post it for everyone to discuss:


[ Last edited by namejm on 2007-5-29 at 11:28 PM ]
Floor2 qzwqzw Posted 2007-05-29 19:57
银牌会员 Posts 636 Credits 2,343
It's really a pity that quotes are not supported. The "filename" can't pass. Why? And it's said to create a file, but the result is to create a folder. It seems there is some difference.
Floor3 vkill Posted 2007-05-29 20:26
金牌会员 Posts 1,744 Credits 4,103 From 甘肃.临泽
...........

[ Last edited by vkill on 2007-5-29 at 08:30 PM ]
Floor4 namejm Posted 2007-05-29 20:27
荣誉版主 Posts 1,737 Credits 5,226 From 成都
Originally posted by qzwqzw at 2007-5-29 19:57:
It's really a pity that quotes are not supported
"filename" can't pass
Why is that?

And it is said to create a file
But what is created is a folder
It seems there is some difference, right

  Quotation marks cannot be used as the name of a file (folder), so "filename" cannot pass.

  Hehe, under the XP system, the naming rules for folder names and file names should be the same, right? I didn't think carefully at that time and casually wrote a code to create a folder. I'd better change it to a code to create a random file.

[ Last edited by namejm on 2007-5-29 at 08:48 PM ]
Floor5 qzwqzw Posted 2007-05-29 22:20
银牌会员 Posts 636 Credits 2,343
Then what about the file name with spaces dragged in directly?
Do I still have to manually delete the quotes before and after?
Moreover, the processing of quotes is not that complicated

The sentence for processing quotes can also be placed in the check
Because it's all related to check

Also, this sentence is so awkward

cd.>"%tmp%\%input%%rnd%" 2>nul && (
del /q "%tmp%\%input%%rnd%"
) || (
goto main
)

Isn't it more convenient and worry-free directly like this?

cd.>"%tmp%\%input%%rnd%" 2>nul || goto main
del /q "%tmp%\%input%%rnd%"

-------------------------------

Other issues
I found that after errors such as cd, echo, etc., it doesn't return errorleve
Nor set %errorlevel%l
But &&, || still work
It seems that cmd uses another communication mechanism

echo.>"%tmp%\%input%%rnd%" & echo %errorlevel%
cd.>"%tmp%\%input%%rnd%" & if not errorlevel 1 echo 0
Floor6 namejm Posted 2007-05-29 23:06
荣誉版主 Posts 1,737 Credits 5,226 From 成都
1. Forgot to mention, the original intention of writing this code is to demonstrate how to judge whether the input file name is legal, and it does not consider the convenience of use, so it does not judge the complete path. In other words, it only judges the file name without considering the path. If you want to judge the path, you have to rewrite the code;
2. The statement to judge the quotation marks should indeed be placed in the :check tag segment. In this way, one more tag symbol has to be written. Hehe, in order to save this tag, I put it in the front. Well, I will change it immediately;
3. The compound statements of && and || do look awkward, just do as brother qzwqzw said.
[ Contact the Union admin team - 中国DOS联盟 - Standard version ]
Sponsored by ifanr Inc | © 2001–2023