• 0

Unzip and unrar first file in archives and give name of file


Question

I have a bunch of manga all in rar or zip files. What I want to do is extract the first file from all the archives give them the name of the file they came from (ex. foo.zip -> foo.jpg) then I can use a visipics to find the duplicates. Anyone have any ideas how to do this effectively. Also some of the file names are in japanese so handling of that would be nice, but unnecessary.

Link to comment
Share on other sites

17 answers to this question

Recommended Posts

  • 0
I have a bunch of manga all in rar or zip files. What I want to do is extract the first file from all the archives give them the name of the file they came from (ex. foo.zip -> foo.jpg) then I can use a visipics to find the duplicates. Anyone have any ideas how to do this effectively. Also some of the file names are in japanese so handling of that would be nice, but unnecessary.

You'll need a custom application written to do that. WinRAR's unrar.exe has no commands allowing you to do this.

Link to comment
Share on other sites

  • 0
You'll need a custom application written to do that. WinRAR's unrar.exe has no commands allowing you to do this.

I wouldnt think I could do this out of the box but I think its doable via batch file. I know I could do this via bash script in linux (well with help I could do it anyway), but Im using windows 7 on my computer and would like a native way of doing it.

Link to comment
Share on other sites

  • 0
I wouldnt think I could do this out of the box but I think its doable via batch file. I know I could do this via bash script in linux (well with help I could do it anyway), but Im using windows 7 on my computer and would like a native way of doing it.

If you want to do it with bash scripting, you can always get it on Cygwin [link].

It's kind of an overkill for this, but hey, if you're more comfy working in linux, it's always worth it to grab cygwin.

Sorry for not helping with the original problem :p

Link to comment
Share on other sites

  • 0
If you want to do it with bash scripting, you can always get it on Cygwin [link].

It's kind of an overkill for this, but hey, if you're more comfy working in linux, it's always worth it to grab cygwin.

Sorry for not helping with the original problem :p

I'd rather stay away from cygwin if possible. I figure there have to be other people who obsessively collect manga who cant speak japanese who would like it too.

Link to comment
Share on other sites

  • 0
I'd rather stay away from cygwin if possible. I figure there have to be other people who obsessively collect manga who cant speak japanese who would like it too.

Heh, yeah. I just tested unrar.exe and 7z.exe's command line switches, and i really don't see an easy way out of this..

Link to comment
Share on other sites

  • 0

I'm thinking using a vbscript to have the files list to a textfile alphabetically, extract the first file in the file, rename to name of original archive file and continue in a loop until the end of the directory. I need to test the extractors to see if I can do it, which I know I can do with at least unrar but Ill look tomorrow morning.

Link to comment
Share on other sites

  • 0
I'm thinking using a vbscript to have the files list to a textfile alphabetically, extract the first file in the file, rename to name of original archive file and continue in a loop until the end of the directory. I need to test the extractors to see if I can do it, which I know I can do with at least unrar but Ill look tomorrow morning.

Well, looks like Unrar doesn't like zip files.. Both in its unrar.exe or unrar.dll form...

Link to comment
Share on other sites

  • 0

You can use the WinRAR GUI to search archives. If the first file has the same name in each archive, then you can extract the pictures to a sub-folder with the same name as the archive. This will be good enough to identify duplicates in Visipics.

When extracting, be sure to check Extract archives to subfolders.

post-57213-1257724561.png

Link to comment
Share on other sites

  • 0
You can use the WinRAR GUI to search archives. If the first file has the same name in each archive, then you can extract the pictures to a sub-folder with the same name as the archive. This will be good enough to identify duplicates in Visipics.

When extracting, be sure to check Extract archives to subfolders.

post-57213-1257724561.png

Yeah that's one way I can do it, but I'm thinking of torrenting the manga so the files with names of the archive would serve a second purpose of a preview. At the very least I can do that for myself to get rid of the duplicates.

Link to comment
Share on other sites

  • 0
I'm thinking using a vbscript to have the files list to a textfile alphabetically, extract the first file in the file, rename to name of original archive file and continue in a loop until the end of the directory. I need to test the extractors to see if I can do it, which I know I can do with at least unrar but Ill look tomorrow morning.

That should be do-able with unrar.exe.

Link to comment
Share on other sites

  • 0
Yeah that's one way I can do it, but I'm thinking of torrenting the manga so the files with names of the archive would serve a second purpose of a preview. At the very least I can do that for myself to get rid of the duplicates.

http://www.den4b.com/downloads.php?project=ReNamer

  • Add Rule
  • Replace
  • Find: *
  • Replace: :File_FolderName:
  • Check Interpret ? * [ ] as wildcards

This will rename the files to the folder name, thereby giving them the archive name.

post-57213-1257734871.png

Link to comment
Share on other sites

  • 0

Well, I was bored today and found a .NET library that encapsulates 7z.dll. So I spent a couple minutes to check it out, and it looks really cool (and also handles a variety of archive formats, unlike unrar.exe/dll that only handles RARs).

The code to do what the OP wants (i hope) is available here:

http://code.google.com/p/7zimageextractor/

There's a compiled version in the downloads section. Mind you, I never spent much time in it. It has minimum error checking and no GUI. All settings are read from the .config file.

Link to comment
Share on other sites

  • 0

Here is my 1.0 version of the vbs, it assumes you have 7za.exe and unrar.exe in the folder with the script. This can probably be made better but I didnt spend much time on it. If you want improve on it, if you do you have to post what you changed.

Option Explicit

' Flags for the options parameter
Const BIF_returnonlyfsdirs   = &H0001
Const BIF_dontgobelowdomain  = &H0002
Const BIF_statustext		 = &H0004
Const BIF_returnfsancestors  = &H0008
Const BIF_editbox			= &H0010
Const BIF_validate		   = &H0020
Const BIF_browseforcomputer  = &H1000
Const BIF_browseforprinter   = &H2000
Const BIF_browseincludefiles = &H4000
Const OverwriteExisting = True


Dim folder, objFSO, objFolder, colFiles, objFile, objExtension, objShell, objLog, sText, sCurPath, sTempPath, sFirstImg

sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
sTempPath = sCurPath & "\temp"
'WScript.Echo sTempPath

folder = BrowseForFolder("Select a folder to get covers from", BIF_returnonlyfsdirs, "")
If folder = "-5" Then 
	WScript.Echo "Not possible to select files in root folder"
	WScript.Quit
Else
	If folder = "-1" Then 
		WScript.Echo "No object selected; Cancel clicked"
		WScript.Quit
	Else
		'Folder path from browsforfolder
		'WScript.Echo "Object: ", folder
	End If
End If 

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(folder)

Set objShell = CreateObject("WScript.Shell")

Set colFiles = objFolder.Files
For Each objFile in colFiles
	'Wscript.Echo objFile.Name
	objExtension = lcase(objFile)
	'Wscript.Echo objExtension
	'Wscript.Echo right(objExtension, 3)
	If right(lcase(objExtension), 3) = "zip" or right(lcase(objExtension), 3) = "cbz" then
		If Not objFSO.FolderExists(sTempPath) Then
			objFSO.CreateFolder(sTempPath)
		End If
		'Wscript.Echo objFile
		'list contents of archive
		objShell.Run "%comspec% /c" & sCurPath &  "\7za.exe e " & """" & objfile & """ -o""" & sTempPath & """", 7, true
		'WScript.Sleep 10000
		objShell.Run "%comspec% /c dir /B " & sTempPath & "  > list.log", 7, true
		Set objLog = objFSO.OpenTextFile("list.log", 1)
			Do While Not objLog.AtEndOfStream
				sText = objLog.ReadLine
				If right(lcase(sText),3) = "jpg" or right(lcase(sText),3) = "png" or right(lcase(sText),3) = "gif" Then
					sFirstImg = sTempPath & "\" & sText
					sText = objFile.Name & "." & right(sText,3)
					'WScript.Echo sFirstImg
					'msgbox(sFirstImg)
					'MsgBox(sCurPath & "\" & sText)
					objFSO.CopyFile sFirstImg, objFolder & "\" & sText, true
					Exit Do
				End If
			Loop
		objLog.Close
		'msgBox("Wait here")
		objFSO.DeleteFile(sCurPath & "/" & "list.log")
		objFSO.DeleteFolder(sTempPath)
	ElseIf  right(lcase(objExtension), 3) = "rar" or right(lcase(objExtension), 3) = "cbr" then
		If Not objFSO.FolderExists(sTempPath) Then
			objFSO.CreateFolder(sTempPath)
		End If
		'Wscript.Echo objFile
		'list contents of archive
		objShell.Run "%comspec% /c cd temp & " & sCurPath &  "\unrar.exe e " & """" & objfile & """", 7, true
		'WScript.Sleep 10000
		objShell.Run "%comspec% /c dir /B " & sTempPath & "  > list.log", 7, true
		Set objLog = objFSO.OpenTextFile("list.log", 1)
			Do While Not objLog.AtEndOfStream
				sText = objLog.ReadLine
				If right(lcase(sText),3) = "jpg" or right(lcase(sText),3) = "png" or right(lcase(sText),3) = "gif" Then
					sFirstImg = sTempPath & "\" & sText
					sText = objFile.Name & "." & right(sText,3)
					'WScript.Echo sFirstImg
					'msgbox(sFirstImg)
					'MsgBox(sCurPath & "\" & sText)
					objFSO.CopyFile sFirstImg, objFolder & "\" & sText, true
					Exit Do
				End If
			Loop
		objLog.Close
		'msgBox("Wait here")
		objFSO.DeleteFile(sCurPath & "/" & "list.log")
		objFSO.DeleteFolder(sTempPath)
	End If
Next

WScript.Echo "Script finished"
WScript.Quit

' Using the shell's BrowseForFolder method to
' return the full path to the selected object
' title = Text shown in the dialog box
' flag = One of the values for controlling the 
'		BrowseForFolder behavior
' dir = Preselected directory (can be "")
Function BrowseForFolder(title, flag, dir)
	On Error Resume Next

	Dim oShell, oItem, tmp

	' Create WshShell object.
	Set oShell = WScript.CreateObject("Shell.Application")

	' Invoke Browse For Folder dialog box.
	Set oItem = oShell.BrowseForFolder(&H0, title, flag, dir)
	If Err.Number <> 0 Then
		If Err.Number = 5 Then
			BrowseForFolder= "-5"
			Err.Clear
			Set oShell = Nothing
			Set oItem = Nothing
			Exit Function
		End If
	End If

	' Now we try to retrieve the full path.
	BrowseForFolder = oItem.ParentFolder.ParseName(oItem.Title).Path

	' Handling: Cancel button and selecting a drive
	If Err<> 0 Then
		If Err.Number = 424 Then		   ' Handle Cancel button.
			BrowseForFolder = "-1"
		Else
			Err.Clear
			' Handle situation in which user selects a drive.
			' Extract drive letter from the title--first search
			' for a colon (:).
			tmp = InStr(1, oItem.Title, ":")
			If tmp > 0 Then		   ' A : is found; use two 
									  ' characters and add \.
				BrowseForFolder = Mid(oItem.Title, (tmp - 1), 2) & "\"
			End If
		End If
	End If

	Set oShell = Nothing
	Set oItem = Nothing
	On Error GoTo 0
End Function

'*** End

Link to comment
Share on other sites

  • 0

There doesn't seem to be anyway to extract a single file from the archives so I have to do each archive in its entirety. I also noticed that windows may try to create thumbnails for the images in the temp folder so I would suggest that you create a shortcut for the script and start it that way.

Link to comment
Share on other sites

  • 0

Apparently there is also a problem when the archives have unicode files in them. When > list.log encounters it you get different entries in the log then the files. Anyone have anyway to pipe out unicode?

Link to comment
Share on other sites

  • 0

Version 1.2

Removed the need for unrar, just using 7z.exe.

Added more file types

Fixed issue where the temp folder wouldn't delete due to a read only file in it.

Still need to fix

If archive has a file with the same name in a folder, I cant seem to get 7z to run the command with the flag to overwrite

support for unicode files in archives

skiping archives that already have the covers

Option Explicit

' Flags for the options parameter
Const BIF_returnonlyfsdirs   = &H0001
Const BIF_dontgobelowdomain  = &H0002
Const BIF_statustext		 = &H0004
Const BIF_returnfsancestors  = &H0008
Const BIF_editbox			= &H0010
Const BIF_validate		   = &H0020
Const BIF_browseforcomputer  = &H1000
Const BIF_browseforprinter   = &H2000
Const BIF_browseincludefiles = &H4000
Const OverwriteExisting = True


Dim folder, objFSO, objFolder, colFiles, objFile, objExtension, objShell, objLog, sText, sCurPath, sTempPath, sFirstImg

sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
sTempPath = sCurPath & "\temp"
'WScript.Echo sTempPath

folder = BrowseForFolder("Select a folder to get covers from", BIF_returnonlyfsdirs, "")
If folder = "-5" Then 
	WScript.Echo "Not possible to select files in root folder"
	WScript.Quit
Else
	If folder = "-1" Then 
		WScript.Echo "No object selected; Cancel clicked"
		WScript.Quit
	Else
		'Folder path from browsforfolder
		'WScript.Echo "Object: ", folder
	End If
End If 

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(folder)

Set objShell = CreateObject("WScript.Shell")

Set colFiles = objFolder.Files
For Each objFile in colFiles
	'Wscript.Echo objFile.Name
	objExtension = lcase(objFile)
	'Wscript.Echo objExtension
	'Wscript.Echo right(objExtension, 3)
	If right(lcase(objExtension), 3) = "zip" or right(lcase(objExtension), 3) = "cbz" or right(lcase(objExtension), 3) = "cb7" or right(lcase(objExtension), 3) = "7z" or right(lcase(objExtension), 3) = "lzh" or right(lcase(objExtension), 3) = "rar" or right(lcase(objExtension), 3) = "cbr" or right(lcase(objExtension), 3) = "tar" or right(lcase(objExtension), 3) = "cbt" then
		If Not objFSO.FolderExists(sTempPath) Then
			objFSO.CreateFolder(sTempPath)
		End If
		'Wscript.Echo objFile
		'list contents of archive
		objShell.Run "%comspec% /c" & sCurPath &  "\7z.exe e " & """" & objfile & """ -o""" & sTempPath & """", 7, true
		'WScript.Sleep 10000
		objShell.Run "%comspec% /c dir /B " & sTempPath & "  > list.log", 7, true
		'dont think its doing anything
		'objShell.Run "cmd /u /c dir /B " & sTempPath & "  > list.log", 7, true
		'log file is now unicode but opentext isn't
		Set objLog = objFSO.OpenTextFile("list.log", 1)
			Do While Not objLog.AtEndOfStream
				sText = objLog.ReadLine
				If right(lcase(sText),3) = "jpg" or right(lcase(sText),3) = "png" or right(lcase(sText),3) = "gif" or right(lcase(sText),3) = "tif" Then
					sFirstImg = sTempPath & "\" & sText
					sText = objFile.Name & "." & right(sText,3)
					'WScript.Echo sFirstImg
					'MsgBox(objFolder & "\" & sText)
					If objFSO.FileExists (sFirstImg) then
						objFSO.CopyFile sFirstImg, objFolder & "\" & sText, true
					Else
						'WScript.Echo sFirstImg
					End If
					Exit Do
				End If
			Loop
		objLog.Close
		'msgBox("Wait here")
		objFSO.DeleteFile(sCurPath & "/" & "list.log")
		objFSO.DeleteFolder sTempPath, 1
	End If
Next

WScript.Echo "Script finished"
WScript.Quit

' Using the shell's BrowseForFolder method to
' return the full path to the selected object
' title = Text shown in the dialog box
' flag = One of the values for controlling the 
'		BrowseForFolder behavior
' dir = Preselected directory (can be "")
Function BrowseForFolder(title, flag, dir)
	On Error Resume Next

	Dim oShell, oItem, tmp

	' Create WshShell object.
	Set oShell = WScript.CreateObject("Shell.Application")

	' Invoke Browse For Folder dialog box.
	Set oItem = oShell.BrowseForFolder(&H0, title, flag, dir)
	If Err.Number <> 0 Then
		If Err.Number = 5 Then
			BrowseForFolder= "-5"
			Err.Clear
			Set oShell = Nothing
			Set oItem = Nothing
			Exit Function
		End If
	End If

	' Now we try to retrieve the full path.
	BrowseForFolder = oItem.ParentFolder.ParseName(oItem.Title).Path

	' Handling: Cancel button and selecting a drive
	If Err<> 0 Then
		If Err.Number = 424 Then		   ' Handle Cancel button.
			BrowseForFolder = "-1"
		Else
			Err.Clear
			' Handle situation in which user selects a drive.
			' Extract drive letter from the title--first search
			' for a colon (:).
			tmp = InStr(1, oItem.Title, ":")
			If tmp > 0 Then		   ' A : is found; use two 
									  ' characters and add \.
				BrowseForFolder = Mid(oItem.Title, (tmp - 1), 2) & "\"
			End If
		End If
	End If

	Set oShell = Nothing
	Set oItem = Nothing
	On Error GoTo 0
End Function

'*** End

Link to comment
Share on other sites

  • 0

1.3

Got it to overwrite, two issues left

Option Explicit

' Flags for the options parameter
Const BIF_returnonlyfsdirs   = &H0001
Const BIF_dontgobelowdomain  = &H0002
Const BIF_statustext		 = &H0004
Const BIF_returnfsancestors  = &H0008
Const BIF_editbox			= &H0010
Const BIF_validate		   = &H0020
Const BIF_browseforcomputer  = &H1000
Const BIF_browseforprinter   = &H2000
Const BIF_browseincludefiles = &H4000
Const OverwriteExisting = True


Dim folder, objFSO, objFolder, colFiles, objFile, objExtension, objShell, objLog, sText, sCurPath, sTempPath, sFirstImg

sCurPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
sTempPath = sCurPath & "\temp"
'WScript.Echo sTempPath

folder = BrowseForFolder("Select a folder to get covers from", BIF_returnonlyfsdirs, "")
If folder = "-5" Then 
	WScript.Echo "Not possible to select files in root folder"
	WScript.Quit
Else
	If folder = "-1" Then 
		WScript.Echo "No object selected; Cancel clicked"
		WScript.Quit
	Else
		'Folder path from browsforfolder
		'WScript.Echo "Object: ", folder
	End If
End If 

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(folder)

Set objShell = CreateObject("WScript.Shell")

Set colFiles = objFolder.Files
For Each objFile in colFiles
	'Wscript.Echo objFile.Name
	objExtension = lcase(objFile)
	'Wscript.Echo objExtension
	'Wscript.Echo right(objExtension, 3)
	If right(lcase(objExtension), 3) = "zip" or right(lcase(objExtension), 3) = "cbz" or right(lcase(objExtension), 3) = "cb7" or right(lcase(objExtension), 3) = "7z" or right(lcase(objExtension), 3) = "lzh" or right(lcase(objExtension), 3) = "rar" or right(lcase(objExtension), 3) = "cbr" or right(lcase(objExtension), 3) = "tar" or right(lcase(objExtension), 3) = "cbt" then
		If Not objFSO.FolderExists(sTempPath) Then
			objFSO.CreateFolder(sTempPath)
		End If
		'Wscript.Echo objFile
		'list extract archive
		objShell.Run "%comspec% /c" & sCurPath &  "\7z.exe e " & """" & objfile & """ -o""" & sTempPath & """ -y", 7, true
		objShell.Run "%comspec% /c dir /B " & sTempPath & "  > list.log", 7, true
		'dont think its doing anything
		'objShell.Run "cmd /u /c dir /B " & sTempPath & "  > list.log", 7, true
		'log file is now unicode but opentext isn't
		Set objLog = objFSO.OpenTextFile("list.log", 1)
			Do While Not objLog.AtEndOfStream
				sText = objLog.ReadLine
				If right(lcase(sText),3) = "jpg" or right(lcase(sText),3) = "png" or right(lcase(sText),3) = "gif" or right(lcase(sText),3) = "tif" Then
					sFirstImg = sTempPath & "\" & sText
					sText = objFile.Name & "." & right(sText,3)
					'WScript.Echo sFirstImg
					'MsgBox(objFolder & "\" & sText)
					If objFSO.FileExists (sFirstImg) then
						objFSO.CopyFile sFirstImg, objFolder & "\" & sText, true
					Else
						'WScript.Echo sFirstImg
					End If
					Exit Do
				End If
			Loop
		objLog.Close
		'msgBox("Wait here")
		objFSO.DeleteFile(sCurPath & "/" & "list.log")
		objFSO.DeleteFolder sTempPath, 1
	End If
Next

WScript.Echo "Script finished"
WScript.Quit

' Using the shell's BrowseForFolder method to
' return the full path to the selected object
' title = Text shown in the dialog box
' flag = One of the values for controlling the 
'		BrowseForFolder behavior
' dir = Preselected directory (can be "")
Function BrowseForFolder(title, flag, dir)
	On Error Resume Next

	Dim oShell, oItem, tmp

	' Create WshShell object.
	Set oShell = WScript.CreateObject("Shell.Application")

	' Invoke Browse For Folder dialog box.
	Set oItem = oShell.BrowseForFolder(&H0, title, flag, dir)
	If Err.Number <> 0 Then
		If Err.Number = 5 Then
			BrowseForFolder= "-5"
			Err.Clear
			Set oShell = Nothing
			Set oItem = Nothing
			Exit Function
		End If
	End If

	' Now we try to retrieve the full path.
	BrowseForFolder = oItem.ParentFolder.ParseName(oItem.Title).Path

	' Handling: Cancel button and selecting a drive
	If Err<> 0 Then
		If Err.Number = 424 Then		   ' Handle Cancel button.
			BrowseForFolder = "-1"
		Else
			Err.Clear
			' Handle situation in which user selects a drive.
			' Extract drive letter from the title--first search
			' for a colon (:).
			tmp = InStr(1, oItem.Title, ":")
			If tmp > 0 Then		   ' A : is found; use two 
									  ' characters and add \.
				BrowseForFolder = Mid(oItem.Title, (tmp - 1), 2) & "\"
			End If
		End If
	End If

	Set oShell = Nothing
	Set oItem = Nothing
	On Error GoTo 0
End Function

'*** End

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.