[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[文件操作] BAT如何批量压缩文件夹的文件包括子目录下的文件?

把文件压缩到一个到临时一个文件,当临时文件体积大于20mb的时候,
要求删除要求删除压缩包里面最后一个文件,使其体积不大于20mb,
然后保存到001.zip或001.rar。然后把临时压缩包体积重置0,继续打包
后面的文件,当体积大于20mb的时候,也删掉临时压缩包的最后一个文件。
然后把临时文件改为002.zip或rar。不知道表达的清楚吗?

本帖最后由 netdzb 于 2019-6-17 11:56 编辑

回复 2# Batcher

分卷压缩是不能单独解压,要求能够单独解压。

代码我能写一点,
就是打包的时候,绝对路径怎么写,我不知道怎么写?

TOP

本帖最后由 netdzb 于 2019-6-17 12:50 编辑

回复 4# Batcher


@echo on
setlocal enabledelayedexpansion

for /f "delims=" %%i in ('dir /a-d /b *.*') do (
set n=001
     rar a temp.rar %%i
     if temp.rar的大小,小于等于20mb
        copy temp.rar !n!.rar
     else
       (
        del temp.rar
        n=n+1
       )
  )

temp.rar的大小用什么变量表示,写了一部分,帮忙修改一下,谢谢。

TOP

回复 6# Batcher

temp.rar是常量, %%~zi是变量,

如何判断temp.rar小于20MB的代码呢?常量的写法我不会啊。

TOP

回复 8# xczxczxcz

我装的是powershell V2版本,代码无法通过。

TOP

回复 10# xczxczxcz

powershell -file 脚本.ps1
是这样运行吗?

刚弄了个Hello,World可以运行,

Write-Host "Hello World!"
Write-Host "lk"

把上面保存到脚本里面,powershell -file 脚本.ps1还是报错,不知道哪里不对?

TOP

回复 10# xczxczxcz

哪里有好用的powershell,手上的版本好像被微软禁用了。

TOP

回复 8# xczxczxcz

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>d:

D:\>cd \power

D:\power>powershell hello.ps1
The term 'hello.ps1' is not recognized as a cmdlet, function, operable program,
or script file. Verify the term and try again.
At line:1 char:10
+ hello.ps1 <<<<

D:\power>

我报上面的错误。

TOP

回复 15# xczxczxcz

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>i:

I:\>cd hetxt

I:\hetxt>path d:\power

I:\hetxt>powershell
Windows PowerShell V2 (Community Technology Preview - Features Subject to Change
)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.

PS I:\hetxt> ./yan.ps1
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
The term 'rar' is not recognized as a cmdlet, function, operable program, or sc
ript file. Verify the term and try again.
At I:\hetxt\yan.ps1:7 char:3
+     & <<<<  rar a -ep $name $files[$Begin].FullName|Out-Null;
Get-Item : Cannot find path 'I:\hetxt\001.rar' because it does not exist.
At I:\hetxt\yan.ps1:8 char:15
+     if ((Get-Item <<<<  $name).Length/1MB -gt 20) {
PS I:\hetxt>

可以运行了,但是遇到rar找不到的问题,怎么解决?
控制台我能运行rar,环境变量我已经设置了啊。

TOP

回复 15# xczxczxcz

代码运行了,结果不正确。文件夹下面的文件被压缩了。下面的子文件夹没有被打包。如果子文件下面还有子
文件夹还要继续被打包。

TOP

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.

C:\WINDOWS\system32>i:

I:\>cd \hetxt

I:\hetxt>path d:\power

I:\hetxt>powershell
Windows PowerShell V2 (Community Technology Preview - Features Subject to Change
)
Copyright (C) 2007 Microsoft Corporation. All rights reserved.

PS I:\hetxt> ./yan.ps1
PS I:\hetxt> ls


    Directory: Microsoft.PowerShell.Core\FileSystem::I:\hetxt


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----         2019-6-17     13:54            yhgf
-a---          2019-6-1     12:50        133 hebing.bat
-a---          2019-6-2     11:30        117 hel.sh
-a---          2019-6-2     11:36        116 hel.sh.txt
-a---         2019-6-17     16:32         47 hello.ps1
-a---          2019-6-1     12:33        145 shiyan.bat
-a---         2019-6-17     16:21        254 test.bat
-a---          2019-6-2     11:31        100 total.txt
-a---         2019-6-17     17:12        341 yan.ps1
-a---          2019-6-1     11:58          8 密码01.txt
-a---          2019-6-1     11:58          8 密码02.txt
-a---          2019-6-1     11:58         10 密码03.txt
-a---          2019-6-1     11:58         11 密码04.txt
-a---          2019-6-1     11:59         10 密码05.txt
-a---          2019-6-1     11:59         12 密码06.txt
-a---          2019-6-1     11:59         11 密码07.txt
-a---          2019-6-1     11:59         14 密码08.txt

文件被打包了,文件夹yhgf没有被打包。

TOP

回复 21# xczxczxcz

@echo on
setlocal enabledelayedexpansion
set num=100
for /f "delims=" %%i in ('dir /a-d /b /s *.*') do (
     rar a i:\hand\temp.rar %%i
     copy/y i:\hand\temp.rar i:\hand\!num:~-2!.rar
     set /a num+=1
     del i:\hand\temp.rar
   )
pause

代码我修改了,文件大小的判断我搞不定。能否增加文件夹判断的功能,谢谢!

TOP

回复 23# xczxczxcz

测试了,还是没有文件夹。

TOP

回复 25# xczxczxcz

把ep去掉就文件夹了。

TOP

回复 28# Batcher

代码有错误,会在页面显示代码,好像是多了括号。

TOP

返回列表