Question

i'd like to create a small script to parse a directory after Mapfiles.zip and its contents. Currently im stucked with the multi for loops. All i wanna do is list the zip content and get the file name and insert into a version.txt next to the zip file. eg

Path = z:\sdfasdf\asdgdfg\njfds\Mapfiles.zip
Content:

2014-01-13 21:06:28 .....      8821287      1503025  9.10.25.359333.2014_01_13_21_06_28.txt
2014-01-13 21:07:32 .....      8821287      1503025  9.10.25.359333.2014_01_13_21_07_30.txt

So the final goal is to z:\sdfasdf\asdgdfg\njfds\version.txt should contains '9.10.25.359333'

@echo off
setlocal
FOR /F "tokens=* delims=" %%A in ('dir /b /s z:\sdfasdf\asdgdfg\njfds\*.zip') do (
for /f "delims=" %%a in ('dir /S /b %%A') do ( set path2=%%a 
)
set path2=%path2:Mapfiles.zip=''%
for /f "delims=" %%b in ('7z.exe l "%%A" | findstr "iGO" ') do (
set var1=%%b
set var1=%var1:~54,-3%
)
echo.%var1%
)
)

How can i do it correctly?

Was it helpful?

Solution

You need to use the var > file to print out the echo result.
Or, double to add (whitout replace) at the end of the file.

Now, using tokens & delims you actually can "split" each line, and see if match your condition with findstr.
So for example:

@echo off
for /f "tokens=1,3 delims= " %%g in ('7z.exe l -slt %file%') do (
  echo.%%g | findstr /b /c:"Path" 1>nul
  if NOT errorlevel 1 (
      echo.%%h >> list.txt
  )
)
  • l -slt Tell 7z to display "Show technical information"
  • tokens=1,3 Token 1 and 3 catch the first and third
  • delims= Delims by "space" is what we going to use here
  • findstr /b Find "Path" at the very start (/b) of "g" (first token)
  • /c:Path We look for "Path" in the first token, because is how start the line that contain the filename in 7z list.
  • errorlevel Is 1 when the string is not found, so...
  • var >> file.txt Print out to "list.txt", but only the filename (second token)
  • %%h Note we set a correlative letter for the second token, if start with g, then the next one is h

Now, you will have to handeld with posible special chars in filenames, spaces or whatever you want to filter out.

More info:

Hope it's helps.

OTHER TIPS

PowerShell can also be used for a native solution to listing zip file contents. Just replace the Test.zip in the example command to see how it works:

for /f "skip=3 delims=" %A in ('PowerShell -NoProfile -ExecutionPolicy RemoteSigned -Command "$sh = (new-object -com shell.application); $zp = ($sh.NameSpace('"Test.zip"')); $zp.Items() | Select-Object Path"') do @echo %A
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top