0

So, please bear with me. This is only the second batch file I've ever written and my first post anywhere on StackExchange.

I'm writing a batch file that tells a remote server to run HP Array Diagnostic Utility and pull the report back to my computer. I'm extremely excited because it's working very well, for the most part. I do have one minor issue, though. I'm trying to pull the serial number from the text file within the zip folder that comes from the remote server. When I pull the information from a test text file not inside a compressed folder, this part of the code works. It doesn't work when the text file is in the zip folder, though. Is there a way to get this to work?

Here is the portion of the code in question:

FOR /f "tokens=1-4 delims= " %%A IN ("%userprofile%\Desktop\report.zip\report.txt") DO (
 IF %%A ==Chassis (
    SET "sn=%%D"
    ECHO %sn%
 )
)

Thank you.

1

1 Answer 1

0

Solution

Despite the fact Windows displays .zip archives as compressed folders, they're not real folders at all. This means you can't directly access the compressed content until you extract it somewhere.

Windows doesn't provide a command-line utility to extract (or create, for the matter) compressed archives. With a little VBScript, you can however bypass this limitation:

REM^ &@echo off
REM^ &if not exist "%~f1" exit /b 2
REM^ &md "%temp%\%~nx1" 2>nul
REM^ &cscript /nologo /e:vbscript "%~f0" "%~f1" "%temp%\%~nx1"
REM^ &exit /b

Set sa = CreateObject("Shell.Application")
Set files = sa.NameSpace(WScript.Arguments(0)).Items
Set target = sa.NameSpace(WScript.Arguments(1))

For Each file In files
target.CopyHere file, 1556
Next

How it works

The above script is a hybrid (thanks to dbenham and jeb for the idea), and contains both regular batch and VBScript commands in a single file.

The script accepts only one parameter, which is the archive path. At first the script checks whether the specified file actually exists, and then proceeds creating a subfolder named after the archive in the built-in temporary folder.

The VBScript code is then executed. Each file in the archive gets copied (extracted) to the target folder. The option value is 1556, which corresponds to 4 + 16 + 512 + 1024 and is required to make the whole operation unattended by skipping confirmation prompts (see the link below for additional details).

Example usage

@echo off
setlocal

set archive=report.zip
call UnzipHelper.cmd "%userprofile%\Desktop\%archive%" >nul
pushd "%temp%\%archive%"

REM your code here

popd
rd /s /q "%temp%\%archive%" 2>nul

pause
endlocal & exit /b

Further reading

1
  • I've never done VBScript before. Thank you for the information and the resources. Something new to play with.
    – duzzy
    Jul 26, 2014 at 17:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .