list folders in cmd code example

Example 1: cmd files in directory

Enter “dir” (without quotes) to list the files and folders contained in the folder.

Example 2: dir files windows

# For finding file use dir followed by string matching file name or regex
# pattern and add "/s" for searching recursively in subfolders:
dir /s "regex_or_file_name"
# For finding file contained in a searched directory use "/b":
dir /b "directory_name"
# Use respectively '/a-d' or '/ad' for finding only files or only folders.

Example 3: how to get list folder in cmd to value

for /f "eol=: delims=" %%F in ('dir /b /o:n') do set /p =""%%F","

Example 4: how to get list folder in cmd to value

@echo off
setlocal disableDelayedExpansion
:: Load the file path "array"
for /f "tokens=1* delims=:" %%A in ('dir /s /b^|findstr /n "^"') do (
  set "file.%%A=%%B"
  set "file.count=%%A"
)

:: Access the values
setlocal enableDelayedExpansion
for /l %%N in (1 1 %file.count%) do echo !file.%%N!

Tags: