-1

There's random amount of files in the directory with same extension.

E.g.

file1.txt
file2.txt
file3.txt

Need to scan the directory and set each filename to a new variable. We can do something like this:

        for %%A in ("*.txt") do (
            set var=%%~nA
        )

But it sets the filenames to that same one variable and at the end only last variable is active, so in this example it will always be just file3.

Need to set each filename to new variable so those variables can be used later.

var1=file1
var2=file2
var3=file3

Also if there's X amount of .txt files that means X amount of variables has to be created. So in this example if there's only 3 files, 3 variables are created.

user3108268
  • 1,043
  • 3
  • 18
  • 37
  • 1
    `set /a counter+=1` and `set var!counter!=%%~nA`. You will need to enable delayed expansion as well. – Squashman Jul 01 '20 at 21:33
  • 2
    See [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/a/10167990/778560). I suggest you to use the standard _array notation_ and enclose the subscript between square braquets this way: `var[1]=file1` – Aacini Jul 01 '20 at 21:47
  • The `FOR` command will iterate the files in the directory in alphabetical order and will assign it to each array variable. – Squashman Jul 01 '20 at 23:42
  • 2
    To create the array: `setlocal EnableDelayedExpansion` and `set n=0` and `for %%A in ("*.txt") do (` and `set /A n+=1` and `set var[!n!]=%%~nA` and `)`. To process the array: `for /L %%i in (1,1,%n%) do echo !var[%%i]!` – Aacini Jul 02 '20 at 01:36
  • @Squashman, `for` doesn't sort anything, it returns the items in the order the file system does; e. g., NTFS returns the items sorted, but FAT doesn't/didn't… – aschipfl Jul 03 '20 at 12:48

1 Answers1

3

Here's one method, complete with Remarks:

@Echo Off
Rem Localize variables, and enable extensions.
Rem  (needed for the Set, SetLocal, EndLocal, If, For, and GoTo commands).
SetLocal EnableExtensions
Rem Undefine any existing variables with names beginning with the $ character.
For /F "Delims==" %%G In ('Set $ 2^>NUL') Do Set "%%G="
Rem Define and count individual incrementing variables for each matching basename.
For /F "Tokens=1,* Delims=:" %%G In ('%__AppDir__%where.exe .:*.txt 2^>NUL^
 ^|%__AppDir__%findstr.exe /N "^"') Do Set "$%%G=%%~nH" & Set "#=%%G"
Rem Provide some feedback if no matches, and end the script after 3 seconds.
If Not Defined # (Echo There were no matching files
    %__AppDir__%timeout.exe /T 3 /NoBreak >NUL
    GoTo :EOF)
Rem Provide feedback on the number of matches.
Echo There were %#% matching files defined as variables.
Rem Ask end user if they would like to view the variables with their values.
Choice /M "Would you like to see them"
Rem If Yes, show them.
If %ErrorLevel% Equ 1 (SetLocal EnableDelayedExpansion
    For /L %%G In (1,1,%#%) Do Echo %%$%%G%% = !$%%G!
    EndLocal & Pause)
Rem Rest of your code goes below here.
Compo
  • 36,585
  • 5
  • 27
  • 39