1

I want to make cmd type all files in its own variable. example:

C:\>dir/b
systemlogs.txt
projects.bat

C:\>echo %file1%
systemlogs.txt

C:\>echo %file2%
projects.bat

Any tips? Any code i can use?

Endoro
  • 37,015
  • 8
  • 50
  • 63
  • The concept behind "each file in its own variable" is called _array_. See this post: http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Jun 03 '13 at 22:33

2 Answers2

1

try this:

@echo off &setlocal enabledelayedexpansion
set /a counter=0
for %%i in (systemlogs.txt projects.bat) do (
    set /a counter+=1
    set "file!counter!=%%i"
)
set "file"

..output is:

file1=systemlogs.txt
file2=projects.bat
Endoro
  • 37,015
  • 8
  • 50
  • 63
0

Something like this should work:

@echo off

setlocal EnableDelayedExpansion

cd /d "C:\some\folder"

set a=0
for %%f in (*) do (
  set /a a+=1
  set "file!a!=%%~ff"
)

set b=0
for /d %%d in (*) do (
  set /a b+=1
  set "folder!b!=%%~fd"
)

for /l %%i in (1,1,%a%) do echo %%i: !file%%i!
for /l %%i in (1,1,%b%) do echo %%i: !folder%%i!

endlocal
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328