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 ]
@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
尺有所短,寸有所长,学好CMD没商量。
考虑问题复杂化,解决问题简洁化。
考虑问题复杂化,解决问题简洁化。
