What's new

Another question about VB 6

Olger901

Banned
Hello Guys

I found out that with the option filecopy I can copy files to different locations. But now I want to know how I can copy files from a CD and that the user can select Drive and a Directory instead of making a directory and that they are unable to choose. Anybody know how to do this?
 

Eagle

aka Alshain
Moderator
One thing that the common dialog controls ocx doesn't allow is the folder picker. This can be done through Windows API calls though. I've attached an example program that I created below. Feel free to use the Browsefolder function in your own program. Be sure to include the declarations and constants at the top.
 

Eagle

aka Alshain
Moderator
This is what you wanted right? Or did you want them to be able to choose the file name as well?
 
OP
O

Olger901

Banned
The program is ok this is what I meant. I made a prog that would allow users to copy files from a CD to a Certain directory on the HD and the user must only be able to select the directory and Diskdrive examples: c:\Testingmap\ or E:\Testingmap etc
 
OP
O

Olger901

Banned
Ok eagle the folder chooser works great how can I make this work? I did this

FileCopy "C:\Testfolder\test.txt", "What should I insert Here?"

I already tried Text1.\Txt1.txt but it doesnt work. Sorry As I told you I am pretty new to visual basic and dont know everything about it
 

Eagle

aka Alshain
Moderator
Heh, I had to pull out my reference book and brush up on FileCopy. I dont think I have ever used it in a practical purpose. Then I figured out I didnt need to know how to use it to answer your question. You need string concatination. In visual basic this is the ampherst and symbol (&).

Code:
FileCopy "C:\Testfolder\test.txt", BrowseFolder("Choose a Folder") & "\test.txt"

This is a single line command. In visual basic, like other modern languages you can call functions that return a type in the same line as other stuffs. Adds what the BrowseFolder function returns to "\test.txt". There is a problem however. If you ran the test program I gave you, you will notice that BrowseFolder doesnt return the ending "\" so you added it to the filename. But have you tried choosing a drive (the root directory) If you choose the C drive Browse folder would return "C:\" and this code above would give you "C:\\test.txt". Thats a bad thing. However drive letters are always three characters (not including the null character) so you can use the built in strlen function to determine the length of the string like so

Code:
Dim strFolder as String

strFolder = BrowseFolder("Choose a Folder")
If StrLen(strFolder) > 3 Then
     FileCopy "C:\Testfolder\test.txt", strFolder & "\test.txt"
Else
     FileCopy "C:\Testfolder\test.txt", strFolder & "test.txt"
End If

This will determine if the path returned by BrowseFolder is a drive letter or a drive letter and one or more folders. If its a drive letter, the file is "test.txt". If its a folder path then the file is "\test.txt".
 
Last edited:
OP
O

Olger901

Banned
Ok thanks it did the trick. BTW I went to the bookstore for a book about VB 6 and C++ these things are really handy.

BTW Just one last question my book also doesnt explain this.

When copying the file I want to add a Progress bar I already added the component Microsoft Common Controls 6.0 and a Progessbar but I can't seem to be getting it to show the blocks like it should do. Do you know an answer to this?
 

Doomulation

?????????????????????????
Easy.
Set the min and max values to a proper value to what you find fitting.
Then just set the .value to proper and the progressbar will change.

However, you cannot use this whilst copying files with filecopy since it does not return progress.
 

Eagle

aka Alshain
Moderator
Yes, see thats the problem. If your copying large amounts of files, you can set the min value to 0 and the max value to the number of files and as each file is copied, add 1 to the value of the progress bar. The problem here is that copying 1 file is usually impossible to tell how far along it is in the copy. Also Ive attactched a better progress bar. This one allows for horizontal and vertical progress bars as well as a solid fill instead of blocks and also you can change the colors among other things.
 

Eagle

aka Alshain
Moderator
Oh, the orginal of that ocx came with instructions for using it but I cant find where I downloaded it.
 

Eagle

aka Alshain
Moderator
Someone on my favorite Visual Basic forums (www.vbforums.com) gave me this alternative to the file copy command.

Code:
const BlockSize = 16384
Dim sInput As String
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
     While Not Eof(#1)
          sInput = Input$(BlockSize, #1)
          Put #2,,sInput
          Debug.Print cStr((Seek(#1)/Lof(#1)) * 100) & "% completed."
     Wend
Close

OK, the problem with VBForums is that they dont comment or explain it so I have come back and show you how it works.

This just a standard block size, the algorithm will copy 16384 bytes at a time, this break up allows for the progressbar to increment.
Code:
const BlockSize = 16384

The next two lines abviously open the files in binary format. They must be opened in binary format since we are copying by bytes. You will need to insert the filenames/variables.
Code:
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2

Performs a loop while not End Of File for file #1
Basically this means it will continue to copy till there is nothing left to copy. Duh.
Code:
While Not Eof(#1)

This is used to input from a file in binary format using the blocksize.
Code:
sInput = Input$(BlockSize, #1)

This is used to put the data you read into the new file.
Code:
     Put #2,,sInput

This is the confusing part, but it doesnt have to be. The seek funtion returns the current position of a file being read in. Basically there is a marker as long as the file is open that marks a position just after the last read byte, this is done automatically. The LOF function returns the total length (in bytes) of a file. The current amount read divided by the total amount multiplied by the constant 100 (100 percent) gives you the percentage complete. This is printed to the debug window. (This isnt what you want, I'll explain how to modifiy it but I want you to understand all the funtions listed here for future reference)
Code:
Debug.Print cStr((Seek(#1)/Lof(#1)) * 100) & "% completed."

Wend marks the end the loop if you didnt know that, Close closes a file, if the number is omitted, it closes all active files. Therefore both files are closed here.


Now how do we get what we want out of this? Well, its easier than the code we just looked over actually. Lets say we are using the better VBOverdrive progressbar I attached above. (The only important difference here in code is that it uses the name "Position" instead of "Value")

Here we set the min value to 0 and the max value to the total size of the file. You can set the Min value at design time, but the Max value must be set at runtime after the file is opened.

Next we replace the Debug.Print statement with an increment for the vboProgressbar using the Seek function to find the current position. And thats it! Hope this helps. You can also change the block size to 1 if you want a more smooth progressbar increase, however, the smaller the block size the less efficient the copy is and the more time it takes to do it.

Code:
const BlockSize = 16384
Dim sInput As String
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
     
     vboProgressbar.Max = Lof(#1)
     vboProgressbar.Position = 0
     
     While Not Eof(#1)
          sInput = Input$(BlockSize, #1)
          Put #2,,sInput
          
          vboProgressbar.Position = Seek(#1)

     Wend
Close

Theoretically this should work. I havent tested it though. If you have any problems let me know. Also this should reset with every file, a running total of all files being copied in this manner (if you are copying multiple files) would be much more difficut.
 

Eagle

aka Alshain
Moderator
Check this out, some one just gave me their version of a progressbar control, its still in the making stages though...
 

Doomulation

?????????????????????????
Eagle, i might tell you that there is a progressbar that can be "flat" and even horitizonal or vertical.
It's found in the microsoft windows common controls.
Although, when using this, the max value can be max of 255, so you should set it to 100 and use the previous formula to set the %. Just replace debug.print with progressbar.value
 

Eagle

aka Alshain
Moderator
Doomulation said:
Eagle, i might tell you that there is a progressbar that can be "flat" and even horitizonal or vertical.

Umm, nope sorry, cant do it. You can make it look verticle but you cant make the fill go upward so after the first block appears the entire thing ifs full.

[
Doomulation said:
Although, when using this, the max value can be max of 255, so you should set it to 100 and use the previous formula to set the %. Just replace debug.print with progressbar.value

Your right and wrong there, yes you should use the original 100% formula but actually the limit is different

From MSDN
For each property, you can specify an integer between -32,768 and 32,767, inclusive. The default settings are:

Max — 32,767.
Min — 0.

I think mine will let you go much higher though. But I checked and it does have a limit so yes, to be safe you will want to use the 100% forumla
 

Eagle

aka Alshain
Moderator
So this would work better

Code:
const BlockSize = 16384
Dim sInput As String

'These two can be coded at design time instead of runtime
vboProgressbar.Min = 0
vboProgressbar.Max = 100

vboProgressbar.Position = 0
Open SourceFile$ For Binary Lock Read As #1
Open DestFile$ For Binary Lock Write As #2
     While Not Eof(#1)
          sInput = Input$(BlockSize, #1)
          Put #2,,sInput
          vboProgressbar.Position cStr((Seek(#1)/Lof(#1)) * 100)
     Wend
Close
 

Doomulation

?????????????????????????
Eagle said:
Your right and wrong there, yes you should use the original 100% formula but actually the limit is different
[/B]
Well, then that must be the "old" progressbar then, lol.
But it doesn't really matter :)
 

Eagle

aka Alshain
Moderator
Old progress bar? Its Windows Common Controls 6.0 (SP4) Thats the latest there is unless you have VB.Net in which case you shouldnt be replying to a VB 6 question anyway.
 

Eagle

aka Alshain
Moderator
Doomulation said:
Supposed there was one...somewhere, perhaps in older libraries.

Oh, did you mean yours was older? Ok, well I dont know about that, but it seems unlikely that they would downgrade the control.
 

Top