Hi,
I'm trying to make a backup program but suddenly i got stock with this problem. I want to search all the .txt file in my drive c: and make a zip file on their respective folders. Can somebody share a code for this one?
Thanks in advance.

Newvbguy

Hi,
I'm trying to make a backup program but suddenly i got stock with this problem. I want to search all the .txt file in my drive c: and make a zip file on their respective folders. Can somebody share a code for this one?
Thanks in advance.

Newvbguy

Ok wait a minute.... if I'm not mistaken, you want to search the hard drive for .txt files, then zip each text file by itself (so instead of hi.txt, you have hi.zip) in the folder that it is in? Or, you want to zip say, all the .txt files in c:\windows into a zip, all the .txt files in c:\program files into a zip, etc, etc?

As for searching the entire hard drive for .txt files, I have a solution for that. There a few different ways of accomplishing this task. One of the easiest (and most effecient) ways that I have found to get this done, is by shelling to DOS, and using it's old-timer fun. If you stick a little VBS in your VB Program, you can trivialize the searching process. Here is a little snippit:

dim wsh
set wsh = createobject("WScript.Shell")

open "c:\runme.bat" for output as #1
     print "@echo off"
     print "dir /a/s/b c:\*.txt >>c:\textpaths.txt"
close #1

wsh.Run "c:\runme.bat", 0, 1
kill "c:\runme.bat"

This code will create a DOS batch file. The batch file executes a dir command, with the /a/s/b switches. A little breakdown here, /a tells dir to show ALL files... hidden, system, whatever. /s is the switch to recurse into sub-directories (all folders). The last switch, /b is for "bare" which is simply... only show me the paths and filenames. Then, we make use of the redirection operators of DOS (the >>). A little about those, is that > means to over-write whatever file files, while >> means to append. So, the command in the batch file basically says, show me a list of paths and filenames to all .txt files, and store that information in the file "textpaths.txt".
Then, We use VBS to run the batch file. The reason I've chosen this method, is because the last trailing number (1) in the wsh.run method, actually tells WSH not to continue processing until the called program or command actually finishes running. This stops VB from running any open commands on a file that is only partially completed. It would suck to open c:\textpaths.txt before all the paths are in it. When all is said and done (with the code above), there will be a file, on the c:\ that contains a list of paths and filenames of only .txt files (1 per line). So You can just:

dim txtPaths() as string

cnt = 0
open "c:\textpaths.txt" for input as #1
     do until eof(1)
          redim preserve txtPaths(cnt)
          line input #1, txtPaths(cnt)
          cnt = cnt + 1
     loop
close #1
if dir("c:\textpaths.txt", vbnormal) <> "" then kill "c:\textpaths.txt"

That should create an array txtPaths, containing ALL of the .txt files on the C: drive.

/me wipes his forehead

Hey men,
this is good enough. thanks a lot.

newvbguy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.