Move files to ZIP while keeping its structure
Moderators: Hacker, petermad, Stefan2, white
Move files to ZIP while keeping its structure
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..
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
Win 10 x64
Re: Move files to ZIP while keeping its structure
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.
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.
Re: Move files to ZIP while keeping its structure
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
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
Polish subforum moderator
Re: Move files to ZIP while keeping its structure
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.
Re: Move files to ZIP while keeping its structure
2Ralph
That is what I thougth too - so maybe you didn't misunderstand it.I thought he wanted to zip files in multiple arbitrary sub-folders by using searching without navigation. I do such processing frequently.
License #524 (1994)
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Re: Move files to ZIP while keeping its structure
Good morning
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.
Yes, that's right.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.
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
Win 10 x64
Re: Move files to ZIP while keeping its structure
Peter,
A short solution in AHK:
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
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
}
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.
Re: Move files to ZIP while keeping its structure
2Peter
But it is not the solution to your problem.
You have to add some lines to wincmd.ini for it to work: viewtopic.php?p=382228#p382228The function Ctrl+Alt+Shift+F5 does nothing on my PC
But it is not the solution to your problem.
License #524 (1994)
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Danish Total Commander Translator
TC 11.51 32+64bit on Win XP 32bit & Win 7, 8.1 & 10 (22H2) 64bit, 'Everything' 1.5.0.1391a
TC 3.60b4 on Android 6, 13, 14
TC Extended Menus | TC Languagebar | TC Dark Help | PHSM-Calendar
Re: Move files to ZIP while keeping its structure
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
@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
Win 10 x64
Re: Move files to ZIP while keeping its structure
Peter,
Well, my code is this part:
The rest is copy/pasted from the AHK forum 
If you don't need the deletion after zipping, it can be easily reduced to (keep the Zip() function, though):
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
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

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
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.
Re: Move files to ZIP while keeping its structure
2Peter
Here .vbs without any external programs (drag to the toolbar and fulfill the conditions):
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! 👎
Re: Move files to ZIP while keeping its structure
Ähh - please more details or screenshots..Fla$her wrote: 2022-12-13, 02:53 UTC 2Peter
Here .vbs without any external programs (drag to the toolbar and fulfill the conditions): ....
TC 10.xx / #266191
Win 10 x64
Win 10 x64
Re: Move files to ZIP while keeping its structure
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.
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! 👎
Re: Move files to ZIP while keeping its structure
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.
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
Win 10 x64
Re: Move files to ZIP while keeping its structure
BTW: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.
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
Win 10 x64