Solved

Compressing +300 files uzing 7za to individual archives without ruining the filenames

Posted on 2004-08-08
13
370 Views
Last Modified: 2012-06-27
Hello.

I have Win Me, and I need to make a backup of more than 300 files, and to save a little space on the Cd, I would like to compress them using 7-zip. Since the program doesn´t have an option (like winrar) to compress files to individual archives, I decided to use the W32 command line driven version of the program.

After trial an error, and searching the web for some examples of .bat files, I found out that some thing like this would work:

For %%n in (*.*) do echo 7za a -tz7 -mx=7 %%.7z %%n>>c7z.bat

and then execute the c7z.bat file created. While the above works fine for the most part, it fails in something crucial for me. The filenames have the 8.3 filename format. So I end up with pretty unrecognizable filenames. I know I can use Winrar´s "Put each file to separate archive" option but on my trials, I found out that 7z compresses better than Winrar, so I have decided to use 7z as my compression selection.

I searched the web, and came up with different tools that I thought could or would help me out. Like Minitrue, the Berkeley utilities, aedit, even a tool called gpatch.exe, etc. But they either didn´t work or the man pages were really confusing or I just didn´t know what to do. So I thought about coming here to ask for a lot of help and expertise.

I just don´t want to type/cut and paste more than 300 entries because it would be time too time consuming. So I´m hoping to have something like this in the batch file:

-----------BOF------------
7za a -tz7 -mx=7 "afilename.7z" "afilename.doc"
7za a -tz7 -mx=7 "allmydocs.7z" "allmydocs.ppt"
7za a -tz7 -mx=7 "template.7z" "template.css"
7za a -tz7 -mx=7 "allprivatedata.7z" "allprivatedata.zip"
7za a -tz7 -mx=7 "all settings.7z" "all settings.dat"
7za a -tz7 -mx=7 "my portafolio.7z" "my portafolio.7z"
     e
     t
     c
7za a -tz7 -mx=7 "etc.7z" "etc.etc"
-----------EOF------------

As you can see the filenames as well as the extentions are different. Although I think the command line edition of 7z is smart enough to know that spaces are part of a given filename, I would like to add the quotes to all filanames so I know the program doesn´t stop because of an error.

The only idea I had was to put all the files I want to compress to an empty directory and then do dir *.* /ogn/b>workdata.txt so I would end up with all the files and their full filenames, and then somehow do the proper modifictions, which btw don´t seem easy to me. I think it can be done, but I have no clue about what to do.

If anyone can help, or give ideas, it will be greatly appreciatte. Needless to say there are many points involved ;-)

Thannks in advance,
Other_user

Ps1: if you need more information, please do let me know.
Ps2: sorry if I wasn´t clear enough. English isn´t my first language.
0
Comment
Question by:Other_user
  • 7
  • 2
  • 2
  • +1
13 Comments
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11746646
Maybe there is a part of the problem I do not understand, but why can't you do this:

For %%n in (*.*) do echo 7za a -tz7 -mx=7 "%%n.7z" "%%n">>c7z.bat

It gives "afilename.doc.7z" ... but that may be good if there's also a file "afilename.txt" ...

I don't use 7za so I don't know...
0
 

Author Comment

by:Other_user
ID: 11746908
Yes, I can use the For command on my files, but the filenames are getting messed up. All of my files have pretty long names, and I don´t want filenames in 8.3 filename format. Ie:

1. Thisisonebigfilename.doc
2. Another Big (Me) name.zip
3. Dont Read This.txt

Those are long names, if I do the For command, I would end up with filenames like these:

1. Thisis~1.7z
2. Anothe~1.7z
3. Dont r~1.7z ---> Though not quite sure because of the space.

The thing is that with 8.3 filenames I won´t be able to recognize most, if not all, of the files.

The filenames of all my files are all different so I don´t think there will be collisions of any type.
0
 
LVL 10

Expert Comment

by:For-Soft
ID: 11747079
I believe this is not the best way to save disk space. You see, every file takes a part of your hdd, even 1 byte in size file can take up to 32k bytes of hdd, because space is allocated in clusters. The best way to save space is to put all files in one file, even if compession is less efective.
0
 

Author Comment

by:Other_user
ID: 11748838
Yeah, I know. I am saving the files to Cd so I don´t know if what For-soft said applies as well.

I would compress the files together, but to tell you the truth 7za is really slow when I compress more than 5 Mb (slow as being totally unreliable) and the total size of all my files is +500 Mb. I haven´t even tried to compress files larger than ie 20 Mb because 7za seems to hang. The reason for that is that my pc is really slow, a Pentium II at 450 Mhz. If the files are less than 10 Mb it does the job just fine.

I don´t want to give up just yet and use Winrar´s feature, which I mentioned above because I it could be done.

Btw, I am actually not looking for debug xxx kind of solutions (I just don´t know how to use that) I would like to see if it can be done in a command prompt, or a batch file.

Could someone, please, take a look at the tools I mentioned. I don´t want to sound pushy or anything (which I actually think I have) but maybe someone can figure something out, and throw some wild ideas...

Btw, Point Value raised to 600 pts

Thanks for reading,
anewuser
0
 

Author Comment

by:Other_user
ID: 11748849
Sorry, I meant to say 500 pts. I would give even more points because this seems difficult, but apparently 500 is the highest I can give.
0
 
LVL 7

Expert Comment

by:LRHGuy
ID: 11748927
I could write a windows (delphi) program that would work like so:

    scandir *.* > c7a.bat

The c7a.bat file would then have an entry for every file:

  7za a -tz7 -mx=7 "all settings.7z" "all settings.dat"
  7za a -tz7 -mx=7 "my portafolio.7z" "my portafolio.7z"
etc...

??
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:Other_user
ID: 11749466
I wish I knew how to program. I wanted to do this on my own so bad, but I got stuck. I will wait a couple of days to see if anybody else has something to say.

If I don´t see anymore replies, I will get back to you and close this question.
0
 
LVL 10

Expert Comment

by:For-Soft
ID: 11749928
In case of CD media.
Space is assigned in 2048 bytes sectors. It means every file wastes about 1kB of space in average. So 300 files means about 300kB of wasted space.
0
 

Author Comment

by:Other_user
ID: 11766261
So I was thinking that maybe I should compress the fies (using For %%n in (*.*) do echo 7za a -tz7 -mx=7 %%.7z %%n>>c7z.bat) so I end up with all my files in 8.3 filenames, but then rename the uncompressed files (.doc, .txt, .etc) to .7z so (hopefully) every 8.3 filename has a long filename  counterpart.

Any ideas, if there are 8.3 to lfn programs out there or in a batch file.

What about For %n in (???) do call.bat type of thing?
0
 

Author Comment

by:Other_user
ID: 11909202
Hello.

I got it!

Well, kind of, and I cheated while I was on it ;-) I didn´t see any big consecuences (annoying, yes) just some files that didn´t get deleted/compressed. When I tried the batch file on my +300 files, ~22 of them were not compressed, and the temporary files I created were not deleted. So if you happen to use this, you may have to deleted some file by hand. You should also edit the created .bat file "COMPRESS.BAT", especifically the last lines.

When is it going to fail? Or when isn´t it going to compress/deleted the files? I didn´t tested fully. At first, I tested on 10 files. Then, I tested on my +300 files the outcome I mentioned before. I found out two ways to make the batch file fail (I didn´t know I used too much dots in my filenames. Bad practice...) and they are:

1. When files have two or more dots (ie: my.filename.txt)
2. When files are extensionless (ie: myfilename)


The batch files:


:::::::::::::7ZC.BAT:::::::::::::
for %%n in (*.*) do call PROCESS.BAT %%n
:::::::::::::7ZC.BAT:::::::::::::

Calling a batch file (PROCESS.BAT) for every file in the current directory...
You might want to change *.* (to something like *.doc)...


The contents of PROCESS.BAT is as follow:

:::::::::::::PROCESS.BAT:::::::::::::
set SFN=%1
dir %1% /b>TEMP.TMP
copy INIT.TXT+TEMP.TMP VARSET.BAT
call VARSET.BAT
del TEMP.TMP
del VARSET.BAT
copy /b "%LFN%" *.
dir *. /b>TEMP.TMP
del *.
copy INIT.TXT+TEMP.TMP VARSET.BAT
call VARSET.BAT
del TEMP.TMP
del VARSET.BAT
echo 7za a -t7z -mx=7 "%LFN%.7z" %SFN%>>COMPRESS.BAT
:::::::::::::PROCESS.BAT:::::::::::::


I would comment this batch file, but I´m afraid my English skills won´t be clear enough to explain it. If anyone has questions about it, I will try to anwser them the best I can...

To create init.txt, at the cmmand prompt type this:

:::::::::::::INIT.TXT:::::::::::::
copy con init.txt[ENTER]
set SFN=[CTRL+^Z][ENTER]
:::::::::::::INIT.TXT:::::::::::::


Finally, I will leave this here a while in case people has doubts. Then, and according to http://www.experts-exchange.com/help.jsp#hi70 I will ask in the Community Support for a refund.

To LRHGuy: Thanks for the offer, but since this is a MS-DOS board, I just can´t take delphi code as an anwser. I hope you understand. Thanks for the offer, though.
0
 

Author Comment

by:Other_user
ID: 11960417
Hello again.

I just noticed some errors with the batch file, and I thought to post back and correct them. Also, I did some minor "updates" to what *I* think is better batch file. It does not (it did not, either) affect the behavior/outcome of the batch file. This is just for completeness.

The way I did it is not really pretty. I, myself, think it is horrible, but I am not a programmer to do it better. It writes to hard disk too much so it should not be execute it everyday. It is more like one time thing.


Four files are needed for this to work now: 7ZC.BAT, PROCESS.BAT, CLEANUP.BAT, and INIT.TXT

We need the long filename, somehow capture it to a variable, remove its extension, and we would also need to short filename, which is what we will compress. Then we can echo all that to another batch file or console if you would like to compress right away. For that take away the word "echo" in PROCESS.BAT, but if you have many files, it will take a long time.


:::::::::::::7ZC.BAT:::::::::::::
for %%n in (%1) do call PROCESS.BAT %%n
:::::::::::::7ZC.BAT:::::::::::::

Calling PROCESS.BAT for every file and use %%n (a file) as a parameter. %1 should be something like *.txt, *.doc, etc. You have to type a set of files, otherwise it will not do anything.


::::::::::::::::PROCESS.BAT::::::::::::::::
dir %1 /b>TEMP.TMP
call CLEANUP.BAT
copy /b "%LFN%" *.>nul
dir *. /b>TEMP.TMP
del *.
call CLEANUP.BAT
echo 7za a -t7z -mx=7 "%LFN%.7z" %1>>COMPRESS.BAT
::::::::::::::::PROCESS.BAT::::::::::::::::

Listing the file "received" as a parameter (%1 takes the value of %%n) from the previous batch file to a temporary file (TEMP.TMP, which contains the long filename) Then CLEANUP.BAT is called to perform 4 different actions. 1. Create a file (VARSET.BAT) 2. Setting up a variable and its value (%LFN%) 3. Delete a file (TEMP.TMP) 4. Delete another file (VARSET.BAT)

Then it copies a file, which its name I have stored in a variable, to another file, but without the extension. Leaving me with something like this: THIS IS A TEST FILE.doc and another file (extensionless) named THIS IS A TEST FILE, and so on for every file.

Dir *. Lists all extensionless files and redirects the output (a filename) to TEMP.TMP There should only be one extensionless file at anytime. Otherwise, COMPRESS.BAT will have weird entries for input and output files. Since I do dir *. files with 2 or more dots will not be compressed with the name I actually wanted and the temporary file will not be deleted, either. So, if you decide to use this, you might want to move those files to another location.

Previously, I had a variable (set SFN=%1) but after trial and error, I noticed it was not need it at all, so I took it out. And I am calling CLEANUP.BAT so I do not repeat the same commands every time in the batch file...


:::::::::::::::CLEANUP.BAT:::::::::::::::
copy INIT.TXT+TEMP.TMP VARSET.BAT
call VARSET.BAT
del TEMP.TMP
del VARSET.BAT
:::::::::::::::CLEANUP.BAT:::::::::::::::

Deleting some files.


:::::::::::::INIT.TXT:::::::::::::
copy con init.txt[ENTER]
set SFN=[CTRL+^Z][ENTER]
:::::::::::::INIT.TXT:::::::::::::

This file is copied with TEMP.TMP to create VARSET.BAT, which contains the %LFN% variable.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 11978767
PAQd, 500 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now