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

2

27

I have bunch of files in a directory. I tried following makecab but it does not include all the files in a folder into the cab file.

makecab /d "C:\Users\crtorres\Documents\- SouthPacific Project 2014\- Projects\Sales Doc Center\New Region" test.cab

The following works but the cab file only has the manifest file. makecab manifest.xml test.cab

Psychiatry answered 16/10, 2013 at 18:52 Comment(0)
S
34

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

Schoolmistress answered 10/7, 2014 at 5:17 Comment(8)
This is beautiful, but, usage: Open PowerShell; Paste this function into PowerShell; Hit Enter; compress-directory .\some-dir-to-compress .\filename.cabElgin
Just some usage issues to be aware of if anyone runs across this older post. Destination will not take a literal path. You can push / pop a location as a workaround. command would look something like "compress-directory c:\temp .\temp.cab" <br> Additionally cab files have issues when going above 2 gigabytes - 512bytes. As an additional option, Compression type LZX can be set as an alternative.Carver
Does this script compresses the directory itself or everything under the directory? In other words, does it compress dir/ or dir/*?Vasilikivasilis
@Vasilikivasilis try it and see. It appears to do dir/*. Also why has no one told me that i have a bug which removes the first character of every filename! fixed now.Schoolmistress
@Elgin Also, you seem to need to press Enter twice after pasting the functionVasilikivasilis
i think the script adds a prefix '/' to the files' name.. is it only whos getting it?Queensland
This didn't work for me if files in different folders have the same name, so I ended up changing it to '"' + $_ + '" "' + $_.Substring($dir.Length + 1) + '"' } instead. (So path is contained in name)Microcircuit
If you get (FCIFlushCabinet)Failure creating or writing cabinet file: <unknown C run-time error>, this is because the target is in the wrong syntax (drive:\folder\file.cab) instead of the correct syntax (.\file.cab).Coup
L
21

/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

Labuan answered 7/11, 2013 at 14:4 Comment(2)
In some cases file path and name of "files.txt" is added to "files.txt" as well. Then files.txt will be in the test.cab, too.Termless
/d "MaxDiskSize=0" can be added to makecab if you only want to create a single .cab fileCommode

© 2022 - 2024 — McMap. All rights reserved.