1.

Solve : Zipping files using batch file?

Answer»

Is there a way to:
1)LOCATE files with a known file extension on a known Drive and
2)send them to a zip file on windows XP desktop?

I know XP has a zip function, not sure what it's CALLED....could it be used instead of an old pkzip exec.

Also: how do you determine the Short File Name with XP as "Open Command Prompt Here" shows only Long File Names.
Actually PKZip is better suited for this task...have you read the PKZip Help files ? ?Apparently microsoft made it impossible to use command line to do this. Bottom line I still need a way, without software installation to locate files of a particular extension, compile them in a single file for emailing. What about using makecab.exe for the compiling??

Usage:

MAKECAB [/V[n]] [/D var=value ...] [/L dir] source [destination]

MAKECAB [/V[n]] [/D var=value ...] /F directive_file [...]

source File to compress.

destination File name to give compressed file. If omitted, the last character of the source file name is replaced with an underscore (_) and used as the destination.

/F directives A file with MakeCAB directives (may be repeated). Refer to Microsoft Cabinet SDK for information on directive_file.

/D var=value Defines variable with specified value.

/L dir Location to place destination (default is current directory).

/V[n] Verbosity level (1..3).


Let me clarify:

1) I need to go to a specific hard drive (EXAMPLE: D:\)

CD D:\

2) From there I need to locate all files, including those in any sub-directories with a particular file extension(EXAMPLE: *.mp3)

If exists *.mp3

3) Copy them to a folder called (EXAMPLE: MYMP3Z)

echo | Y copy file to MYMP3Z

4) Convert the folder or all files in the folder into a single file either .zip, .cab....whatever(EXAMPLE: MYMP3Z.CAB)

pkzip D:\MP3Z\*.* C:\Documents and Settings\John Doe\Desktop\MP3Z.zip

5) Place that file on the windows desktop(EXAMPLE: C:\Documents and Settings\John Doe\Desktop)

6) Delete the folder MYMP3Z

ECHO | Y DEL D:\MP3Z\*.*
RD D:\MP3Z

I don't think PKZIP for DOS can do all of that.

I may also need to a DOS zip executable to the Windows\System32 Directiry prior to running.



ionic,
Iam using 7-zip software for zipping and unzipping files using batch file.


http://www.7-zip.org/download.html

Download the .exe file from the above link and install on ur pc.

suppose u r installed in c:\programfiles\
Please assign the path in envirnomental variables (my computer--properties) ---system variables---path .
say: c:\programfiles\7z paste this path in the system variables---path

cmd for unzpping:
7z e c:\a.zip -oc:\

-o : output dir.
unzips the a.zip to a.txt file

cmd for zipping :
7z a -tzip c:\b.zip c:\a.txt
zips the a.txt to b.zip

Save the cmd in .bat format and execute

Hope this may help you.

regards,
chinna


I am currently playing around with XCOPY to copy the files from directories and subdirectories and place them in a specified folder. From there I can run a zip app to place them in one file where I want.

Not sure if XCOPY can copy only the files from a directory and subdirectories without maintaining the folder structure.

This is what I have come up with thus far:


@ECHO OFF
MD D:\TXTZ

XCOPY I:\*.txt D:\TXTZ\ /n

PKZIP -es -m -rp -r- D:\MYTXTZ.ZIP D:\TXTZ\*.*

IF EXISTS D:\MYTXTZ.ZIP (
ECHO Y|RD D:\TXTZ /s /q
)ELSE(
ECHO Missing file "MYTXTZ.ZIP"
)
exit


The results: Although I included the full path where XCOPY should find files, it copies files from where the batch file is located, in this case on the desktop. XCOPY did not copy any files in subfolders on the desktop.
If I set the switch for XCOPY to Coy folders and subfolders except empty ones with /S it does copy them, but
I'd rather leave the folders out of the equation.

PKZIP worked fine

RMDIR did not delete the folder D:\TXTZ I think I'm stuck here. Any help would be appreciated. I think it should work but I need to lookup the switch options. Then again, maybe xcopy is not the right way to go.Try HereYou're making this harder than it has to be. You had all the pseudo-code laid out, all you needed to do was code it up:

Code: [Select]cd /d d:\
for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.mp3') do (
copy "%%x" d:\mymp3z
)
cscript scriptname.vbs
rd d:\mymp3z /s/q

The cscript is below. You can indeed use Window's built-in zip ROUTINES; you just can't use batch code.

Code: [Select]strTargetFile = "C:\Documents and Settings\John Doe\Desktop\MP3Z.zip"

Set fso = CreateObject("Scripting.FileSystemObject")
Set zip = fso.CreateTextFile(strTargetFile)
zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
zip.Close
WScript.Sleep 500

Set objShell = CreateObject("Shell.Application")
Set objFile = objShell.NameSpace(strTargetFile)
Set src = objShell.NameSpace("d:\MYMP3Z")

objFile.CopyHere src
WScript.Sleep 30000

Save the script (the name has to match the batch file reference) in the same directory as the batch file and with a vbs extension.

The Copyhere method runs asynchronous to the batch file. I built in a 30 second time delay but you may have to increase it based on the size and number of the mp3 files. The time interval on the sleep method is measured in milliseconds. This is important as you do not want to remove the mymp3z directory before you finish zipping the files.

Good luck. Quote from: Sidewinder on January 03, 2008, 02:16:23 PM

You're making this harder than it has to be. You had all the pseudo-code laid out, all you needed to do was code it up:

That's just it, the cscript and vbs stuff is completely foreign to me. Coding it up was not possible so I would need expernal programs like pkzip and batch file for a time delay...

I do appreciate your work and will let you know how it turns out.

ThanksI have run the code(bat & vbs) and encountered some problems:

There is the code I am using at present:
Code: [Select]@echo off
md c:\docume~1\brianb~1\desktop\TEXTZ\
cd /d i:\
for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
copy "%%x" C:\docume~1\brianb~1\desktop\TEXTZ\
)
cscript grabnzip.vbs
rd C:\docume~1\brianb~1\desktop\TEXTZ\ /s/q
As a test I altered the code to copy some txt files...
The first problem I had was that a folder was not created, but rather a file with no extension. So I added the
line to make a directory. I also changed the folder path to be the same as the .zip destination.

Code: [Select]strTargetFile = "C:\docume~1\brianb~1\desktop\TEXTZ.zip"

Set fso = CreateObject("Scripting.FileSystemObject")
Set zip = fso.CreateTextFile(strTargetFile)
zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
zip.Close
WScript.Sleep 500

Set objShell = CreateObject("Shell.Application")
Set objFile = objShell.NameSpace(strTargetFile)
Set src = objShell.NameSpace("C:\docume~1\brianb~1\desktop\TEXTZ\")

objFile.CopyHere src
WScript.Sleep 30000
After all the code ran I did get a folder created and the txt files(15) were copied to the folder. Then the folder
was deleted, but there was no zip file created.

It seems that at this point I might as well use pkzip as I can't achieve my goal with a single batch file. So if I use a .vbs file or pkzip.exe makes no difference.

You're also ignoring how powerful PKZip is/was...it went outside of DOS/batch limitations in ALOT of ways way before it's time and it would be a good learning experience to do some more research along with the link i provided...

XTree Gold is another example of batch/scripting on steroids but takes some getting used to...actually that's innaccurate. It's not a batch/script tool rather a powerful DOS shell.Using PKZip is a fine alternative. I only posted because you originally wanted to use the built-in capabilities of Windows.

When I tested my original code, it worked fine! I'm very surprised the TEXTZ.zip file did not end up in C:\docume~1\brianb~1\desktop. I'm going to take it on faith that brianb~1 is your account.

Good luck.

Quote from: patio on January 04, 2008, 02:01:59 PM
You're also ignoring how powerful PKZip is/was...it went outside of DOS/batch limitations in alot of ways way before it's time and it would be a good learning experience to do some more research along with the link i provided...

I will take the time to read the pkzip documentation. It's not a short doc but like you said it may be all I need.Quote from: Sidewinder on January 04, 2008, 02:44:47 PM
When I tested my original code, it worked fine! I'm very surprised the TEXTZ.zip file did not end up in C:\docume~1\brianb~1\desktop. I'm going to take it on faith that brianb~1 is your account.


At first it did not. After using PKZip.exe it did, but it contained files other than the ones from the TEXTZ folder.

The trouble I was having was that I refferenced a thumb drive for the test(I:\) and once there it could not change directories back to the hard drive(C:\). Not that I found this out the original code you wrote might work.

I am going to revisit it.

Yes brianb~1 is my account. I also searched all drives for the file but found nothing.

P.S. What does the "/d" after the CD command designate? It doesn't show up as syntax for the DOS CD command reference.

Sidewinder,

I got your code to run. as wel as a PKZip version. It might be possible for PKZip to do it all by itself, but for now I am satisfied.

This is the code that worked for me:

Code: [Select]cd /d d:\
md TEXTZ
cd /d d:\testfold\
for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
copy "%%x" d:\TEXTZ\
)

CD /d C:\Docume~1\brianb~1\desktop\
cscript grabnzip.vbs

rd d:\TEXTZ /s/q
exit
Code: [Select]strTargetFile = "C:\docume~1\brianb~1\desktop\TEXTZ.zip"

Set fso = CreateObject("Scripting.FileSystemObject")
Set zip = fso.CreateTextFile(strTargetFile)
zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
zip.Close
WScript.Sleep 1000

Set objShell = CreateObject("Shell.Application")
Set objFile = objShell.NameSpace(strTargetFile)
Set src = objShell.NameSpace("D:\TEXTZ\")

objFile.CopyHere src
WScript.Sleep 15000
I'd still like to know what CD /d means...

Thanks for your help.

patio, thanks for your insite as well. I will be examining the POWER of pkzip
soon!


Forgot...here is the batch file code using pkzip.

Code: [Select]@echo off
cd /d d:\
md TEXTZ
cd /d d:\testfold\
for /f "tokens=* delims=" %%x in ('dir /s /b /a:-d *.txt') do (
copy "%%x" d:\TEXTZ\
)
call PKZIP c:\docume~1\brianb~1\desktop\TXTZ.ZIP d:\TEXTZ\*.*
pause

rd d:\TEXTZ /s /q
exit
Brian


Discussion

No Comment Found