标题: [文本处理] 【已解决】bat将当前目录往下的第三级子文件夹分别打包成zip或rar [打印本页]
作者: 7803181 时间: 2021-1-22 19:24 标题: 【已解决】bat将当前目录往下的第三级子文件夹分别打包成zip或rar
bat将当前目录下的三级子文件夹分别打包成zip或rar
如下图:图3为压缩目标(文件夹名称不一定是123这些,有些可能是中文甚至有空格和特殊字符。
作者: flashercs 时间: 2021-1-23 12:39
- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- $dir2Compress = ".\*\*\*"
- Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop
- foreach ($item in (Resolve-Path -Path $dir2Compress -Relative)) {
- if (Test-Path -LiteralPath $item -PathType Container) {
- try {
- $zipfile = "$item.zip"
- Write-Host "$item -> $zipfile" -ForegroundColor Green
- Remove-Item -LiteralPath $zipfile -ErrorAction Ignore
- [System.IO.Compression.ZipFile]::CreateFromDirectory($item, $zipfile, "Optimal", $true, [System.Text.Encoding]::UTF8)
-
- } catch {
- $_ | Write-Host -ForegroundColor Red
- }
-
- }
- }
复制代码
作者: 7803181 时间: 2021-1-23 13:08
回复 2# flashercs
需要什么软件支持吗。我的是win7 64位,WinRAR.exe
作者: went 时间: 2021-1-23 14:10
rarPath设置为你winrar的路径- @echo off & cd /d "%~dp0"
- set "rarPath=C:\Program Files\WinRAR"
- set "path=%path%;%rarPath%"
- set "dirList="
- for /d %%i in (*) do call set "dirList=%%dirList%% "%%i""
- for %%i in ("%cd%") do rar a "%%~nxi.rar" %dirList%
- pause&exit
复制代码
作者: 7803181 时间: 2021-1-23 14:17
回复 4# went
大神,不行,这个只是把当前目录下所有文件打包。不是我想要的批量打包第三级目录,然后包就放在第三级目录
作者: flashercs 时间: 2021-1-23 15:10
本帖最后由 flashercs 于 2021-1-23 15:13 编辑
- <#*,:&cls
- @echo off
- pushd "%~dp0"
- powershell -NoProfile -ExecutionPolicy RemoteSigned -Command ". ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~0\" -ReadCount 0 | Out-String ))) "
- popd
- pause
- exit /b
- #>
- $dir2Compress = ".\*\*\*"
- $rarexe="C:\Program Files\WinRAR\rar.exe"
-
- foreach ($item in (Resolve-Path -Path $dir2Compress -Relative)) {
- if (Test-Path -LiteralPath $item -PathType Container) {
- try {
- $zipfile = "$item.rar"
- Write-Host "$item -> $zipfile" -ForegroundColor Green
- Remove-Item -LiteralPath $zipfile -ErrorAction Ignore
- & $rarexe a $zipfile $item | Out-Null
- } catch {
- $_ | Write-Host -ForegroundColor Red
- }
- }
- }
复制代码
改成用WinRar,改下winrar安装路径
作者: 7803181 时间: 2021-1-23 15:26
回复 6# flashercs
感谢大神,不过我已经找到了,我把代码贴出来供大家借鉴学习。- /*&cls
- @echo off
- rem 将一个指定目录下指定层级的子文件夹进行压缩打包
- set #=Any question&set @=WX&set $=Q&set/az=0x53b7e0b4
- title %#% +%$%%$%/%@% %z%
- set "mainfolder=C:\Users\Administrator\Desktop\aaaaaaaaa\"
- set "exefile=C:\Program Files\WinRAR\WinRAR.exe"
- set level=3
- if not exist "%mainfolder%" (echo;"%mainfolder%" not found&pause&exit)
- if not exist "%exefile%" (echo;"%exefile%" not found&pause&exit)
- if "%mainfolder:~-1%" equ "\" set "mainfolder=%mainfolder:~,-1%"
- for /f "delims=" %%a in ('dir /ad-h/b/s "%mainfolder%\"^|cscript -nologo -e:jscript "%~f0" "%mainfolder%" "%level%"') do (
- echo;"%%a"
- "%exefile%" a -y -ep1 -r "%%~dpa%%~na.rar" "%%a\*"
- )
- echo;%#% +%$%%$%/%@% %z%
- pause
- exit
- */
- var fd=WSH.Arguments(0);
- while(!WSH.StdIn.AtEndOfStream){
- var it=WSH.StdIn.ReadLine();
- var arr=it.substring(fd.length).split('\\');
- if(arr.length == Number(WSH.Arguments(1))+1){
- WSH.echo(it);
- }
- }
复制代码
set "mainfolder=C:\Users\Administrator\Desktop\aaaaaaaaa\"(后面必须带斜杠,不然就会把根目录下所有文件打包,开始就是这样以为这代码不能用。)
level=3(这里设置文件夹层数)
作者: went 时间: 2021-1-23 15:27
回复 5# 7803181 - @echo off & cd /d "%~dp0"
- setlocal enabledelayedexpansion
- set "rarPath=C:\Program Files\WinRAR"
- set "path=%path%;%rarPath%"
- set "dirList="
- for /r /d %%i in (*) do (
- pushd "%%i"
- for /l %%a in (1,1,2) do cd ..
- if "!cd!\"=="%~dp0" (
- echo pack %%i
- pushd "%%i"
- set "dirList="
- for /d %%i in (*) do call set "dirList=!dirList! "%%i""
- for %%i in ("!cd!") do rar a "%%~nxi.rar" !dirList!
- popd
- echo ------------------
- )
- popd
- )
- pause&exit
复制代码
作者: 7803181 时间: 2021-1-23 15:29
回复 8# went
感谢,你这也可以用,
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |