validation - How to verify if variable contains valid filename in Windows Batch -


i'm asking user provide filename in windows batch, using command:

set /p my_filename=enter filename project 

how can verify %my_filename% contains valid windows filename (not path) , re ask user if not?

edit file in question not yet exists when running script (it created script)

@echo off     setlocal enableextensions disabledelayedexpansion  :askfile      rem retrieve filename. on empty input ask again     set /p "my_file=enter filename project: " || goto :askfile      rem use delayed expansion avoid problems special characters     setlocal enabledelayedexpansion      rem disable delimiters , end of line characters in command     /f delims^=^ eol^= %%a in ("!my_file!") (         rem cancel delayed expansion avoid ! removal during expansion         endlocal          rem until checked, don't have valid file         set "my_file="          rem check don't have path, not folder , file exists         if /i "%%~a"=="%%~nxa" if not exist "%%~a\" if exist "%%~a" set "my_file=%%~nxa"     )      rem if don't have file name (it not valid) ask again     if not defined my_file goto :askfile      echo selected file "%my_file%" 

edited adapt comments

@echo off     setlocal enableextensions disabledelayedexpansion  :askfile      rem retrieve filename. on empty input ask again     set /p "my_file=enter filename project: " || goto :askfile      rem see naming files, paths, , namespaces     rem     https://msdn.microsoft.com/en-us/library/windows/desktop/aa365247%28v=vs.85%29.aspx      rem note: here, enabling/disabling delayed expansion      rem       avoid problems special characters      setlocal enabledelayedexpansion     rem ensure not have restricted characters in file name trying use them      rem delimiters , requesting second token in line     /f tokens^=2^ delims^=^<^>^:^"^/^\^|^?^*^ eol^= %%y in ("[!my_file!]") (         rem if here there second token, so, there 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 long         endlocal & goto :askfile     )      rem check path inclusion, file name correction     /f delims^=^ eol^= %%a in ("!my_file!") (         rem cancel delayed expansion avoid ! removal during expansion         endlocal          rem until checked, don't have valid file         set "my_file="          rem check don't have path          if /i not "%%~a"=="%%~nxa" (             echo error : paths not allowed             goto :askfile         )          rem check not folder          if exist "%%~nxa\" (             echo error : folder same name present              goto :askfile         )          rem ascii 0-31 check. check file name can created         2>nul ( >>"%%~nxa" type nul ) || (             echo error : file name not valid file system             goto :askfile         )          rem ensure not special file name trying delete newly created file         2>nul ( del /q /f /a "%%~nxa" ) || (             echo error : reserved file name used             goto :askfile         )          rem ok - have file name          set "my_file=%%~nxa"     )      echo selected file "%my_file%"     goto :eof 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -