我开发了一个纯Windows批处理工具 FileNameValidator ,用于检查是否提供了批处理参数,或者是否由用户文件路径&名字是绝对的&在使用这些路径\名称运行用户代码之前,在当前Windows版本下有效。该工具不同于在SO上发布在该主题上的片段,因为它提供了用于多路径和自动化的自动用户输入验证器。文件名既可以作为批处理参数输入,也可以手动输入。其他线程解决方案基于手动单路径或使用正则表达式进行名称检查。这个工具检查路径&通过将目标文件夹测试写入磁盘来确保文件有效性。到目前为止,该工具可以检查多达9个本地或网络文件路径&名称参数。 "仅限本地驱动器"限制被解除了。
我问,如何a)检查超过9个参数,b)提升代码注释中陈述的用户输入要求。在这个主题的几个SO线程中没有解决这些问题。该工具纯粹基于批处理,而其他主题则讨论C ++正则表达式或PowerShell相关技术,或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
以下是一个示例用例:
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>