Best way to setup a "Dictionary" similar to VBA?
Moderator: DosItHelp
Re: Best way to setup a "Dictionary" similar to VBA?
Except that with real files, it's much slower, and very fragile: If other files have one of these extensions, the code will break.
Re: Best way to setup a "Dictionary" similar to VBA?
Interesting Idea. I Doubt the below is any more effecient than just using variables, however it is a somewhat different way of getting values from an array in batchAacini wrote: ↑21 Feb 2021 23:11This method is interesting and may be extended to use real disk files, like this:CJM wrote: ↑21 Feb 2021 19:12For a quick lookup list, I use the name.ext variable parsers %~n and %~x, adding a . before the lookup value like this example which expands the 3-letter abbreviation for day-of-week to the full word:
It has a few obvious character limitations such as:Code: Select all
@ FOR %%T in ( %DATE% )do @ FOR %%D in ( Sunday.Sun Monday.Mon Tuesday.Tue Wednesday.Wed Thursday.Thu Friday.Fri Saturday.Sat )do @ IF .%%T==%%~xD ECHO/%%~nD
: \ ? *
but for simple lookups, it's quick and easy.
AntonioCode: Select all
@echo off setlocal rem Create the array-like table as empty files on disk for %%a in ( Sunday.Sun Monday.Mon Tuesday.Tue Wednesday.Wed Thursday.Thu Friday.Fri Saturday.Sat ) do rem/> %%a :next echo/ set /P "ext=Enter 3-letters day: " if errorlevel 1 pause & goto :EOF rem Do the mapping for %%a in (*.%ext%) do set "name=%%~Na" echo Full day: %name% goto next
Code: Select all
@Echo off & CD /D "%~dp0"
Setlocal EnableDelayedExpansion
break>"Dictionary.txt"
Set "Map=Monday.[1] Tuesday.[2] Wednesday.[3] Thursday.[4] Friday.[5] Saturday.[6] Sunday.[7]"
For %%G in (%Map%)Do >>"Dictionary.txt" Echo(%%G
(Set \n=^^^
%= \n macro newline var. Do Not Modify =%)
%= Define GetIndexMacro; Intiate Getargs =% Set GetIndex=For %%n in (1 2)Do if %%n==2 (%\n%
%= Iterate over each pending assignment =% For %%G in (^^!Args^^!) Do (%\n%
%= Split current returnvar and Index Target =% For /F "Tokens=1,2 Delims==" %%i in ("%%~G")Do (%\n%
%= Capture Indexed String for assignment =% For /F "Delims=" %%V in ('%__APPDIR__%findstr.exe /LIC:".[%%j]" "Dictionary.txt"')Do (%\n%
%= Assign to returnvariable =% set "%%i=%%~nV"%\n%
%= Close Command Loops =% )))%\n%
%= Catch Args for returnvarEQUindex =% )Else Set Args=
%GetIndex% "Return1=1" "Return2=3"
Set Return