0

我正在创建一个批处理文件,它将根据来自外部文本文件的数组中的路径收集信息(大小、文件数和文件夹数)。目前,我可以获得这些详细信息,但我只获得文本文件中列出的最后一条路径的信息。

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

::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!

很抱歉弄得乱七八糟。我不能很好地缩进它。我可能忽略了一些东西。任何帮助表示赞赏。

谢谢!

4

2 回答 2

1

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

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

for /L %%i in (1,1,%n%) do (set folder=!array[%%i]!)
于 2013-11-07T11:37:43.693 回答
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:
)
于 2013-11-07T15:13:19.693 回答