The commands SET and IF can be used to check if a string assigned to an environment variable contains a specific character or string.
Example:
@echo off
setlocal EnableExtensions
for %%I in ("C:\Users\MikeW" "Folder\FileName" "Folder or file name") do call :ColonBackslash %%I
endlocal
goto :EOF
:ColonBackslash
set "Argument=%~1"
rem Substitute in argument string all colons by nothing and compare this
rem string with unmodified argument string. The argument string contains
rem a colon if the modified and the unmodified strings are not equal.
if not "%Argument::=%" == "%Argument%" (
echo Colon in argument: %Argument%
goto :EOF
)
rem Substitute in argument string all backslashes by nothing and compare
rem this string with unmodified argument string. The argument string
rem contains a backslash if the modified and the unmodified strings
rem are not equal.
if not "%Argument:\=%" == "%Argument%" (
echo Backslash in argument: %Argument%
goto :EOF
)
echo No colon or backslash in: %Argument%
goto :EOF
This batch file outputs:
Colon in argument: C:\Users\MikeW
Backslash in argument: Folder\FileName
No colon or backslash in: Folder or file name
Comparing strings with substituted characters/strings with unmodified string can be used to determine type of input string as demonstrated with the batch code below:
@echo off
setlocal EnableExtensions
for %%I in ("C:\Users\MikeW" "C:\Users\Mi:keW" "documentation" "\\server\share\folder" "\\server\share\folder1\..\folder2" "\\server\share\fo:lder" "\\server\" "\\server\share\folder\..\" "%SystemRoot%\..\" ".\Folder" "..\..\Folder" "\Windows" ">:Invalid") do call :classifyInput %%I
endlocal
goto :EOF
:classifyInput
rem Subroutine called with no parameter or with just "" as parameter.
if "%~1" == "" goto :EOF
set "Argument=%~1"
rem Replace all forward slashes by backslashes in argument string.
set "Argument=%Argument:/=\%"
rem Is the second character a colon and third character a backlash?
if "%Argument:~1,2%" == ":\" goto AbsolutePath
rem Is there a colon anywhere else than at second position in argument?
rem C:Folder is interpreted as invalid argument and not as relative
rem path to subfolder "Folder" of current directory on drive C:.
if not "%Argument::=%" == "%Argument%" (
echo Invalid argument: %1
goto :EOF
)
rem Starts the argument with two backslashes?
if "%Argument:~0,2%" == "\\" goto PathUNC
rem Starts the argument with \ for a path relative to current drive?
if "%Argument:~0,1%" == "\" (
echo Relative path: %1
goto :EOF
)
rem Starts the argument with .\ for a path relative to current directory?
if "%Argument:~0,2%" == ".\" (
echo Relative path: %1
goto :EOF
)
rem Starts the argument with ..\ for a path relative to parent directory?
if "%Argument:~0,3%" == "..\" (
echo Relative path: %1
goto :EOF
)
rem Contains the argument a backslash for a path relative to current directory?
if not "%Argument:\=%" == "%Argument%" (
echo Relative path: %1
goto :EOF
)
echo Name without path: %1
goto :EOF
:AbsolutePath
set "Remaining=%Argument:~2%"
rem Is there a colon anywhere else after second position in argument?
if not "%Remaining::=%" == "%Remaining%" (
echo Invalid argument: %1
goto :EOF
)
rem Is the first character a drive letter if second character is a colon?
set "FirstIsLetter="
for /F "delims=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" %%# in ("%Argument:~0,1%") do set "FirstIsLetter=%%#"
if not "%FirstIsLetter%" == "" (
echo Invalid argument: %1
goto :EOF
)
rem Contains the absolute path also .\ or ..\ in path?
if "%Argument:.\=%" == "%Argument%" (
echo Absolute path: %1
) else (
echo Abs. + rel. path: %1
)
goto :EOF
:PathUNC
rem Does the UNC path contain also a colon?
if not "%Argument::=%" == "%Argument%" (
echo Invalid argument: %1
goto :EOF
)
set "ServerName="
set "ShareName="
set "Remaining="
for /F "tokens=1,2* delims=\" %%A in ("%Argument%") do (
set "ServerName=%%A"
set "ShareName=%%B
set "Remaining=%%C"
)
rem Is there no share name specified after server name in UNC path?
if "%ShareName%" == "" (
echo Invalid argument: %1
goto :EOF
)
rem Is there an invalid share name specified after server name in UNC path?
if "%ShareName%" == "." (
echo Invalid argument: %1
goto :EOF
)
rem Is there an invalid share name specified after server name in UNC path?
if "%ShareName%" == ".." (
echo Invalid argument: %1
goto :EOF
)
rem Contains the UNC path also .\ or ..\ in remaining path?
if "%Remaining:.\=%" == "%Remaining%" (
echo UNC path: %1
) else (
echo UNC + rel. path: %1
)
goto :EOF
The output is:
Absolute path: "C:\Users\MikeW"
Invalid argument: "C:\Users\Mi:keW"
Name without path: "documentation"
UNC path: "\\server\share\folder"
UNC + rel. path: "\\server\share\folder1\..\folder2"
Invalid argument: "\\server\share\fo:lder"
Invalid argument: "\\server\"
UNC + rel. path: "\\server\share\folder\..\"
Abs. + rel. path: "C:\WINDOWS\..\"
Relative path: ".\Folder"
Relative path: "..\..\Folder"
Relative path: "\Windows"
Invalid argument: ">:Invalid"
But this batch code is not complete for checking for invalid paths, folder or file names in an argument string. There are still a lot of possible errors not detected by this batch code.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
set /?
setlocal /?
It would be better to let Windows kernel check if the argument specifies an existing folder or file independent if the argument string is without or with a path of any type and being valid at all. See the topics:
And in case of testing for validity on non existing folder/file, just create the file/folder, suppress the error message using 2>nul
(see the Microsoft article Using command redirection operators) and check with if errorlevel 1
or with if exist "%~1"
if the folder/file could be created successfully, or if this operation failed because of an invalid folder/file name string, denied access, missing permissions, etc.