0

I'm developing a pure Windows batch tool FileNameValidator that checks if supplied with batch arguments or typed by a user file paths & names are absolute & valid under current Windows version, before user code is run using these paths\names. The tool is different from snippets posted on this topic on SO, as it presents an automated user input validator for multiple path & file names entered either as batch arguments or typed manually. Other threads solutions are based on manual single path or name check by using regex. This tool checks path & file validity by test-writing a target folder to disk. So far the tool can check up to 9 local or network filepath & name arguments. "Local drives only" limitation was lifted.

I ask, how to a) check more than 9 arguments, b) lift user input requirements stated in code comments. None of these questions were addressed in a few SO threads on this topic. The tool is purely batch based, while other topics discuss C++ regex, or powershell related techniques, or limited batch regex supported by FINDSTR.

@echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Checks if files name.ext or path entered as arguments or typed are absolute & valid in installed Windows version
:: by trying to create a new directory with a given path\name, if not exists already and target disk is accesible
:: If missing in batch arguments or invalid, user can type new or add to ":default" & choose default file names or paths
:: File variable names must use matching 3-letter ID with "save to" directory path variables (i.e. esd_dir\esd_file)
:: Default values, path & name variables, and arguments numbering must match ([arg1 & dir1 & pat1],[var.iso & file.iso])

:default
set "mes1=  Invalid dir path or file name" & set "mes2=  The dir or file already exists"
set "mes3=Use (d)efault, e(x)isting, or (r)e-enter value?" & set "mes4=  Entered name is a directory"
set "mes5=value used:" & set "mes6=  No default value exists" & set "mes7=is empty or invalid"
set "mes8=All arguments are entered" & set "mes9=C(o)ntinue, (r)e-enter arguments, or e(x)it?"
set "pat1=winsource_dir" & set "pat2=iso_dir" & set "pat3=esd_dir" & set "pat4=esd_file.iso"
set "dir1=K:\Temp\Tests\Windows_files" & set "dir2=K:\Temp\Tests\Modified_iso"
set "dir3=K:\Temp\Tests\Esd_files" & set "dir4=Final.iso" & set "i=1" & echo/

:input
if not defined pat%i% (echo/ & echo %mes8% & echo/ & set /a "i-=1
    (for /l %%k in (1,1,!i!) do (echo   !pat%%k! = !path%%k!)) & echo/ 
    choice /c orx /n /m "%mes9%" /t 20 /d o & echo/
        if errorlevel 3 (goto :end) else if errorlevel 2 (set "i=1" & set "ver=" & goto :input) else (goto :core)
) else if not defined ver (call set "path%i%=%%~%i%"
    if not defined path%i% (echo   Batch arg%i% %mes7% & set "ver=1"
    ) else (echo Read arg%i% = !path%i%!))
if defined ver (set "ver=" & set /p "path%i%=Enter !pat%i%! > " 2>nul)
set "!pat%i%!=!path%i%!" & set "cho="
if "!pat%i%:~-4,1!"=="." (
    if not "!path%i%:~-4!"=="!pat%i%:~-4!" (echo %mes1% & set "cho=1"
    ) else ( set "break=" & for /l %%j in (1,1,9) do (
        if not defined break if "!pat%%j:~0,3!"=="!pat%i%:~0,3!" (set "break=TRUE"
        set "pathf%i%=!path%i%!" & set "path%i%=!path%%j!\!path%i%!"
        if not exist !path%%j! set "pathp%i%=!path%%j!")))
) else if not exist "!path%i%:~0,3!" if not "!path%i%:~0,2!"=="\\" (echo %mes1% & set "cho=1")
if exist "!path%i%!" if not defined cho (
        if exist "!path%i%!\*" if "!pat%i%:~-4,1!"=="." (echo %mes4% & set "cho=1"
        ) else (echo %mes2% & set "cho=2"))
if not defined cho (md "!path%i%!" 2>nul
    if errorlevel 1 (echo %mes1% & set "cho=1"
    ) else ( rmdir /s /q "!path%i%!" & set "cho="
        (if "!pat%i%:~-4,1!"=="." set "path%i%=!pathf%i%!") & echo   Entered %mes5% !path%i%!)
    if defined pathp%i% rmdir /s /q "!pathp%i%!")
if defined cho (choice /c dxr /n /m "%mes3%" /t 20 /d d
    if errorlevel 3 (set "ver=1") else if errorlevel 2 (
        if !cho! equ 2 (set "ver=" & echo   Existing %mes5% !path%i%!) else (set "ver=1" & echo %mes1%)
    ) else (if not "!dir%i%!"=="" (set "path%i%=!dir%i%!" & set "ver=" & echo   Default %mes5% !dir%i%!
        ) else (set "ver=1" & echo %mes6%)))
(if not defined ver set /a "i+=1") & goto :input

:core
:: add your code here that uses pat# variable values as path\file variable names

:end
echo/ & echo Exiting program...
timeout /t 1 /nobreak >nul
exit /b

Here's a sample use case:

K:\Temp\Tests>test.bat "K:\Temp\Tests1" "K:\Temp\Tests]>" "" "Final:.iso"

Read arg1 = K:\Temp\Tests1
  Entered value used: K:\Temp\Tests1
Read arg2 = K:\Temp\Tests]>
  Invalid dir path or file name
Use (d)efault, e(x)isting, or (r)e-enter value? R
Enter iso_dir > K:\Temp\Tests2
  Entered value used: K:\Temp\Tests2
  Batch arg3 is empty or invalid
Enter esd_dir > \\MyNetworkDrive\Test
  Invalid dir path or file name
Use (d)efault, e(x)isting, or (r)e-enter value? D
  Default value used: K:\Temp\Tests\Esd_files
Read arg4 = Final:.iso
  Invalid dir path or file name
Use (d)efault, e(x)isting, or (r)e-enter value? R
Enter esd_file.iso > Final.txt
  Invalid dir path or file name
Use (d)efault, e(x)isting, or (r)e-enter value? R
Enter esd_file.iso > Final.iso
  Entered value used: Final.iso

All arguments are entered

  winsource_dir = K:\Temp\Tests1
  iso_dir = K:\Temp\Tests2
  esd_dir = K:\Temp\Tests\Esd_files
  esd_file.iso = Final.iso

C(o)ntinue, (r)e-enter arguments, or e(x)it?

Exiting program...

K:\Temp\Tests>
sambul35
  • 1,058
  • 14
  • 22
  • 4
    If the code works but you just want to make it better, you should post this on CodeReview instead. – SomethingDark Jul 04 '16 at 13:13
  • At present it doesn't work in particular for network drives. Not sure, how to classify it correctly on this site? – sambul35 Jul 04 '16 at 13:25
  • 2
    Possible duplicate of [Check if an absolute path of a directory or a file name are valid](http://stackoverflow.com/questions/38169201/check-if-an-absolute-path-of-a-directory-or-a-file-name-are-valid) – Mofi Jul 04 '16 at 17:03
  • The thread you linked above is itself a possible duplicate of [How do I check if a given string is a legal / valid file name under Windows?](http://stackoverflow.com/questions/62771/how-do-i-check-if-a-given-string-is-a-legal-valid-file-name-under-windows), which presents a series of comprehensive regex validator examples. I can't add a comment about it in that thread due to high threshold required. – sambul35 Jul 04 '16 at 17:37
  • 1
    @sambul35 [Check if an absolute path of a directory or a file name are valid](http://stackoverflow.com/questions/38169201/) is NOT a duplicate of [How do I check if a given string is a legal / valid file name under Windows?](http://stackoverflow.com/questions/62771/) because the first one is for directory/file path/name checking from within a batch file using Windows standard console commands while the second one is for doing that within a C# application using C# code. That is completely different although what is a valid directory/file path/name is of course the same. – Mofi Jul 05 '16 at 04:55

0 Answers0