寻找一个批处理脚本,该脚本允许从文件夹(.\source
)获取文件并.rar
在其他文件夹(.\target
)上创建文件,并使用这些文件,但其中所有文件的最大大小是给定的.rar
。
我已经存档、在文件夹之间移动文件和/或创建.rar
包含所有源文件的文件。
问题是收集可变数量的文件以达到给定(输入选项)的聚合文件的最大大小。
找到了这个答案(分裂成.\folders
,而不是.zip
文件)回答@lo-ol:
@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 )
但无法弄清楚如何将其反转为.zip
文件。
EDIT2(修订澄清):
有一个文件夹(.\source),其中有多个(假设有 100 个文件).CSV 文件(source_file),大小可变,范围从(大约)500kb 到 +50Mb,带有一个“孪生”标志文件 - 尺寸较小(低于 100kb 左右),名称变化不大(source_file_flag)。
打算:
- 选择给定数量的文件(source_file + source_file_flag)以达到给定的总文件大小(Mb)- 当前可以选择给定数量(n)的文件
- 将 #1 中的“source_file”聚合到单个 .CSV 文件中,保存到 .\target 中,作为 pack_xx.csv(xx 为一个递增的数字)
- 将打包的“source_file”列在 pack_xxx_list.txt 中,放到 .\target 中
- 将“source_file”和相应的“source_file_flag”表格 #1 添加到 rar 文件中,并保存为 .\targetfile,即 Pack_xx.rar
- 从 .\temp 文件夹中删除 #1 和 #2 中的临时文件
以下是已存档的代码:允许选择给定数量的文件(n)并执行所有任务,然后重新运行批处理脚本,直到没有可用的文件
@echo off && setlocal enabledelayedexpansion
set "_source_folder=D:\source"
REM Files in folder D:\source
REM ------------------------------------------
REM 1000-source-file.csv (main file)
REM 1000-source-file-flag.csv (flag file)
REM 1001-source-file.csv (main file)
REM 1001-source-file-flag.csv (flag file)
REM ...
set "_target_folder=D:\target"
set "_temp_folder=D:\temp"
REM ===============================================================================
REM TASK 1 - select files from source
REM ===============================================================================
::: currently is copy a given number of files (_SrcMax) the "_temp_folder",
::: objective is to copy a given amount/size (Mb) of files, so each package as around the same size
set "_DataLoc=%_temp_folder%"
Set "_HoldLoc=%_source_folder%"
for /F %%a in ('dir /b "%_DataLoc%\*source-file.csv" ^| find /v /c ""
') do if %%~a GEQ 60 (goto work) else (goto FMove)
:FMove
echo Gather Top 40 files
::: maximum number of files to copy
set "_Src_count=0"
for /f "delims=" %%a in ('dir /a-d /o-d /b "%_HoldLoc%"\*source-file*.csv"') do (
set /A "_Src_count+=1"
if !_Src_count! LEQ 40 move /y "%_HoldLoc!\%%~a" "%_DataLoc%"
)
REM ===============================================================================
REM TASK 2 - create agregated CSV from selected files in TASK 1 (only "main" files)
REM ===============================================================================
::: create temporary agregated CSV file
copy "%_temp_folder%\*source-file.csv" "%_temp_folder%\temp_0.csv" /b
::: Set Pack number
set "_pack=01"
::: create "pack number" final agregated CSV file with proper headings
>"%_target_folder%\Pack_%_pack%.csv" echo.Date,Time
::: remove lines with headings from the temporary agregated CSV file created above
>"%_temp_folder%\temp_1.csv" findstr /v "Column1,Column2" "%_temp_folder%\temp_0.csv"
::: adds the data from temporary agregated CSV file to the final agregated CSV file
>>"%_target_folder%\Pack_%_pack%.csv" type "%_temp_folder%\temp_1.csv"
REM ===============================================================================
REM TASK 3 - create list of csv files on Pack (only "main" files)
REM ===============================================================================
>>"%_target_folder%\Pack_%_pack%_LIST.txt" (
for /f "usebackq delims=" %%a in (`type "%_temp_folder%\*source-file.csv"
`) do echo/%%~na
)
REM ===============================================================================
REM TASK 4 - create zip files (archive) of the selected files in TASK 1 ("main" + "flag" files)
REM ===============================================================================
"C:\Program Files\WinRAR\winrar.exe" a "%_target_folder%\Pack_%_pack%.rar" "%_temp_folder%\*source-file*.csv"
REM ===============================================================================
REM TASK 5 - delete files from temp folder // clean temporary files
REM ===============================================================================
del /q /a "%_temp_folder%\*.*"
目标是在任务 #1 上选择文件以达到给定的总文件大小 (Mb)(如上面的示例代码,用于文件夹)- 因此每个创建的“pack_xx.csv”大小大致相同。并运行它,直到所有文件都“打包”,除非剩余文件小于给定的总大小。