[英]Batch - Store output of dir command into variable - directory listing
我需要將整個目錄列表存儲到一個變量中,然后將所述變量作為參數傳遞給另一個腳本。 直接或首先將 dir 的輸出存儲到文本文件,然后執行以下操作:
dir \path\todir > temp.txt
set /p VAR=<temp.txt
但上面只讀了一行。 有人可以幫忙嗎?
由於您沒有向我們提供有關您的最終目標的任何其他信息;
我發布了一些內容,也許會給您任何想法,以便使用如下代碼的數組輕松存儲變量:
@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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.