makecab - create a cab file from all the files in a folder

不羁的心 提交于 2019-11-29 23:12:09
Nacht

I've finally created a script that can actually do this properly (with powershell)

It doesn't use WSPBuilder as I'm often contracted out and it's inconvenient to download new software/extra files. This works OOTB.

function compress-directory([string]$dir, [string]$output)
{
    $ddf = ".OPTION EXPLICIT
.Set CabinetNameTemplate=$output
.Set DiskDirectory1=.
.Set CompressionType=MSZIP
.Set Cabinet=on
.Set Compress=on
.Set CabinetFileCountThreshold=0
.Set FolderFileCountThreshold=0
.Set FolderSizeThreshold=0
.Set MaxCabinetSize=0
.Set MaxDiskFileCount=0
.Set MaxDiskSize=0
"
    $dirfullname = (get-item $dir).fullname
    $ddfpath = ($env:TEMP+"\temp.ddf")
    $ddf += (ls -recurse $dir | where { !$_.PSIsContainer } | select -ExpandProperty FullName | foreach { '"' + $_ + '" "' + ($_ | Split-Path -Leaf) + '"' }) -join "`r`n"
    $ddf
    $ddf | Out-File -Encoding UTF8 $ddfpath
    makecab.exe /F $ddfpath
    rm $ddfpath
    rm setup.inf
    rm setup.rpt
}

please let me know if i'm doing something wrong and/or could be better.

for reference:

http://www.pseale.com/blog/StrongOpinionSayNoToMAKECABEXE.aspx

NOTE: Change made by Jerry Cote, see edit notes

npocmaka

/d switch cannot be used for files:

@echo off
dir /s /b /a-d >files.txt
makecab /d "CabinetName1=test.cab" /f files.txt
del /q /f files.txt

More info

EDIT here can be found a script that preserves the whole directory structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!