批处理-将dir命令的输出存储到变量中-目录列表 [英] Batch - Store output of dir command into variable - directory listing

查看:336
本文介绍了批处理-将dir命令的输出存储到变量中-目录列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to store an entire directory listing into a variable, then pass said variable as a parameter to another script. Either directly or by first storing the output of dir to a text file, then doing something like this :

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

但是上面只读一行.有人可以帮忙吗?

But the above only reads one line. Can someone help?

推荐答案

由于您没有向我们提供有关最终目标的其他信息;

Since you didn't provide us any other information about your end goal ;

我发布了一些可能会给您一些想法的东西,这些东西可以使用像这样的代码的数组轻松地存储变量:

I post something that perhapes give you any ideas to store your variables easily with an array like this code :

@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

这篇关于批处理-将dir命令的输出存储到变量中-目录列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆