我是 dos 批处理脚本的新手。我拼凑了几个代码片段,并进行了一些修改,让我的脚本适用于一个小目录。它通过目录树递归,计算作为参数传入的日期范围内的文件数,然后写入输出文件报告。
它适用于小型目录结构,但如果它必须递归遍历数百个文件夹,则它会因“批量递归超出堆栈限制”错误而中止。
我从这个站点了解到递归循环不是很有效,一旦我在堆栈上放置了一定数量的数据,我就会干杯。我已经在这个网站和其他地方寻求帮助。大多数建议是编写一个更高效的程序,但我不确定如何做到这一点。任何帮助,将不胜感激。我需要将效率提高一个数量级,因为我需要它的目录结构将有数千个文件夹。这是我的代码:
@echo off
setlocal enableDelayedExpansion
pushd %1
REM This program takes three parameters <starting directory> <startdate> <enddate>
REM The startdate and endate should be in format: mm/dd/yyyy
REM The program will recursively look through all directories and sub-directories
REM from the given <starting directory> and count up the number of files written
REM within the date range from <startdate> until <endate>
REM It will then write out a RetentionReport_<date>_<time>.txt file that lists
REM one line showing the <startdate> <enddate> and the # of files found.
REM If you don't pass in all three arguments it will let you know and then exit.
REM You need to set your TESTDIR below to a hardpath location for the writing
REM of temporary files and writing of the Reports
REM There is one .tmp file created during processing and then deleted.
REM To prevent the .tmp file being deleted you can comment out the second
REM instance of this line by adding a REM in front of it:
REM if exist %TESTDIR%*.tmp del %TESTDIR%*.tmp
REM If you want to print out a .tmp file that lists the files counted for the
REM period given then you could remove the REM from in front of the following
REM line in the below code: echo %%~tF %%F>> %TESTDIR%MONTH_files.tmp
set "TAB= "
set "MONTHTOTAL=0"
set hr=%time:~0,2%
if "%hr:~0,1%" equ " " set hr=0%hr:~1,1%
set "TESTDIR=C:\TEST\"
if "%~2" == "" (
echo Please pass in arguments for starting directory, startdate, and enddate.
echo startdate and endate should be in the format mm/dd/yyyy
exit/b
)
if "%~3" == "" (
echo Please pass in arguments for starting directory, startdate, and enddate.
echo startdate and endate should be in the format mm/dd/yyyy
exit/b
)
set "startdate=%~2"
set "enddate=%~3"
if exist %TESTDIR%*.tmp del %TESTDIR%*.tmp
call :run >nul
FOR /F %%i IN (%TESTDIR%temp_TotalByDir.tmp) DO set /a MONTHTOTAL=!MONTHTOTAL!+%%i
echo %startdate%%TAB%%enddate%%TAB%!MONTHTOTAL! >> %TESTDIR%RetentionReport_%date:~- 4,4%%date:~-10,2%%date:~-7,2%_%hr%%time:~3,2%%time:~6,2%.txt
if exist %TESTDIR%*.tmp del %TESTDIR%*.tmp
exit /b
:run
for %%F in (.) do echo %%~fF
endlocal
:listFolder
setlocal enableDelayedExpansion
set "ADD=0"
for %%F in (*) do (
if %%~tF GEQ %startdate% (
if %%~tF LEQ %enddate% (
REM echo %%~tF %%F>> %TESTDIR%MONTH_files.tmp
set /a ADD+=1
)
)
)
echo !ADD! >> %TESTDIR%temp_TotalByDir.tmp
for /d %%F in (*) do (
pushd "%%F"
call :listFolder
popd
)
endlocal
exit /b
在此先感谢您的帮助!!!