You are not logged in.
Presented FileNameValidator R0.9 batch allows unlimited number of local or network file paths and/or names entered as batch arguments or manually, and validated on the fly. It was developed to address obvious need for a pure batch solution for this task, currently missing. In the :core section a user can add own code that uses entered file paths & names to run user defined tasks. The user must add to the :default section all path & file variable names pat# used in their :core section code, and if desired can add some or all default values of file names & paths dir# to be shown as a choice option during interactive input.
At present, as per my limited testing, the code works pretty well "as is" with local and LAN drives, for which the user has write access. Any code samples aiming to lift conditions stated in the code comments below, or enrich the tool functionality are appreciated. As well, code samples are invited making this code significantly simpler or shorter, while preserving its interactive, safety, and validation features. True bug reports are welcome.
My comments in this thread will be restricted to the above scope. Minor code variations not affecting its functionality are always possible, and users are encouraged to implement such at their discretion to keep this thread on target. Pls consult this site's Reference section with command usage related questions. Otherwise, enjoy this free tool.
@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 write accessible
:: If missing in batch args or invalid, user can type new or add to ":default" section & choose default file names or paths
:: Use matching 3-letter ID for pat# "SaveTo" directory and file name 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= Directory 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= Default value not found" & set "mes7=is empty or invalid"
set "mes8=All arguments entered" & set "mes9=C(o)ntinue, (r)e-enter arguments, or e(x)it?"
set "mes10= Extension doesn't match" & set "mes11= SaveTo directory not defined in code"
set "pat1=winsource_dir" & set "pat2=iso_dir" & set "pat3=esd_dir" & set "pat4=esd_file.iso"
set "pat5=psk_dir" & set "pat6=pst_file.txt"
set "dir1=K:\Other\Tests\Windows_files" & set "dir2=K:\Other\Tests\Modified_iso"
set "dir3=K:\Other\Tests\Esd_files" & set "dir4=Final.iso" & set "i=1" & echo/
:input
if not defined patc (set "patn=;0;1;2;3;4;5;6;7;8;9;" & for /f %%l in ('set pat') do (
set "patl=%%l" & set "patl=!patl:~3,1!" & call set "patm=%%patn:;!patl!;=%%"
if not "!patm!"=="!patn!" set /a "patc+=1"))
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%=%%~1" & shift & if not defined path%i% (
echo Batch arg%i% for "!pat%i%!" not found & set "ver=1") else (echo Read arg%i%: !pat%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 %mes10% & set "cho=1"
) else ( set "break=" & for /l %%j in (1,1,%patc%) do (
if not defined break (if "!pat%%j:~0,3!"=="!pat%i%:~0,3!" if "!pat%%j:~4,7!"=="dir" (
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!")))
if not defined pathf%i% echo %mes11%)
) 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>FileNameValidator.bat "K:\Other\Tests" "K:\Other\Tests<" "" "test.txt"
Read arg1: winsource_dir = K:\Other\Tests
Directory or file already exists
Use (d)efault, e(x)isting, or (r)e-enter value? X
Existing value used: K:\Other\Tests
Read arg2: iso_dir = K:\Other\Tests<
Invalid dir path or file name
Use (d)efault, e(x)isting, or (r)e-enter value? D
Default value used: K:\Other\Tests\Modified_iso
Batch arg3 for "esd_dir" not found
Enter esd_dir > K:\Other\Tests
Directory or file already exists
Use (d)efault, e(x)isting, or (r)e-enter value? R
Enter esd_dir > K:\Other\Tests1
Entered value used: K:\Other\Tests1
Read arg4: esd_file.iso = test.txt
Extension doesn't match
Use (d)efault, e(x)isting, or (r)e-enter value? D
Default value used: Final.iso
Batch arg5 for "psk_dir" not found
Enter psk_dir > K:\Other\Tests2
Entered value used: K:\Other\Tests2
Batch arg6 for "pst_file.txt" not found
Enter pst_file.txt > test.txt
SaveTo directory not defined in code
Entered value used:
All arguments entered
winsource_dir = K:\Other\Tests
iso_dir = K:\Other\Tests\Modified_iso
esd_dir = K:\Other\Tests1
esd_file.iso = Final.iso
psk_dir = K:\Other\Tests2
pst_file.txt =
C(o)ntinue, (r)e-enter arguments, or e(x)it? X
Exiting program...
K:\Other\Tests>
EDIT: updated to R0.9 version 7/7/16
Last edited by sambul35 (09 Jul 2016 01:23)
Offline
Updated the tool to R0.9 to support:
a) unlimited number of batch arguments
b) mismatch errors handling between directory and file variable IDs added by user in :default section
Last edited by sambul35 (09 Jul 2016 00:15)
Offline
Released FileNameValidator R1.0 COPYRIGHT©
@echo off
setlocal EnableExtensions EnableDelayedExpansion
:: Accepts variables from args_file named in batch arguments, supplied as batch arguments, from :default section, and typed.
:: Checks if filenames & path are absolute & valid in installed Windows version by trying to create a new folder with a given
:: path\name, if not exists and target disk is write accessible.
:: If missing in args_file or batch args or invalid, user can type new value, or add default values to :default section or the
:: file and then choose default names & paths when the tool runs.
:: Use matching 4-letter IDs for "SaveTo" dir and file name vars (i.e. esd_dir & esd_file). Variables without IDs are processed
:: without validation. Same name vars values in "args_file" override those in :default section.
:: Default values, path & name variables, and arguments numbering must match ([%1 & dir[1] & pat[1]],[var.iso & file.iso])
:: If "user_bat" exists in the same dir, the tool can save processed vars to a new "vargs_file" and call the user_bat with
:: vargs_file name as argument, or run own code in :core section.
:default
set "mes1= Invalid dir path or file name" & set "mes2= Directory 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= Default value not defined" & set "mes7=is empty or invalid"
set "mes8=All arguments entered" & set "mes9=Con(T)inue, (r)e-enter arguments, or e(x)it?"
set "mes10= Extension doesn't match" & set "mes11= SaveTo directory not defined"
set "mes12= Not a path or filename" & set "mes13=Manually enter all arguments..."
set "mes14=Save args to file & start user batch?"
set "pat[1]=win_source_dir" & set "pat[2]=iso_dir" & set "pat[3]=esd_dir" & set "pat[4]=esd_file.iso"
set "pat[6]=psk_dir" & set "pat[7]=pst_file.txt" & set "pat[8]=psl_dir" & set "pat[9]=psl_file.txt"
set "dir[1]=K:\Other\Tests\Windows_files" & set "dir[2]=K:\Other\Tests\Modified_iso"
set "dir[3]=K:\Other\Tests\Esd_files" & set "dir[4]=Final.iso"
set "args_file=args.txt" & set "vargs_file=checked_args.txt" & set "user_bat=user.bat" & set "patn="
:input
rem Read vars & arg names & values from file. Count pat# variables entered from file or :default section
cls & if not defined patn (set "patf=1" & set "patd=1" & set "argf=" & set "args=" & set "patcn="
for /f "delims==" %%p in ('set pat[') do (set "pac=%%p" & set "pan=!pac:~4,-1!"
set "patcn=!patcn! !pan!" & set "patn=!patcn!")
if exist %args_file% (for %%o in (%*) do (if "%%~o"=="%args_file%" (set "argf=1" & set "patn=" & set "patfn="
for /F "tokens=1,2,3 delims==" %%p in (%args_file%) do (set "pac=%%p" & set "pan=!pac:~4,-1!"
if not "!pac!"=="" if not "!pac:~0,3!"=="dir" (
set "pat[!pan!]=%%q" & set "path[!pan!]=%%r" & set "patfn=!patfn! !pan!"
) else if not "!pac!"=="" (set "dir[!pan!]=%%q"))
for %%i in (!patfn!) do (for %%j in (!patcn!) do (if %%j==%%i set "patcb=!patcb! %%j"))
call set "patcn=%%patcn:!patcb!=%%" & set "patc=!patfn!!patcn!"
for %%k in (!patc!) do (set /a "patl=1000+%%k" & set "patl=!patl:~-3!" & set "pac.!patl!=1")
for /f "tokens=2 delims=.=" %%l in ('set pac.') do (
set "patm=1%%l" & set /a "patm=!patm!-1000" & set "patn=!patn! !patm!"))))
if not defined argf (for %%o in (%*) do if not "%%o"=="" set "args=!args! %%o"))
rem Process file or batch arguments & typed variables
echo/ & if not defined args echo %mes13% & echo/
for %%i in (%patn%) do (set "i=%%i" & set "def=" & set "ver="
if defined args (call set "path[%%i]=%%~1" & shift & set "source=Batch" & if not defined path[%%i] set "def=1"
) else if defined argf ( set "source=File" & if not defined path[%%i] set "def=1") else (set "ver=1")
if not defined ver (if defined def (echo !source! arg%%i for "!pat[%%i]!" is empty & set "ver=1"
) else (echo !source! arg%%i: !pat[%%i]! = !path[%%i]!))
call :validate !i!)
:output
echo/ & echo %mes8% & echo/ & for %%i in (%patn%) do (echo !pat[%%i]! = !path[%%i]!)
echo/ & choice /c trx /n /m "%mes9%" /t 20 /d t & echo/
if errorlevel 3 (goto :end) else if errorlevel 2 (for %%i in (%patn%) do set "path[%%i]="
set "ver=1" & set "args=" & set "argf=" & goto :input)
:core
rem Save args to file, than call user batch with that filename as argument
if exist !user_bat! (choice /c ny /m "%mes14%" /t 10 /d n & echo/
if errorlevel 2 (if exist !vargs_file! del /q !vargs_file!
for /l %%i in (%patn%) do (echo !pat[%%i]!=!path[%%i]! >> !vargs_file!)
call !user_bat! !vargs_file! & del /q !vargs_file!))
:: add your code here that uses pat# variable values as path\file variable names, if user_bat batch isn't used
:end
echo/ & echo Exiting program...
timeout /t 1 /nobreak >nul
exit /b
: validate
rem Validate file & folder names and absolute paths
if defined ver (set /p "path[%1]=Enter !pat[%1]! > " 2>nul)
set "!pat[%1]!=!path[%1]!" & set "cho=" & set "val=" & set "ver="
if "!pat[%1]:~-4,1!"=="." (
if not "!path[%1]:~-4!"=="!pat[%1]:~-4!" (echo %mes10% & set "cho=1"
) else ( set "break=" & for %%j in (%patn%) do (
if not defined break (if "!pat[%%j]:~0,3!"=="!pat[%1]:~0,3!" if "!pat[%%j]:~4,7!"=="dir" (
set "break=TRUE" & set "pathf[%1]=!path[%1]!" & set "path[%1]=!path[%%j]!\!path[%1]!"
if not exist !path[%%j]! set "pathd[%1]=!path[%%j]!")))
if not defined pathf[%1] echo %mes11% & set "val=1" & set "path[%1]=" )
) else if not "!pat[%1]:~3,1!"=="_" (echo %mes12% & set "val=1"
) else if not exist "!path[%1]:~0,3!" if not "!path[%1]:~0,2!"=="\\" echo %mes1% & set "cho=1")
if exist "!path[%1]!" if not defined cho (
if exist "!path[%1]!\*" if "!pat[%1]:~-4,1!"=="." (echo %mes4% & set "cho=1"
) else (echo %mes2% & set "cho=2"))
if not defined cho if not defined val ( md "!path[%1]!" 2>nul
if errorlevel 1 (echo %mes1% & set "cho=1") else ( rmdir /s /q "!path[%1]!" & set "cho="
(if "!pat[%1]:~-4,1!"=="." set "path[%1]=!pathf[%1]!") & echo Entered %mes5% !path[%1]!)
if defined pathd[%1] rmdir /s /q "!pathd[%1]!" 2>nul)
rem Offer user corrections
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[%1]!) else (set "ver=1" & echo %mes1%)
) else (if not "!dir[%1]!"=="" (set "path[%1]=!dir[%1]!" & set "ver=" & echo Default %mes5% !dir[%1]!
) else (set "ver=1" & echo %mes6%)))
if defined ver ((goto) 2>nul & call :validate !i!)
exit /b
Major changes in this release:
a) batch arguments and default values can now be entered from args.txt file supplied by user and in :default section, or args entered in-line when launching this batch
b) added support for non-path or filename arguments that are not validated
c) tool structure redesigned to support and sort random order of batch args entered from a file and/or :default section
d) added filtering duplicate arguments supplied via file and :default section. Values in file override those in :default section
e) support combined processing of unique arguments from file and :default section
f) in-line supplied batch arguments are numbered & processed in sequence defined by pat[#] variables in :default section
g) all arguments can be re-entered manually once automatic all args processing is completed
h) user can save processed args to checked_args.txt, and then call own user.bat file to perform desired tasks with checked_args.txt filename as an argument, or run own tasks in the tool's :core section
i) enriched user messaging system
Usage examples are posted below. Will review true bug reports and notable improvement code samples.
EDIT: Its nice to notice the tool is getting popular on other forums. Development of alternative tools is always welcome for coding expertise sharing and user choice, but this task may be hard to crack.
Last edited by sambul35 (14 Jul 2016 02:02)
Offline
Sample use case: arguments file and :default section values used as File args
args.txt content:
pat[1]=win_source_dir=K:\Other\Tests1
pat[2]=iso_dir=K:\Other\Tests2>
pat[3]=esd_dir=K:\Other\Tests3
pat[4]=esd_file.iso=test.txt
pat[6]=psk_dir=K:\Other\Tests4
pat[7]=pst_file.txt=test.txt
pat[11]=var1=opus11
pat[12]=var2=opus12
dir[1]=K:\Other\Tests\Windows files
dir[2]=K:\Other\Tests\Modified_iso
dir[3]=K:\Other\Tests\Esd_files
dir[4]=Esd.iso
dir[7]=pst.txt
Batch output printout:
K:\Other\Tests>FileNameValidator.bat "args.txt"
File arg1: win_source_dir = K:\Other\Tests1
Entered value used: K:\Other\Tests1
File arg2: iso_dir = K:\Other\Tests2>
Invalid dir path or file name
Use (D)efault, e(x)isting, or (r)e-enter value? D
Default value used: K:\Other\Tests\Modified_iso
File arg3: esd_dir = K:\Other\Tests3
Entered value used: K:\Other\Tests3
File arg4: esd_file.iso = test.txt
Extension doesn't match
Use (D)efault, e(x)isting, or (r)e-enter value? D
Default value used: Esd.iso
File arg6: psk_dir = K:\Other\Tests4
Entered value used: K:\Other\Tests4
File arg7: pst_file.txt = test.txt
SaveTo directory not defined
File arg8 for "psl_dir" is empty
Enter psl_dir > K:\Other\Tests2
Entered value used: K:\Other\Tests2
File arg9 for "psl_file.txt" is empty
Enter psl_file.txt > psl.txt
Entered value used: psl.txt
File arg11: var1 = opus11
Not a path or filename
File arg12: var2 = opus12
Not a path or filename
All arguments entered
win_source_dir = K:\Other\Tests1
iso_dir = K:\Other\Tests\Modified_iso
esd_dir = K:\Other\Tests3
esd_file.iso = Esd.iso
psk_dir = K:\Other\Tests4
pst_file.txt =
psl_dir = K:\Other\Tests2
psl_file.txt = psl.txt
var1 = opus11
var2 = opus12
Con(T)inue, (r)e-enter arguments, or e(x)it? T
Save args to file & start user batch? [N,Y]?Y
User batch started with validated arguments...
Hello world
C(o)ntinue or e(x)it? X
Option: 2
User batch completed
Exiting program...
K:\Other\Tests>
Last edited by sambul35 (11 Jul 2016 12:12)
Offline