如何验证变量是否在Windows Batch中包含有效的文件名 [英] How to verify if variable contains valid filename in Windows Batch

查看:94
本文介绍了如何验证变量是否在Windows Batch中包含有效的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要求用户使用以下命令在Windows Batch中提供文件名:

I'm asking user to provide filename in Windows Batch, by using this command:

set /p my_filename=Enter filename for this project

如何验证%my_filename%是否包含有效的Windows文件名(不是路径),并询问用户是否不包含该文件名?

How can I verify that %my_filename% contains valid Windows filename (not path) and re ask user if it does not?

编辑 运行脚本时,相关文件尚不存在(它将由脚本创建)

EDIT The file in question does not yet exists when running the script (it will be created by script)

推荐答案

@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

这篇关于如何验证变量是否在Windows Batch中包含有效的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆