批处理 - 将dir命令的输出存储到变量 - 目录列表中

时间:2016-10-10 11:33:23

标签: batch-file variables directory

我需要将整个目录列表存储到变量中,然后将所述变量作为参数传递给另一个脚本。直接或首先将dir的输出存储到文本文件中,然后执行以下操作:

dir \path\todir > temp.txt
set /p VAR=<temp.txt

但上面只读了一行。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

由于您未向我们提供有关您最终目标的任何其他信息,

我发布了一些内容,perhapes可以让你轻松地使用类似代码的数组来存储你的变量:

@echo off
set "folder=%userprofile%\Desktop"
setLocal EnableDelayedExpansion
REM Populate the array with existent sub-folders in this folder
for /f "tokens=* delims= " %%a in ('Dir /b /a:d "%folder%"') do (
    set /a N+=1
    set "Folder[!N!]=%%a"
)
Rem Populate the array with existent files in this folder
for /f "tokens=* delims= " %%a in ('Dir /b /a:-d "%folder%"') do (
    set /a M+=1
    set "File[!M!]=%%a"
)
::*****************************************************************
:Display_Folders
cls & color 0E
echo Just showing only Folders :
echo(
For /L %%i in (1,1,%N%) do (
    echo Folder[%%i] = !Folder[%%i]!
)
echo(
echo Hit any key to show only files :
pause>nul
::*****************************************************************
:Display_Files
cls & color 0B
echo Just showing only Files :
echo(
For /L %%j in (1,1,%M%) do (
    echo File[%%j] = !File[%%j]!
)
echo(
echo Hit any key to exit :
Pause>nul & exit