我有大约 15000 个 zip 文件,总大小刚好超过 10 gb,需要分类存放到大小不超过 200 mb 的文件夹中。(没有一个文件大于 40 mb,大多数文件小于 1mb)
现在它们都在一个文件夹中,命名约定(如果需要)是 CON-XXXXX.zip(其中 xxxxx 是 00001 到 15000 之间的数字)
有没有什么好主意,可以用 bat/vbs 或其他东西来创建所需的文件夹(名称不重要)并将文件移动或复制到其中?
非常感谢您的帮助!我不知道该怎么做,因为我不知道如何“跟踪”文件夹的大小,以便当文件夹接近 200 mb 时,脚本会创建一个新文件夹。不过,我对脚本了解不多。
答案1
这是为您提供的一个示例批处理脚本。
它将按照 Folder1、Folder2 等模式创建文件夹。您必须将第二行中的“Filepath”替换为 .zip 文件所在的路径。
代码(已测试)
@echo off
pushd "Filepath"
setlocal enabledelayedexpansion
set fn=1
for %%a in (*.zip) do (
if not exist "Folder!fn!" md "Folder!fn!"
for %%b in ("%__cd__%Folder!fn!") do if "%%~zb" gtr "209715200" (
set /a fn=!fn!+1
md "Folder!fn!"
)
move "%%~fa" "Folder!fn!\"
)
答案2
Groovy 中的解决方案。
该脚本应该是独立于平台的,以防您需要在非 Windows 系统上运行它。
设置和文件收集
def archiveRoot = './test/backups'
def destinationRoot = archiveRoot
def maxDirSize = 200 * 1024 * 1024
def dirSize = maxDirSize + 1 // Size of current sub-dir
def dirNum = 1 // Sequence No. of next sub-dir
String dirName = "" // Name of current sub-dir
zipFiles = new AntBuilder().fileScanner {
fileset( dir: archiveRoot ) {
include name: "*.zip"
}
}
变量zipFiles
现在是需要移动的每个 zip 文件的列表。
移动文件、创建子文件夹
文件大小逻辑是唯一重要的部分。AntBuilder 将移动文件,如果目录不存在,则创建目录。
zipFiles.each { file ->
println '-' * 45
def newDirSize = dirSize + file.size()
printf '%10s, size: %s dirSize: %s, would be: %s\n',
file.name, sizeInMeg( file.size() ),
sizeInMeg( dirSize ), sizeInMeg( newDirSize )
if ( newDirSize >= maxDirSize ) {
dirName = "subDir_${dirNum++}"
println '=' * 45
dirSize = 0
}
dirSize += file.size()
new AntBuilder().move(
file: file.absolutePath,
todir: "${destinationRoot}/${dirName}"
)
println "new size: ${sizeInMeg(dirSize)}"
}
String sizeInMeg( size ) {
sprintf '%2.2f', size / 1024 / 1024
}
示例输出
使用这些文件运行时:
28/07/2020 12:28 <DIR> .
28/07/2020 12:28 <DIR> ..
18/08/2016 18:06 45,908,522 0001.zip
06/12/2018 07:35 110,335,437 0002.zip
27/08/2016 04:54 87,199,956 0003.zip
05/12/2018 02:40 127,576,284 0004.zip
01/08/2016 01:24 126,012,971 0005.zip
5 File(s) 497,033,170 bytes
2 Dir(s) 27,829,731,328 bytes free
这些文件比你的大得多,它们只是我手头上的一堆文件。
最终的终端输出是:
---------------------------------------------
0001.zip, size: 43.78 dirSize: 200.00, would be: 243.78
=============================================
[move] Moving 1 file to c:\test\backups\subDir_1
new size: 43.78
---------------------------------------------
0002.zip, size: 105.22 dirSize: 43.78, would be: 149.01
[move] Moving 1 file to c:\test\backups\subDir_1
new size: 149.01
---------------------------------------------
0003.zip, size: 83.16 dirSize: 149.01, would be: 232.17
=============================================
[move] Moving 1 file to c:\test\backups\subDir_2
new size: 83.16
---------------------------------------------
0004.zip, size: 121.67 dirSize: 83.16, would be: 204.83
=============================================
[move] Moving 1 file to c:\test\backups\subDir_3
new size: 121.67
---------------------------------------------
0005.zip, size: 120.18 dirSize: 121.67, would be: 241.84
=============================================
[move] Moving 1 file to c:\test\backups\subDir_4
new size: 120.18
两个文件被移到第一个子目录中。其余三个文件相当大,因此每个文件都有自己的子目录。对于明显较小的文件,正如您所看到的,几乎所有目录都会有多个文件。最后一个目录可能最终只有一个文件。
在真实环境中运行
由于要扫描的文件太多,您可能希望删除所有println
行。然后,您只会看到要移动的文件列表以及目标。即每个文件一行。
答案3
@echo off & title <nul
setlocal enabledelayedexpansion
color 0a && cd /d "%~dp0" & title .\%~nx0
:next
set "_zOk=." && set _dir=<nul
set /a "_cnt+=1+0, _sz_lmt=209715200"
if not exist *.zip endlocal && goto=:EOF
for /f tokens^=* %%i in ('dir /b /o:s *.zip')do (
call :^) %%~zi 0000 "%%~fi" !_cnt! && echo\
2>nul cd /d "%~dp0!_dir!" && cd.. || mkdir "%~dp0!_dir!"
if not !_sz_lmt! gtr %%~zi move "%%~fi" "!_dir!\" >nul && echo\ Moved: "%%~fj" ".\!_dir!"
set /a "_sz_lmt-=%%~zi" && echo\Folder: "%~dp0!_dir!"
:loop
for /f tokens^=* %%j in ('dir /b /o:-s *.zip')do for /f %%K in ('
set /a !_sz_lmt!-%%~zj')do set "_szj=%%~K" && if not !_szj! leq -1 (
move "%%~fj" "!_dir!\" >nul && set /a "_sz_lmt=!_szj!" && echo\ Moved: "%%~fj" ".\!_dir!"
)
call :^] "%~dp0!_dir!" && goto=:next
)
:^)
set "_lst=%~1" && set "_dir=00%~4" || endlocal && goto=:EOF
set "_lst_=%~3!_lst!" && call set "_dir=Zip_!_dir:~-3!" & exit /b
:^]
for /f tokens^=3 %%i in ('dir "%~1\*"^|findstr.exe ",.*bytes"
')do set "_zf=%%~i" && call set /a "_zf=!_zf:,=!/1024/1024" && (
cmd.exe /v:on /c "echo\Folder: "%~1\" Size: !_zf!MB" && exit /b )
基本上,使用一个 for 循环来获取 1º 较大的文件,使用 2 个循环来获取其他较小的文件,直到总和达到 200 MB 或近似值,添加一个计数器来创建文件夹 Zip_001-Zip_xxx
- 我的文件夹的输出 = 3.8 GB
Folder: "F:\zip_test\Zip_001"
Moved: "F:\zip_test\0084.zip" ".\Zip_001"
Moved: "F:\zip_test\0089.zip" ".\Zip_001"
Moved: "F:\zip_test\0086.zip" ".\Zip_001"
Folder: "F:\zip_test\Zip_001\" Size: 191MB
Folder: "F:\zip_test\Zip_002"
Moved: "F:\zip_test\0013.zip" ".\Zip_002"
Moved: "F:\zip_test\0030.zip" ".\Zip_002"
Moved: "F:\zip_test\0115.zip" ".\Zip_002"
Folder: "F:\zip_test\Zip_002\" Size: 191MB
Folder: "F:\zip_test\Zip_003"
Moved: "F:\zip_test\0140.zip" ".\Zip_003"
Moved: "F:\zip_test\0108.zip" ".\Zip_003"
Moved: "F:\zip_test\0051.zip" ".\Zip_003"
Folder: "F:\zip_test\Zip_003\" Size: 187MB
Folder: "F:\zip_test\Zip_004"
Moved: "F:\zip_test\0058.zip" ".\Zip_004"
Moved: "F:\zip_test\0057.zip" ".\Zip_004"
Moved: "F:\zip_test\0075.zip" ".\Zip_004"
Folder: "F:\zip_test\Zip_004\" Size: 188MB
Folder: "F:\zip_test\Zip_005"
Moved: "F:\zip_test\0056.zip" ".\Zip_005"
Moved: "F:\zip_test\0035.zip" ".\Zip_005"
Moved: "F:\zip_test\0036.zip" ".\Zip_005"
Folder: "F:\zip_test\Zip_005\" Size: 189MB
Folder: "F:\zip_test\Zip_006"
Moved: "F:\zip_test\0055.zip" ".\Zip_006"
Moved: "F:\zip_test\0059.zip" ".\Zip_006"
Moved: "F:\zip_test\0110.zip" ".\Zip_006"
Moved: "F:\zip_test\0135.zip" ".\Zip_006"
Folder: "F:\zip_test\Zip_006\" Size: 191MB
Folder: "F:\zip_test\Zip_007"
Moved: "F:\zip_test\0049.zip" ".\Zip_007"
Moved: "F:\zip_test\0052.zip" ".\Zip_007"
Moved: "F:\zip_test\0062.zip" ".\Zip_007"
Moved: "F:\zip_test\0070.zip" ".\Zip_007"
Folder: "F:\zip_test\Zip_007\" Size: 190MB
Folder: "F:\zip_test\Zip_008"
Moved: "F:\zip_test\0011.zip" ".\Zip_008"
Moved: "F:\zip_test\0074.zip" ".\Zip_008"
Moved: "F:\zip_test\0069.zip" ".\Zip_008"
Moved: "F:\zip_test\0100.zip" ".\Zip_008"
Moved: "F:\zip_test\0077.zip" ".\Zip_008"
Folder: "F:\zip_test\Zip_008\" Size: 191MB
Folder: "F:\zip_test\Zip_009"
Moved: "F:\zip_test\0155.zip" ".\Zip_009"
Moved: "F:\zip_test\0119.zip" ".\Zip_009"
Moved: "F:\zip_test\0032.zip" ".\Zip_009"
Moved: "F:\zip_test\0060.zip" ".\Zip_009"
Moved: "F:\zip_test\0085.zip" ".\Zip_009"
Folder: "F:\zip_test\Zip_009\" Size: 191MB
Folder: "F:\zip_test\Zip_010"
Moved: "F:\zip_test\0083.zip" ".\Zip_010"
Moved: "F:\zip_test\0173.zip" ".\Zip_010"
Moved: "F:\zip_test\0017.zip" ".\Zip_010"
Moved: "F:\zip_test\0050.zip" ".\Zip_010"
Moved: "F:\zip_test\0158.zip" ".\Zip_010"
Moved: "F:\zip_test\0079.zip" ".\Zip_010"
Folder: "F:\zip_test\Zip_010\" Size: 191MB
Folder: "F:\zip_test\Zip_011"
Moved: "F:\zip_test\0107.zip" ".\Zip_011"
Moved: "F:\zip_test\0001.zip" ".\Zip_011"
Moved: "F:\zip_test\0008.zip" ".\Zip_011"
Moved: "F:\zip_test\0031.zip" ".\Zip_011"
Moved: "F:\zip_test\0167.zip" ".\Zip_011"
Moved: "F:\zip_test\0144.zip" ".\Zip_011"
Folder: "F:\zip_test\Zip_011\" Size: 186MB
Folder: "F:\zip_test\Zip_012"
Moved: "F:\zip_test\0104.zip" ".\Zip_012"
Moved: "F:\zip_test\0065.zip" ".\Zip_012"
Moved: "F:\zip_test\0118.zip" ".\Zip_012"
Moved: "F:\zip_test\0142.zip" ".\Zip_012"
Moved: "F:\zip_test\0143.zip" ".\Zip_012"
Moved: "F:\zip_test\0102.zip" ".\Zip_012"
Moved: "F:\zip_test\0149.zip" ".\Zip_012"
Folder: "F:\zip_test\Zip_012\" Size: 184MB
Folder: "F:\zip_test\Zip_013"
Moved: "F:\zip_test\0148.zip" ".\Zip_013"
Moved: "F:\zip_test\0117.zip" ".\Zip_013"
Moved: "F:\zip_test\0145.zip" ".\Zip_013"
Moved: "F:\zip_test\0087.zip" ".\Zip_013"
Moved: "F:\zip_test\0114.zip" ".\Zip_013"
Moved: "F:\zip_test\0116.zip" ".\Zip_013"
Moved: "F:\zip_test\0127.zip" ".\Zip_013"
Moved: "F:\zip_test\0103.zip" ".\Zip_013"
Folder: "F:\zip_test\Zip_013\" Size: 191MB
Folder: "F:\zip_test\Zip_014"
Moved: "F:\zip_test\0076.zip" ".\Zip_014"
Moved: "F:\zip_test\0162.zip" ".\Zip_014"
Moved: "F:\zip_test\0111.zip" ".\Zip_014"
Moved: "F:\zip_test\0007.zip" ".\Zip_014"
Moved: "F:\zip_test\0006.zip" ".\Zip_014"
Moved: "F:\zip_test\0129.zip" ".\Zip_014"
Moved: "F:\zip_test\0067.zip" ".\Zip_014"
Moved: "F:\zip_test\0109.zip" ".\Zip_014"
Folder: "F:\zip_test\Zip_014\" Size: 186MB
Folder: "F:\zip_test\Zip_015"
Moved: "F:\zip_test\0016.zip" ".\Zip_015"
Moved: "F:\zip_test\0113.zip" ".\Zip_015"
Moved: "F:\zip_test\0147.zip" ".\Zip_015"
Moved: "F:\zip_test\0150.zip" ".\Zip_015"
Moved: "F:\zip_test\0019.zip" ".\Zip_015"
Moved: "F:\zip_test\0018.zip" ".\Zip_015"
Moved: "F:\zip_test\0160.zip" ".\Zip_015"
Moved: "F:\zip_test\0136.zip" ".\Zip_015"
Moved: "F:\zip_test\0038.zip" ".\Zip_015"
Folder: "F:\zip_test\Zip_015\" Size: 190MB
Folder: "F:\zip_test\Zip_016"
Moved: "F:\zip_test\0028.zip" ".\Zip_016"
Moved: "F:\zip_test\0005.zip" ".\Zip_016"
Moved: "F:\zip_test\0168.zip" ".\Zip_016"
Moved: "F:\zip_test\0138.zip" ".\Zip_016"
Moved: "F:\zip_test\0009.zip" ".\Zip_016"
Moved: "F:\zip_test\0010.zip" ".\Zip_016"
Moved: "F:\zip_test\0088.zip" ".\Zip_016"
Moved: "F:\zip_test\0161.zip" ".\Zip_016"
Moved: "F:\zip_test\0093.zip" ".\Zip_016"
Moved: "F:\zip_test\0066.zip" ".\Zip_016"
Folder: "F:\zip_test\Zip_016\" Size: 187MB
Folder: "F:\zip_test\Zip_017"
Moved: "F:\zip_test\0166.zip" ".\Zip_017"
Moved: "F:\zip_test\0152.zip" ".\Zip_017"
Moved: "F:\zip_test\0151.zip" ".\Zip_017"
Moved: "F:\zip_test\0043.zip" ".\Zip_017"
Moved: "F:\zip_test\0153.zip" ".\Zip_017"
Moved: "F:\zip_test\0082.zip" ".\Zip_017"
Moved: "F:\zip_test\0081.zip" ".\Zip_017"
Moved: "F:\zip_test\0096.zip" ".\Zip_017"
Moved: "F:\zip_test\0171.zip" ".\Zip_017"
Moved: "F:\zip_test\0163.zip" ".\Zip_017"
Moved: "F:\zip_test\0097.zip" ".\Zip_017"
Folder: "F:\zip_test\Zip_017\" Size: 186MB
Folder: "F:\zip_test\Zip_018"
Moved: "F:\zip_test\0098.zip" ".\Zip_018"
Moved: "F:\zip_test\0063.zip" ".\Zip_018"
Moved: "F:\zip_test\0080.zip" ".\Zip_018"
Moved: "F:\zip_test\0053.zip" ".\Zip_018"
Moved: "F:\zip_test\0061.zip" ".\Zip_018"
Moved: "F:\zip_test\0094.zip" ".\Zip_018"
Moved: "F:\zip_test\0095.zip" ".\Zip_018"
Moved: "F:\zip_test\0106.zip" ".\Zip_018"
Moved: "F:\zip_test\0159.zip" ".\Zip_018"
Moved: "F:\zip_test\0172.zip" ".\Zip_018"
Moved: "F:\zip_test\0090.zip" ".\Zip_018"
Moved: "F:\zip_test\0078.zip" ".\Zip_018"
Moved: "F:\zip_test\0120.zip" ".\Zip_018"
Moved: "F:\zip_test\0073.zip" ".\Zip_018"
Folder: "F:\zip_test\Zip_018\" Size: 191MB
Folder: "F:\zip_test\Zip_019"
Moved: "F:\zip_test\0137.zip" ".\Zip_019"
Moved: "F:\zip_test\0002.zip" ".\Zip_019"
Moved: "F:\zip_test\0037.zip" ".\Zip_019"
Moved: "F:\zip_test\0020.zip" ".\Zip_019"
Moved: "F:\zip_test\0027.zip" ".\Zip_019"
Moved: "F:\zip_test\0004.zip" ".\Zip_019"
Moved: "F:\zip_test\0124.zip" ".\Zip_019"
Moved: "F:\zip_test\0123.zip" ".\Zip_019"
Moved: "F:\zip_test\0122.zip" ".\Zip_019"
Moved: "F:\zip_test\0125.zip" ".\Zip_019"
Moved: "F:\zip_test\0164.zip" ".\Zip_019"
Moved: "F:\zip_test\0126.zip" ".\Zip_019"
Moved: "F:\zip_test\0112.zip" ".\Zip_019"
Moved: "F:\zip_test\0146.zip" ".\Zip_019"
Moved: "F:\zip_test\0025.zip" ".\Zip_019"
Moved: "F:\zip_test\0024.zip" ".\Zip_019"
Folder: "F:\zip_test\Zip_019\" Size: 187MB
Folder: "F:\zip_test\Zip_020"
Moved: "F:\zip_test\0046.zip" ".\Zip_020"
Moved: "F:\zip_test\0048.zip" ".\Zip_020"
Moved: "F:\zip_test\0047.zip" ".\Zip_020"
Moved: "F:\zip_test\0045.zip" ".\Zip_020"
Moved: "F:\zip_test\0044.zip" ".\Zip_020"
Moved: "F:\zip_test\0033.zip" ".\Zip_020"
Moved: "F:\zip_test\0068.zip" ".\Zip_020"
Moved: "F:\zip_test\0026.zip" ".\Zip_020"
Moved: "F:\zip_test\0141.zip" ".\Zip_020"
Moved: "F:\zip_test\0022.zip" ".\Zip_020"
Moved: "F:\zip_test\0021.zip" ".\Zip_020"
Moved: "F:\zip_test\0023.zip" ".\Zip_020"
Moved: "F:\zip_test\0099.zip" ".\Zip_020"
Moved: "F:\zip_test\0133.zip" ".\Zip_020"
Moved: "F:\zip_test\0157.zip" ".\Zip_020"
Moved: "F:\zip_test\0034.zip" ".\Zip_020"
Moved: "F:\zip_test\0165.zip" ".\Zip_020"
Moved: "F:\zip_test\0029.zip" ".\Zip_020"
Folder: "F:\zip_test\Zip_020\" Size: 185MB
Folder: "F:\zip_test\Zip_021"
Moved: "F:\zip_test\0064.zip" ".\Zip_021"
Moved: "F:\zip_test\0042.zip" ".\Zip_021"
Moved: "F:\zip_test\0121.zip" ".\Zip_021"
Moved: "F:\zip_test\0003.zip" ".\Zip_021"
Moved: "F:\zip_test\0041.zip" ".\Zip_021"
Moved: "F:\zip_test\0092.zip" ".\Zip_021"
Moved: "F:\zip_test\0101.zip" ".\Zip_021"
Moved: "F:\zip_test\0154.zip" ".\Zip_021"
Moved: "F:\zip_test\0169.zip" ".\Zip_021"
Moved: "F:\zip_test\0170.zip" ".\Zip_021"
Moved: "F:\zip_test\0040.zip" ".\Zip_021"
Moved: "F:\zip_test\0134.zip" ".\Zip_021"
Moved: "F:\zip_test\0130.zip" ".\Zip_021"
Moved: "F:\zip_test\0131.zip" ".\Zip_021"
Moved: "F:\zip_test\0132.zip" ".\Zip_021"
Folder: "F:\zip_test\Zip_021\" Size: 128MB
- 相关说明:
观察:1您需要/想要先测试一下,我建议使用另一个少于 1 GB(总计)文件的文件夹来检查是否有效,或者是否需要进行一些调整。
观察:2请记住,Windows 仍在后台执行许多操作,并且会不时“刷新”这些文件和文件夹,请注意防止可能的错误(请参阅下面的消息),在 Windows 资源管理器未打开 zip 文件夹的情况下运行此 bat
观察:3仔细读观察:1和观察:2在尝试测试这一点之前请再多试一次以避免出现以下消息:
该进程无法访问该文件,因为它正在被另一个进程使用。