知道如何循环遍历数组,在批处理文件中,根据每个值获取信息?

时间:2022-03-09 08:54:24

I am creating a batch file that will gather information (size, number of files and number of folders) based on a pathway in an array from an external text file. At the minute, I am able to get these details but I am only getting the information for the last pathway listed in the text file.

我正在创建一个批处理文件,它将根据外部文本文件中的数组路径收集信息(大小,文件数和文件夹数)。在那一刻,我能够获得这些细节,但我只获取文本文件中列出的最后一条路径的信息。

Here's what I've go so far:

这是我到目前为止所做的事情:

::Title for the cmd window
TITLE Getting Repository information
ECHO OFF
::Setting parameters
setlocal ENABLEDELAYEDEXPANSION
set today=!date:/=-!
::Setting the location of the output file
set output=!C:\Users\%username%\Desktop\output_!today!.txt!
set pathway=!C:\Users\%username%\Desktop\pathways.txt!
ECHO ------------------------------------------------------------>> !output!
ECHO ----------Time to get some repository information:---------->> !output!
ECHO ------------------------------------------------------------>> !output!
ECHO.>> !output!
ECHO %date% %time%>> !output!
ECHO.>> !output!
ECHO.>> !output!
::Creating an array
setlocal EnableDelayedExpansion
set i=0
for /F %%a in (C:\Users\%username%\Desktop\pathways.txt) do (
  set /A i+=1
  set array[!i!]=%%a
)
set n=%i%
for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)
::Getting the repository size (only displays the size info for the location of this file)
ECHO OFF
Set _size=0
For /F "tokens=3 delims= " %%I In ('Dir %folder% /a-d /S ^|Find /I "file(s)"') Do Set _size=%%I
ECHO %folder% %_size% Bytes>> !output!
::Getting the number of folders
for /f %%a in ('dir /b /s /ad %folder%^|find /c /v "" ') do set count=%%a
ECHO %folder% %count% folder(s^)>> !output!
::Getting the number of files
set /a count=0 
for /f "tokens=* delims= " %%a in ('dir/s/b/a-d %folder%') do ( 
set /a count+=1 
) 
ECHO %folder% %count% file(s)>> !output!
ECHO ============================================================>> !output!
ECHO ============================================================>> !output!
ECHO.>> !output!

Sorry for it being messy. I can't indent it nicely. I am probably overlooking something. Any help is appreciated.

对不起因为它很乱。我不能很好地缩进它。我可能忽视了一些事情。任何帮助表示赞赏。

Thanks!

2 个解决方案

#1


1  

::Title for the cmd window
TITLE Getting Repository information
ECHO OFF

::Setting parameters
setlocal ENABLEDELAYEDEXPANSION
set today=!date:/=-!

::Setting the location of the output file
set output=!C:\Users\%username%\Desktop\output_!today!.txt!
set pathway=!C:\Users\%username%\Desktop\pathways.txt!
ECHO ------------------------------------------------------------>> !output!
ECHO ----------Time to get some repository information:---------->> !output!
ECHO ------------------------------------------------------------>> !output!
ECHO.>> !output!
ECHO %date% %time%>> !output!
ECHO.>> !output!
ECHO.>> !output!

::Creating an array
setlocal EnableDelayedExpansion
set i=0
for /F %%a in (C:\Users\%username%\Desktop\pathways.txt) do (
  set /A i+=1
  set array[!i!]=%%a
)
set n=%i%
REM APA - Next line get LAST array element only and process it:
REM for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)
REM To process all folders, nest the following process into this FOR loop
REM and change all %var% by !var! for vars assigned into the loop:

for /L %%i in (1,1,%n%) do (
   set folder=!array[%%i]!

   ::Getting the repository size (only displays the size info for the location of this file^)
   ECHO OFF
   Set _size=0
   For /F "tokens=3 delims= " %%I In ('Dir !folder! /a-d /S ^|Find /I "file(s^)"') Do Set _size=%%I
   ECHO !folder! !_size! Bytes>> !output!

   ::Getting the number of folders
   for /f %%a in ('dir /b /s /ad !folder!^|find /c /v "" ') do set count=%%a
   ECHO !folder! !count! folder(s^)>> !output!

   ::Getting the number of files
   set /a count=0 
   for /f "tokens=* delims= " %%a in ('dir/s/b/a-d !folder!') do ( 
      set /a count+=1 
   )

   ECHO !folder! !count! file(s^)>> !output!
   ECHO ============================================================>> !output!
   ECHO ============================================================>> !output!
   ECHO.>> !output!

rem The FOR loop ends here:
)

#2


1  

This line will set folder to the last item in the array, only.

此行仅将文件夹设置为数组中的最后一项。

Move the closing parenthesis to the end of your next code section.

将右括号移动到下一个代码部分的末尾。

for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)

#1


1  

::Title for the cmd window
TITLE Getting Repository information
ECHO OFF

::Setting parameters
setlocal ENABLEDELAYEDEXPANSION
set today=!date:/=-!

::Setting the location of the output file
set output=!C:\Users\%username%\Desktop\output_!today!.txt!
set pathway=!C:\Users\%username%\Desktop\pathways.txt!
ECHO ------------------------------------------------------------>> !output!
ECHO ----------Time to get some repository information:---------->> !output!
ECHO ------------------------------------------------------------>> !output!
ECHO.>> !output!
ECHO %date% %time%>> !output!
ECHO.>> !output!
ECHO.>> !output!

::Creating an array
setlocal EnableDelayedExpansion
set i=0
for /F %%a in (C:\Users\%username%\Desktop\pathways.txt) do (
  set /A i+=1
  set array[!i!]=%%a
)
set n=%i%
REM APA - Next line get LAST array element only and process it:
REM for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)
REM To process all folders, nest the following process into this FOR loop
REM and change all %var% by !var! for vars assigned into the loop:

for /L %%i in (1,1,%n%) do (
   set folder=!array[%%i]!

   ::Getting the repository size (only displays the size info for the location of this file^)
   ECHO OFF
   Set _size=0
   For /F "tokens=3 delims= " %%I In ('Dir !folder! /a-d /S ^|Find /I "file(s^)"') Do Set _size=%%I
   ECHO !folder! !_size! Bytes>> !output!

   ::Getting the number of folders
   for /f %%a in ('dir /b /s /ad !folder!^|find /c /v "" ') do set count=%%a
   ECHO !folder! !count! folder(s^)>> !output!

   ::Getting the number of files
   set /a count=0 
   for /f "tokens=* delims= " %%a in ('dir/s/b/a-d !folder!') do ( 
      set /a count+=1 
   )

   ECHO !folder! !count! file(s^)>> !output!
   ECHO ============================================================>> !output!
   ECHO ============================================================>> !output!
   ECHO.>> !output!

rem The FOR loop ends here:
)

#2


1  

This line will set folder to the last item in the array, only.

此行仅将文件夹设置为数组中的最后一项。

Move the closing parenthesis to the end of your next code section.

将右括号移动到下一个代码部分的末尾。

for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)