Creating Batch File to Recompress MS Cabinet Files


  1. Posts : 16
    Windows 10 Pro x64, 7 Pro x64
       #1

    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 Computer


  2. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #2

    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 Computer


  3. Posts : 16
    Windows 10 Pro x64, 7 Pro x64
    Thread Starter
       #3

    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 Computer


  4. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #4

    meeshu said:
    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.

    meeshu said:
    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 Computer


  5. Posts : 16
    Windows 10 Pro x64, 7 Pro x64
    Thread Starter
       #5

    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.
    Creating Batch File to Recompress MS Cabinet Files Attached Files
      My Computer


  6. Posts : 721
    Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan
       #6

    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 Computer


 

  Related Discussions
Our Sites
Site Links
About 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 03:29.
Find Us