3,000개 파일의 디렉터리를 생성하는 Windows 스크립트

3,000개 파일의 디렉터리를 생성하는 Windows 스크립트

모든 이메일을 디렉토리에 버리는 일부 이메일 보관 기능이 있습니다. 서버의 일부 성능상의 이유로 하루에 한 번 스크립트를 실행하는 자동화된 작업을 설정하고 기본 디렉터리에 3,000개(또는 원하는 수) 이상의 파일이 있는 경우 날짜가 포함된 새 디렉터리를 생성하고 싶습니다. 모든 기본 디렉터리 파일을 해당 폴더로 이동합니다. 누군가가 이미 비슷한 것을 썼을 것이라고 확신합니다. 누군가가 저에게 그것을 지적해 줄 수 있다면 좋을 것입니다. 배치 파일이나 Powershell 모두 괜찮습니다.

답변1

작성하고 테스트했습니다. 다음 코드를 *.bat 파일에 복사합니다. 코드 시작 부분에서 이메일이 존재하는 디렉터리를 수정하고 싶을 것입니다. cBig 변수는 이미 3000으로 설정되어 있지만 원하는 경우 이를 변경할 수 있습니다. 하단에서 이동하려는 이메일의 확장자를 반영하도록 move *.txt를 변경해야 합니다. 테스트하고 만족스러우면 일시 중지 명령을 제거할 수 있습니다. 이는 단지 무슨 일이 일어나고 있는지 확인하는 데 도움이 될 뿐입니다. 행운을 빌어요!

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

답변2

테스트되지 않은 .CMD 스크립트.

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

관련 정보