Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


Windows 7: Creating Batch File to Recompress MS Cabinet Files

14 Jul 2015   #1
meeshu

Windows 7 Pro x64
 
 
Creating Batch File to Recompress MS Cabinet Files

I'm trying to write a batch file that will scan/search a directory for MS Cabinet files. Then extract the contents of each cabinet file. The extracted contents are then "stored" in a cabinet file with the same file name as the original file. The new file then overwrites the old file.

Presently, the setup is -

search for cabinet files;
extract cabinet content to temporary directory;
"store" the extracted content as a cabinet file, and the cabinet file named the same as the original cabinet file;
copy the new cabinet file to original file location and overwrite the old file;
delete contents of temporary directory for receiving extracted contents of any other cabinet files;
repeat process until all cabinet files changed.

I've written a batch file as below, but on running it, it only processes some cabinet files, and get a message that file not found for other non-processed cabinet files?

The directory being tested on has approximately 150 cabinet files, but only 20 or so cabinet files are processed by the batch file!?

What is wrong with the batch file?

FOR %%A IN (*.cab) DO (cabarc x d:\sw_2014_rev\plastics\%%A C:\SW_TEMP\ | cabarc /m none n c:\sw_temp\%%A C:\SW_TEMP\*.* | XCOPY C:\SW_TEMP\%%A /v /y | DEL C:\SW_TEMP\*.* /q)


My System SpecsSystem Spec
.
15 Jul 2015   #2
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Hi Meeshu,

I don't quite understand your description of the problem, however, I can sort of read your batch snippet; note that the Cabarc command is depreciated, though, and I don't know it's syntax. Please use the Makecab command.

I've notice a few mistakes with your batch snippet too: your paths aren't double quoted, files with spaces in them will break the command line; also you seem to have misused the pipe symbol; an ampersand is instead used to run multiple commands on a line.

Try,
Code:
for %%A in (*.cab) do (
	cabarc x "d:\sw_2014_rev\plastics\%%~A" "C:\SW_TEMP\"
	cabarc /m none n "c:\sw_temp\%%~A" "C:\SW_TEMP\*.*"
	xcopy "C:\SW_TEMP\%%~A" /v /y
	del "C:\SW_TEMP\*.*" /q
)
My System SpecsSystem Spec
20 Jul 2015   #3
meeshu

Windows 7 Pro x64
 
 

Thanks for the response, and apologies for delay in replying (due to other issues taking up most of my time).

Tried the modified batch file as posted, but it didn't quite work either. Apparently no *.cab files were processed (the original *.cab files remain unchanged).

Did note a couple of things though.

I'm aware that if a directory/file path includes a space (or spaces), then double quotes (") are required to enclose those paths. However, in my original batch file, there were no spaces in the file paths, so double quotes do not appear to be necessary.

Secondly, I believe the variable %%A should stay that way. Changing the variable to %%~A midway through the process doesn't seem to make sense.

Thanks for the ampersand (&) hint regarding separating commands on a single line of code!

For reference, the following is the syntax for using cabarc -

Code:
 
Usage: CABARC [options] command cabfile [@list] [files] [dest_dir]
 
 Commands:
    L   List contents of cabinet (e.g. cabarc l test.cab)
    N   Create new cabinet (e.g. cabarc n test.cab *.c app.mak *.h)
    X   Extract file(s) from cabinet (e.g. cabarc x test.cab foo*.c)
 
 Options:
   -c   Confirm files to be operated on
   -o   When extracting, overwrite without asking for confirmation
   -m   Set compression type [LZX:<15..21>|MSZIP|NONE], (default is 
MSZIP)
   -p   Preserve path names (absolute paths not allowed)
   -P   Strip specified prefix from files when added
   -r   Recurse into subdirectories when adding files (see -p also)
   -s   Reserve space in cabinet for signing (e.g. -s 6144 reserves 6K 
bytes)
   -i   Set cabinet set ID when creating cabinets (default is 0)
   -d   Set diskette size (default is no limit/single CAB)
 
 
When creating a cabinet, the plus sign (+) may be used as a filename
to force a folder boundary; e.g. cabarc n test.cab *.c test.h + *.bmp
 
When extracting files to disk, the <dest_dir>, if provided, must end 
in a backslash; e.g. cabarc x test.cab bar*.cpp *.h d:\test\
 
The -P (strip prefix) option can be used to strip out path information 
e.g. cabarc -r -p -P myproj\ a test.cab myproj\balloon\*.*
 
The -P option can be used multiple times to strip out multiple paths
 
When creating cabinet sets using -d, the cabinet name should contain 
a single '*' character where the cabinet number will be inserted.
 
Examples:
 cabarc.exe -m LZX:21 n NEW_CAB-DATE.CAB @DATELIST.TXT
 cabarc.exe -m LZX:MSZIP n NEW_CAB-DATE.CAB FILE1.EXE FILE2.EXE FILE.TXT C:\

Basically, the format for when creating cab files the command is - cabarc n target source(s)

and when extracting cab files the command is - cabarc x source target

Still working on this batch file (using "&" to separate commands).
My System SpecsSystem Spec
.

21 Jul 2015   #4
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Quote   Quote: Originally Posted by meeshu View Post
I'm aware that if a directory/file path includes a space (or spaces), then double quotes (") are required to enclose those paths. However, in my original batch file, there were no spaces in the file paths, so double quotes do not appear to be necessary.
Don't forget about the dynamic variables. Any path that contains spaces or could potentially contain spaces should be double quoted. Double quote all paths regardless, it's good practise.

Quote   Quote: Originally Posted by meeshu View Post
Secondly, I believe the variable %%A should stay that way. Changing the variable to %%~A midway through the process doesn't seem to make sense.
Your justification here is needed. Although it is reasonable to say that the tilde in '%%~A' is useless in this instance because, practically, a For loop used in this way would never output double quotes. However explicit is better than implicit, and readability counts.

'%%A' or '%%~A' is purely your own choice. But I highly recommend you encapsulate all your paths in double quotes, always.


Thanks for posting the CabArc's syntax reference, but for my help to be efficient, it would really help if I actually had the command with me to test solutions. Would you be able to upload the CabArc command here? Or better provide a download link?

Alternatively, I encourage you just use the newer MakeCab and Expand commands to manipulate .cab files. These commands exist in all flavours of Windows 7. Using these commands instead of CabArc will make this thread more valuable, because think of the many who will come across this thread only to realise that the solution involves a command that doesn't exist on their install. Still your choice on what you decide to do though.

But before you do anything, would you first be able to clarify your question more concisely? Particularly, what should the input be and what should the output look like? I'm still a little confused with what you are trying to achieve here.
My System SpecsSystem Spec
22 Jul 2015   #5
meeshu

Windows 7 Pro x64
 
 

Thanks for the comments.

Briefly, as I'm once again working on other matters, Makecab was found to be difficult to use, and CabArc is conversely much easier to use. This is why I prefer CabArc over Makecab.

CabArc file enclosed as "CabArc.zip". Rename CabArc.zip to CabArc.exe and place the executable in either or both - "Root:\Windows\System32" and/or "Root:\Windows\SysWOW64" directories.

Basically what is wanted is for all the cabinet files within a (sub)directory to be uncompressed, then files from each cabinet file to be "stored" with the same file name (and in cabinet file format again) in the same directory as the original file (and overwriting the original file).

So ultimately all compressed cabinet files will then become stored files instead.


Attached Files
File Type: zip CabArc.zip (36.6 KB, 6 views)
My System SpecsSystem Spec
22 Jul 2015   #6
Pyprohly

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
 
 

Okay, I've pasted a script below. It may not work exactly how you want it right now, but anything can be changed. So please test the script first, by either backing up your data or creating a test environment to work in.

th-375526.bat
Code:
@echo off

:: file.bat CabFilesPath

REM For all files in each .cab file in 'CabFilesPath', extract them to the current directory, 
REM then re-compress each of the now uncompressed files to a new Cabinet file. Naming conflicts 
REM will be overwritten. Directories uncompressed will be ignored. 

REM If 'CabFilesPath' parameter is not specified, the current directory will be used instead. 
if "%~1"=="" ( set CabFilesPath=.) else ( set CabFilesPath=%1)

set temp_dir="%TEMP%\tmp_%~n0\"

set temp_dir=%TEMP_DIR:"=%
if "%TEMP_DIR:~-1%"=="\" set temp_dir=%TEMP_DIR:~0,-1%
set temp_dir="%TEMP_DIR%"

if exist "%TEMP_DIR:"=%"\* rd "%TEMP_DIR:"=%" /s /q
md "%TEMP_DIR:"=%"

pushd %CABFILESPATH%
for /f "delims=" %%I in (' dir /b *.cab ') do (
	cabarc x "%%~I" "%TEMP_DIR:"=%"\
	pushd "%TEMP_DIR:"=%"
	for /f "delims=" %%J in (' dir /a:-d /b "*" ') do (
		cabarc n "%%~nJ.cab" "%%~fJ"
	)
	popd
	xcopy "%TEMP_DIR:"=%\*.cab" "." /y
	del "%TEMP_DIR:"=%" /q
	for /f "delims=" %%K in ('dir /a:d /b "%TEMP_DIR:"=%"') do rd "%%~K"
)
popd

rd "%TEMP_DIR:"=%"
My System SpecsSystem Spec
Reply

 Creating Batch File to Recompress MS Cabinet Files




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
Creating the PPPOE internet connection using batch file
I have a PPPoE internet connetion from my ISP. Every time I want to access the internet connection I have to goto the setting and in that I have to open it and then have to dial it. This is the traditional way to log in but I wanted to make a batch file in which the PPPoE user name and password is...
Network & Sharing
Windows 7 creating blank txt file after running a batch script
I have the following batch script @echo off setlocal enableextensions set "baseName=dnshistory" set "count=0" for /f "delims=%baseName%." %%a in (
General Discussion
Creating Batch File with Robo Copy
Hi I am currently running the following to supposedely create a directory IE V:\Backup Folder\Pictures\Backup 21-05-2012 robocopy /e "c:\users\jon\pictures" "v:\Backup Folder\Pictures\Backup %date:~-10,2%-%date:~-7,2%-%date:~-4,4%" Evrything works fine except the backed up pictures folder is...
General Discussion
Help creating a batch file to auto login to a website
I have created the following batch file to automatically load and login to a site, I created it and tested it on a computer running xp, but I can't get it to work on a windows 7 machine... "@ECHO OFF TITLE Water Monitor Auto Start Up mode con: cols=42 lines=1 REM Create a time delay...
General Discussion
Help Needed Creating Batch File
Hi, I have 2 files that are in the same directory which I want to start one after the other. I have created a simple batch file like this which opens 2 files at the same time. Example Start Server.exe Start Game.exe
General Discussion
Creating a welcoming batch file...
I need help creating batch file that will do the following... I want it to echo "Welcome (insert name here)." I want it to say "How are you today, good or bad?" If G, I want it to display, "That's great to know, sir! Would you like me to open your homepage? If Y, I want it to execute cd...
Software


Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 19:33.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App