且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何验证变量是否在Windows Batch中包含有效的文件名

更新时间:2023-11-29 13:20:28

@echo off
    setlocal enableextensions disabledelayedexpansion

:askFile

    rem Retrieve filename. On empty input ask again
    set /p "my_file=Enter filename for this project: " || goto :askFile

    rem Use delayed expansion to avoid problems with special characters
    setlocal enabledelayedexpansion

    rem Disable delimiters and end of line characters in for command
    for /f delims^=^ eol^= %%a in ("!my_file!") do (
        rem Cancel delayed expansion to avoid ! removal during expansion
        endlocal

        rem Until checked, we don't have a valid file
        set "my_file="

        rem Check we don't have a path, it is not a folder and the file exists
        if /i "%%~a"=="%%~nxa" if not exist "%%~a\" if exist "%%~a" set "my_file=%%~nxa"
    )

    rem If we don't have a file name (it was not valid) ask again
    if not defined my_file goto :askFile

    echo Selected file is "%my_file%"

已编辑以适应评论

@echo off
    setlocal enableextensions disabledelayedexpansion

:askFile

    rem Retrieve filename. On empty input ask again
    set /p "my_file=Enter filename for this project: " || goto :askFile

    rem See Naming Files, Paths, and Namespaces
    rem     https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx

    rem NOTE: From here, we will be enabling/disabling delayed expansion 
    rem       to avoid problems with special characters

    setlocal enabledelayedexpansion
    rem Ensure we do not have restricted characters in file name trying to use them as 
    rem delimiters and requesting the second token in the line
    for /f tokens^=2^ delims^=^<^>^:^"^/^\^|^?^*^ eol^= %%y in ("[!my_file!]") do (
        rem If we are here there is a second token, so, there is a special character
        echo Error : Non allowed characters in file name
        endlocal & goto :askFile
    )

    rem Check MAX_PATH (260) limitation
    set "my_temp_file=!cd!\!my_file!" & if not "!my_temp_file:~260!"=="" (
        echo Error : file name too long
        endlocal & goto :askFile
    )

    rem Check path inclusion, file name correction
    for /f delims^=^ eol^= %%a in ("!my_file!") do (
        rem Cancel delayed expansion to avoid ! removal during expansion
        endlocal

        rem Until checked, we don't have a valid file
        set "my_file="

        rem Check we don't have a path 
        if /i not "%%~a"=="%%~nxa" (
            echo Error : Paths are not allowed
            goto :askFile
        )

        rem Check it is not a folder 
        if exist "%%~nxa\" (
            echo Error : Folder with same name present 
            goto :askFile
        )

        rem ASCII 0-31 check. Check file name can be created
        2>nul ( >>"%%~nxa" type nul ) || (
            echo Error : File name is not valid for this file system
            goto :askFile
        )

        rem Ensure it was not a special file name by trying to delete the newly created file
        2>nul ( del /q /f /a "%%~nxa" ) || (
            echo Error : Reserved file name used
            goto :askFile
        )

        rem Everything was OK - We have a file name 
        set "my_file=%%~nxa"
    )

    echo Selected file is "%my_file%"
    goto :eof