Move files to ZIP while keeping its structure

English support forum

Moderators: white, Hacker, petermad, Stefan2

User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Move files to ZIP while keeping its structure

Post by *Peter »

I have a deep folder which contains different files with extension XYZ ("test1.xyz"). Now I want to

a) move every XYZ in a single ZIP with same name ("test1.xyz.zip") and
b) keep every ZIP in the source folder

To solve a) is simple, but for b) I have no idea: Ctrl+B or "Search for XYZ" does not keep it in same folder..
TC 10.xx / #266191
Win 10 x64
Ralph
Junior Member
Junior Member
Posts: 97
Joined: 2004-08-21, 14:58 UTC
Location: USA

Re: Move files to ZIP while keeping its structure

Post by *Ralph »

The method in this post answers the wrong question. This post gives a method for zipping files in place, if the files have been discovered by searching without navigation to the files.

If I understand correctly, you want the following:
. A separate zip file should be created for each original file.
. Each zip file should be created in the original file's sub-folder.
. The sub-folders should be discovered automatically by the search. That is, you do not want to write the specific sub-folders into the process or navigate to the specific sub-folders.

If the problem statement above is correct, I would use the following method:
. Install the MakeBat plugin for Total Commander, from https://www.ghisler.com/plugins.htm or specifically, https://plugins.ghisler.com/plugins/makebat.zip.
. Have available a zip program that can run in "batch" mode (as opposed to interactive mode). The zip program will be called by a .bat file running in a Command Prompt (DOS) window. I use zip.exe from Info-zip, but other zip programs should work too.
. Select a target folder.
. In the source folder, search and feed to listbox. (Branch View will not pass the original files' sub-folders to the Pack process.)
. Open the Pack dialog box. At the bottom of the Packer section, click on the drop-down list of filetypes to be created, and select BAT. If you selected more than one file in the listbox, then by default, the bat file will be named pack.bat. Click on OK.
. The Batch File Creator dialog box will appear. There, enter a string like the following:
zip -j "%P%N.zip" "%P%N"
The -j parameter avoids putting the sub-folder structure into the zip file. By default, Info-zip's zip.exe will include the sub-folder structure in the zip file. Click on OK to create the bat file.
. Open a Command Prompt window so you will be able to review any error messages. Run the bat file.
Last edited by Ralph on 2022-12-09, 21:14 UTC, edited 1 time in total.
User avatar
Usher
Power Member
Power Member
Posts: 1675
Joined: 2011-03-11, 10:11 UTC

Re: Move files to ZIP while keeping its structure

Post by *Usher »

2Peter
You don't need any special tools. Just read the following topic, please:
* How to make TC to pack a file into the same directory?
viewtopic.php?t=67249
Andrzej P. Wozniak
Polish subforum moderator
Ralph
Junior Member
Junior Member
Posts: 97
Joined: 2004-08-21, 14:58 UTC
Location: USA

Re: Move files to ZIP while keeping its structure

Post by *Ralph »

Thanks, Usher. I misunderstood Peter's question. I thought he wanted to zip files in multiple arbitrary sub-folders by using searching without navigation. I do such processing frequently.
User avatar
petermad
Power Member
Power Member
Posts: 14793
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Move files to ZIP while keeping its structure

Post by *petermad »

2Ralph
I thought he wanted to zip files in multiple arbitrary sub-folders by using searching without navigation. I do such processing frequently.
That is what I thougth too - so maybe you didn't misunderstand it.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Re: Move files to ZIP while keeping its structure

Post by *Peter »

Good morning
Ralph wrote: 2022-12-09, 21:00 UTC ...I thought he wanted to zip files in multiple arbitrary sub-folders by using searching without navigation. I do such processing frequently.
Yes, that's right.

As far as I see now both hints from @Ralph and @Usher leads to workarounds with tools like makebat / nircmd / AHk. The function Ctrl+Alt+Shift+F5 does nothing on my PC; maybe I changed some settings long time ago.

So I think that I will take a look at AHK.
TC 10.xx / #266191
Win 10 x64
User avatar
Hacker
Moderator
Moderator
Posts: 13064
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: Move files to ZIP while keeping its structure

Post by *Hacker »

Peter,
A short solution in AHK:

Code: Select all

RootDir := "C:\My\Root\Dir\"
FileName := "test1.xyz"

Loop, Files, % RootDir . FileName, FR
{
	Zip(A_LoopFileLongPath, A_LoopFileLongPath . ".zip")
;	FileRecycle, % A_LoopFileLongPath
;	FileDelete, % A_LoopFileLongPath
}
ExitApp

Zip(sDir, sZip)
{
	fso := ComObjCreate("Scripting.FileSystemObject")
	If !fso.FileExists(sZip)
	{
		VarSetCapacity(PKZip,44,0), StrPut("PK" Chr(5) Chr(6), &PKZip, "UTF-16")
		bstr := DllCall("oleaut32\SysAllocStringByteLen", "Str", PKZip, "UInt", 44, "Ptr")
		fso.CreateTextFile(sZip).Write(ComObjArg(8,bstr))
	}
	pZip := ComObjCreate("Shell.Application").Namespace(sZip)
	pZip.CopyHere(sDir, 4|16)
	While (pZip.Items().Count != 1)
		Sleep, 200
}
I'd suggest using as is and if all is OK, delete the test1.xyz files manually afterwards, but you can also uncomment the FileRecycle (to move into Recycle bin) or FileDelete (to delete permanently) lines if you wish to try at your own risk.

HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
User avatar
petermad
Power Member
Power Member
Posts: 14793
Joined: 2003-02-05, 20:24 UTC
Location: Denmark
Contact:

Re: Move files to ZIP while keeping its structure

Post by *petermad »

2Peter
The function Ctrl+Alt+Shift+F5 does nothing on my PC
You have to add some lines to wincmd.ini for it to work: viewtopic.php?p=382228#p382228
But it is not the solution to your problem.
License #524 (1994)
Danish Total Commander Translator
TC 11.03 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1371a
TC 3.50 on Android 6 & 13
Try: TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Re: Move files to ZIP while keeping its structure

Post by *Peter »

Thanks guys!

@Hacker
your code is rather sophisticated for my level of knowledge; usually my code looks like the hand-axe of the Flintstones. But I will try to learn from it ;-)
TC 10.xx / #266191
Win 10 x64
User avatar
Hacker
Moderator
Moderator
Posts: 13064
Joined: 2003-02-06, 14:56 UTC
Location: Bratislava, Slovakia

Re: Move files to ZIP while keeping its structure

Post by *Hacker »

Peter,
Well, my code is this part:

Code: Select all

RootDir := "C:\My\Root\Dir\"
FileName := "test1.xyz"

Loop, Files, % RootDir . FileName, FR
{
	Zip(A_LoopFileLongPath, A_LoopFileLongPath . ".zip")
;	FileRecycle, % A_LoopFileLongPath
;	FileDelete, % A_LoopFileLongPath
}
ExitApp
The rest is copy/pasted from the AHK forum :D

If you don't need the deletion after zipping, it can be easily reduced to (keep the Zip() function, though):

Code: Select all

Loop, Files, C:\My\Root\Dir\test1.xyz, FR
	Zip(A_LoopFileLongPath, A_LoopFileLongPath . ".zip")
ExitApp
where you just edit the path and file name to your liking.

Feel free to ask if you have any questions. RootDir is the folder where you wish to start to search for the test1.xyz files, and FileName is the name of the file to look for in each subfolder.

HTH
Roman
Mal angenommen, du drückst Strg+F, wählst die FTP-Verbindung (mit gespeichertem Passwort), klickst aber nicht auf Verbinden, sondern fällst tot um.
Fla$her
Power Member
Power Member
Posts: 2296
Joined: 2020-01-18, 04:03 UTC

Re: Move files to ZIP while keeping its structure

Post by *Fla$her »

2Peter
Here .vbs without any external programs (drag to the toolbar and fulfill the conditions):

Code: Select all

'————————————————————————————————————————————————————————————————
' Zip packaging of each file in the folder structure by extension
' Conditions: empty start path and extension as parameter
'————————————————————————————————————————————————————————————————
Option Explicit: Dim Shell, FSO, ZIP
If WSH.Arguments.Count = 0 Then MsgBox "Specify the extension as a parameter!", 4144: WSH.Quit
Set Shell = CreateObject("Shell.Application").Windows.Item.document.Application
Set FSO   = CreateObject("Scripting.FileSystemObject")
ZIP = "PK" & Chr(5) & Chr(6) & String(18, Chr(0))
Recursion FSO.GetAbsolutePathName("")
Set Shell = Nothing: Set FSO = Nothing
MsgBox "       Done!", 262208, " Zip packaging by extension       "

Sub Recursion(Dir)
   Dim Items, File, Arch, Folder
   Set Items = Shell.NameSpace(Dir).Items
   Items.Filter 73920, "*." & WSH.Arguments(0)
   If Items.Count Then
      For Each File In Items
         Arch = Dir & "\" & File.Name & ".zip"
         With FSO.CreateTextFile(Arch, 1) .Write ZIP: .Close: End With
         Shell.NameSpace(Arch).MoVeHere File, 13332
      Next
   End If
   For Each Folder In FSO.GetFolder(Dir).SubFolders
      Recursion Folder.Path
   Next
End Sub
Last edited by Fla$her on 2022-12-13, 14:52 UTC, edited 1 time in total.
Overquoting is evil! 👎
User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Re: Move files to ZIP while keeping its structure

Post by *Peter »

Fla$her wrote: 2022-12-13, 02:53 UTC 2Peter
Here .vbs without any external programs (drag to the toolbar and fulfill the conditions): ....
Ähh - please more details or screenshots..
TC 10.xx / #266191
Win 10 x64
Fla$her
Power Member
Power Member
Posts: 2296
Joined: 2020-01-18, 04:03 UTC

Re: Move files to ZIP while keeping its structure

Post by *Fla$her »

2Peter
And what exactly is unclear? Save the code, for example, in MoveFilesToArchivesByExt.vbs (ANSI or UTF-16), drag it from the file panel to the button bar and make in the button the changes described in the conditions of the code header. Then click the button for the active panel with the source catalog.

Added a message to the code after completion.
Overquoting is evil! 👎
User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Re: Move files to ZIP while keeping its structure

Post by *Peter »

OK, now I see. I expand your description a little bit for slow guys like me:

Preparation - only once:
- Save the code, for example, in MoveFilesToArchivesByExt.vbs (ANSI or UTF-16)
- drag it from the file panel to the button bar of TC
- check the settings of the newly created button:
-- Command: the current path to the VBS
-- Parameter: add the extensions of the file you want to ZIP, e.g. DOC or TXT or LOG or ...
-- Start path: delete existing entry; the field has to be empty

Usage:
- go to the structure you want to ZIP, e.g. c:\data\mystuff
- click the button created above
- all files with the defined extension in the current folder AND in all subfolders will be moved and compressed to "<filename><fileextension>.zip" in their source folder
- a message will appear when all commands are invoked, the compression itself maybe will take a few moments longer

Thanks Fla$her,
a fine tool.
TC 10.xx / #266191
Win 10 x64
User avatar
Peter
Power Member
Power Member
Posts: 2064
Joined: 2003-11-13, 13:40 UTC
Location: Schweiz

Re: Move files to ZIP while keeping its structure

Post by *Peter »

Peter wrote: 2022-12-14, 11:49 UTC OK, now I see. I expand your description a little bit for slow guys like me:

Preparation - only once:
- Save the code, for example, in MoveFilesToArchivesByExt.vbs (ANSI or UTF-16)
- drag it from the file panel to the button bar of TC
- check the settings of the newly created button:
-- Command: the current path to the VBS
-- Parameter: add the extensions of the file you want to ZIP, e.g. DOC or TXT or LOG or ...
-- Start path: delete existing entry; the field has to be empty

Usage:
- go to the structure you want to ZIP, e.g. c:\data\mystuff
- click the button created above
- all files with the defined extension in the current folder AND in all subfolders will be moved and compressed to "<filename><fileextension>.zip" in their source folder
- a message will appear when all commands are invoked, the compression itself maybe will take a few moments longer

Thanks Fla$her,
a fine tool.
BTW:
The script fires the commands very quick, so I had a lot of parallel processes for 200-300 files. I get no error and all ZIPs, but the deletion of the source-files comes late or is skipped.

So, just for discussion: maybe a "wait until current file is done" could cause a better (and slower) execution?
TC 10.xx / #266191
Win 10 x64
Post Reply