私は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
よろしくお願いします!!!