指定した量のファイルを親フォルダからサブフォルダに移動するバッチ スクリプト

指定した量のファイルを親フォルダからサブフォルダに移動するバッチ スクリプト

バッチファイルスクリプトを使用して、1つの親フォルダから必要な数のサブフォルダにすべてのファイルを移動する必要があります。ただし、各サブフォルダには、バッチファイルまたはコマンドラインで指定された最大数のファイルが格納されます。つまり、スクリプトは、親フォルダのすべてのファイルをその下の複数のサブフォルダ(スクリプトが自動的に作成する必要があります)に配布し、ファイルを複数のサブフォルダに移動する必要があります。バツファイル(「バツ「各サブフォルダーが受信するファイルの量」です。

追加要件:

  1. サブフォルダー名は、001、002、003 などの命名法に従って作成する必要があります。

  2. あらゆる種類のファイル名(スペース、特殊文字、英語以外のアクセントなどを含む)を持つファイルをサポートする必要があります。

  3. 数万のファイルの移動をサポートする必要があります。

  4. Windows 10 Professional ではローカル管理者権限で動作する必要があります。

たとえば、フォルダー「D:\Downloads」に 2400 個のファイルがあり、それらをサブフォルダーに分散して、各サブフォルダーに最大 1000 個のファイルを格納したいとします。スクリプトを実行すると、次の構造が作成されます。

D:\Downloads
       |__001
       |__002
       |__003

どこ:

D:\Downloads --> Will have no files inside it anymore
       |__001 --> Will have 1000 files inside it
       |__002 --> Will have 1000 files inside it
       |__003 --> Will have the last 400 files inside it

ファイルの順序 (どのファイルがどのサブフォルダーに移動するか) は重要ではありません。つまり、移動時に特定の基準 (ファイル名、サイズ、ファイルタイプなど) を考慮する必要はありません。ただし、この点に関する改善があれば歓迎します (たとえば、最後に作成されたファイルを最初に最初のサブフォルダーに移動するオプションなど)。

何か案は?


アップデート

私にとって効果があった解決策は次のとおりです:

@echo off
setlocal enableextensions
setlocal enabledelayedexpansion

if not %3.==. goto syntax
if %2.==. goto syntax
:: Checks if %2 is a number:
SET "var="&for /f "delims=0123456789" %%i in ("%2") do set var=%%i
if defined var (goto syntax) 
if /i %1.==. goto syntax
if /i not exist %1 echo. & echo  The folder %1 does not exist... && echo  Folder paths must be typed between "quotes" if there's any empty space. && echo. && goto end

setlocal enableextensions
setlocal enabledelayedexpansion
:: Maximum amount of files per subfolder:
SET limit=%2
:: Initial counter (everytime counter is 1, a new subfolder is created):
SET n=1
:: Subfolder counter:
SET nf=0
::Retrieves the amount of files in the specified folder:
set count=0
for %%A in (%1%\*.*) do set /a count+=1
echo.
echo  Distributing %count% files in subfolders of up to %2 files...
FOR %%f IN (%1%\*.*) DO (
  :: If counter is 1, create a new subfolder with name starting with "00...":
  IF !n!==1 (
    SET /A nf+=1
    MD %1%\00!nf!
  )
  :: Move files into subfolders with names starting with "00...":
  MOVE /-Y "%%f" %1%\00!nf! > NUL
  :: Reset counter when a subfolder reaches the maximum file limit:
  IF !n!==!limit! (
    SET n=1
  ) ELSE (
    SET /A n+=1
  )
)
SET limit=
SET n=
SET nf=
SET count=
echo  Move finished successfully.
goto end

:syntax
echo.
echo  YOU TYPED: movedown %*
echo  SYNTAX: movedown ["full path"] (between quotes if there's any space) [n] (maximum number of files per subfolder)
echo.
:end
ENDLOCAL

関連情報