Wir haben ein E-Mail-Archivierungsprogramm, das alle E-Mails in ein Verzeichnis speichert. Aus Leistungsgründen des Servers möchte ich eine automatisierte Aufgabe einrichten, die einmal am Tag ein Skript ausführt und, wenn sich mehr als 3.000 (oder eine beliebige Anzahl) Dateien im Hauptverzeichnis befinden, ein neues Verzeichnis mit dem Datum erstellt und alle Dateien des Hauptverzeichnisses dorthin verschiebt. Ich bin sicher, dass jemand bereits etwas Ähnliches geschrieben hat, also wäre es großartig, wenn mich jemand darauf hinweisen könnte. Batch-Datei oder Powershell wären beide in Ordnung.
Antwort1
Geschrieben und getestet. Kopieren Sie den folgenden Code in eine *.bat-Datei. Sie sollten das Verzeichnis ändern, in dem die E-Mails am Anfang des Codes vorhanden sind. Die Variable cBig wurde bereits auf 3000 gesetzt, aber Sie können dies ändern, wenn Sie möchten. Unten muss move *.txt geändert werden, um die Erweiterung der E-Mails widerzuspiegeln, die Sie verschieben. Sobald Sie es getestet haben und zufrieden sind, können Sie die Pausenbefehle entfernen ... sie helfen nur dabei, zu sehen, was vor sich geht. Viel Glück!
echo off
REM **navigate to the directory
cd\bat_test
REM **store count of files to file count.txt (/a-d removes folders from count)
dir /b /a-d | find /v /c "::" > count.txt
REM **read count back in to variable (easiest way I knew how to do this)
set /p myvar=<count.txt
REM **set your upper limit (in your case 3000)
set cBig=3000
REM **quick display of the number of files
echo %myvar%
pause
REM **is the number of files larger than our upper limit? If so goto BIG
if '%myvar%' gtr '%cBig%' goto BIG
:SMALL
REM **do nothing
exit
:BIG
REM **create new directory with date and move all files
Set FDate=%Date:~-10,10%
Set Fdate=%FDate:/=-%
MD %FDate%
move *.txt ./%FDate%
pause
Antwort2
Ungetestetes .CMD-Skript.
REM @echo off
setlocal enableextensions enabledelayedexpansion
rem Print all filenames (excl. folders) in current directory into temporary text-file
set TMPTXT=%TEMP%\%~n0.%RANDOM%.TMP
dir /B /A-D 1>%TMPTXT%
rem Count number of files (lines) in text-file
set FILECNT=0
for /F %%i in (%TMPTXT%) do (
set /A FILECNT=!FILECNT!+1
)
echo Number of files in folder: !FILECNT!
rem Is number of files greater than expected?
if /I !FILECNT! GTR 2999 call :MoveFiles
del %TMPTXT%
goto :EOF
:MoveFiles
rem Construct a folder-name based on date (remember date changes at midnight)
rem Since the date value is locale specific, you might want to fiddle with string-replacing.
set SUBFLDR=%DATE%
mkdir "%SUBFLDR%"
if /I !ERRORLEVEL! NEQ 0 (
echo Failed to create sub-folder '%SUBFLDR%'.
goto :EOF
)
rem Move only those files found in text-file to the new folder.
for /F %%f in (%TMPTXT%) do (
move "%%f" "%SUBFLDR%\."
if /I !ERRORLEVEL! NEQ 0 echo Failed to move file '%%f'
)
goto :EOF