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-06-26 01:01
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » Batch script to judge whether the input file name is legal View 1,892 Replies 5
Original Poster Posted 2007-05-28 22:08 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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:

@echo off
:: Idea: Create a random file in the system temporary directory and leave the task of detecting illegal characters to the operating system
:: Thanks to qzwqzw

:main
cls
set input=
set /p input= Please enter the file name:
call :check
pause
goto main

:check
set "str1=%input:"=%"
set "str2=%input:"= %"
if not "%str1%"=="%str2%" goto main
:loop
set rnd=%random%
if exist "%tmp%\%input%%rnd%" goto loop
cd.>"%tmp%\%input%%rnd%" 2>nul || goto main
del /q "%tmp%\%input%%rnd%"
echo "%input%" is a legal file name
goto :eof


[ Last edited by namejm on 2007-5-29 at 11:28 PM ]
Recent Ratings for This Post ( 2 in total) Click for details
RaterScoreTime
youxi01 +8 2007-05-28 22:43
lxmxn +8 2007-05-28 23:00
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 2 Posted 2007-05-29 19:57 ·  中国 山西 运城 联通
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
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.
Floor 3 Posted 2007-05-29 20:26 ·  中国 甘肃 张掖 电信
金牌会员
★★★★
Credits 4,103
Posts 1,744
Joined 2006-01-20 13:00
20-year member
UID 49241
Gender Male
From 甘肃.临泽
Status Offline
...........

[ Last edited by vkill on 2007-5-29 at 08:30 PM ]
Floor 4 Posted 2007-05-29 20:27 ·  中国 广东 电信
荣誉版主
★★★★
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 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 ]
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Floor 5 Posted 2007-05-29 22:20 ·  中国 江西 南昌 电信
银牌会员
★★★
天的白色影子
Credits 2,343
Posts 636
Joined 2004-03-06 00:00
22-year member
UID 19350
Gender Male
Status Offline
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
Floor 6 Posted 2007-05-29 23:06 ·  中国 广东 电信
荣誉版主
★★★★
batch fan
Credits 5,226
Posts 1,737
Joined 2006-03-10 00:38
20-year member
UID 51697
From 成都
Status Offline
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.
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
Forum Jump: