This question already has answers here :
Batch File : Read File Names from a Directory and Store in Array , I'm creating a batch file in which I need to list all the text file name of the specified folder then store and retrieve the same from an array. batch-file documentation: Using a Variable as an Array. Example. It is possible to create a set of variables that can act similar to an array (although they are not an actual array object) by using spaces in the SET statement:
Something like that should work :
@echo off
set paths[0]="C:\p\test1"
set paths[1]="C:\p\test2"
set paths[2]="C:\p\test3"
SetLocal EnableDelayedExpansion
for /L %%i in (0,1,2) do (
for %%j in (!paths[%%i]!) do echo Processing %%~nxj
)
pause
Return 'dir' command results into array/list, Populating Array in DOS Batch Script (12 answers) I'm using the command like this: dir /s /b *.txt how would I put each result (file name) into an array/list. DOS: Get the directory name from a file path If you need to write a DOS batch file that takes a file path as an input parameter and needs to do something to other files in the same directory as the input file then you’ll find there is no easy way to get the file’s directory name into a variable.
Define the Array within the function.
This approach can be used to define multiplay Arrays.
@ECHO OFF
Setlocal EnableDelayedExpansion
:: REM P_C is used to define the range of the Array. The -1 operations on P_C is to shift the paths parameter out of the Arrays working Index.
::REM the first parameter passed is used as the Arrays Name. all other parameters are assigned to index values 0 +
Call :process paths "C:\p\test1" "C:\p\test2" "C:\p\test3"
pause
:process
Set P_C=0
Set /a P_C-=1
For %%a in (%*) DO (
CALL :populate %1 "%%~a"
)
Set /a P_C-=1
For /L %%b in (0,1,!P_C!) DO (
ECHO Processing %1[%%b] = "!%1[%%b]!"
)
GOTO :EOF
:populate
Set "%1[!P_C!]=%~2"
Set /a P_C+=1
GOTO :EOF
Populating Array in DOS Batch Script, A batch file named array.bat is included in the article, that contains a library of on: SET MENUITEM=!MENUITEM%CHOICE%! << Get the stored filename from� The listing of folder contents can be done with the dir command. This command allows you to see the available files and directories in the current directory. The dir command also shows the last modification date and time, as well as the file size.
how i can get folder and file name in windows and store it in variable , FOR %%f in (folder1\*) DO @echo %%f. in a batch file will echo the filename of each file in the folder. To do the same thing at the command line, use only one� ' For Directory.GetFiles and Directory.GetDirectories ' For File.Exists, Directory.Exists Imports System.IO Imports System.Collections Public Class RecursiveFileProcessor Public Overloads Shared Sub Main(ByVal args() As String) Dim path As String For Each path In args If File.Exists(path) Then ' This path is a file.
Batch Script - Arrays, Batch Script - Arrays - Arrays are not specifically defined as a type in Batch Script but to retrieve within square brackets immediately after the name of the array. How to Use Array in Windows Batch Programming?We have to define variables using set and the length has to defined first. The above script will print A, B, C in three lines and we treat obj[0], obj[1] and obj[2] as three individual variables (their memory address locations are not continuous).
Windows Environment Variables - Windows CMD, Array variables. Unlike PowerShell, which fully supports arrays, there is no built in support for array variables within the CD, Y, The current directory (string). An interesting difference that Batch arrays have versus other programming languages is that the indexes are not restricted to be numerical values, so they may be any string. The Batch file below is an example of a two-dimensional array management program that uses no-numerical indexes; it is a very basic multi-language translation program that
Comments You may also want to look at this answer. I think you need to enable delayed expansion. Nice, post it as answer and I'll accept it ! It works I have upvoted this answer as it is exactly the same methodology I would have used. I would however have added something additional, (to replace the empty line 2) , For /F "Delims==" %%I In ('Set paths[ 2^>NUL')Do Set "%%I="
. This ensures that there are definitely no existing defined variable names beginning with paths[
so that only those defined within the script are returned by the for-loop on line 7. For more information on Arrays: stackoverflow.com/questions/10166386/…