-
Hello all,
I'm trying to write a subroutine that will make a copy of all the files in a directory and then rename them to say tmp1.txt, tmp2.txt, tmp3.txt, ... etc,.. For example if the directory c:\myfolder consists of the following three files:
apple.txt, orange.txt and pears.txt; I the program to make a copy of the three files and rename it to tmp1.txt, tmp2.txt, and tmp3.txt. Any help will be much appreciated!
Thanks in advance!
Marci Sarwan ([email protected])
P.S. please include some sample code.
-
You can use a FileListBox to list all the files in a directory, then loop through them and use the CopyFile API function to copy them.
Code:
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Private Sub Command1_Click()
Dim i As Long
For i = 0 To File1.ListCount - 1
Call CopyFile(File1.Path & "\" & File1.List(i), "tmp" & i + 1 & ".txt", False)
Next
End Sub
-
Or without a FileListbox:
Code:
Private Sub Command1_Click()
Dim a As string, i as long
a=dir(Yourdirectory)
Do while len(a)
i=i+1
Call CopyFile(Yourdirectory & "\" & a, "tmp" & i + 1 & ".txt", False)
a=Dir
Loop
End Sub
-
stupid question
How do you guys know to declare this for the CopyFile?
Public Declare Function CopyFile Lib "kernel32" Alias "CopyFileA" (ByVal lpExistingFileName As String, ByVal lpNewFileName As String, ByVal bFailIfExists As Long) As Long
Marci Sarwan ([email protected])
-
because we are 3l3et hax0r doods
;)
just kidding, there is a tool called the API Viewer that comes with VB.
you view the declarations, and there is also something called the API guide which gives the declares, and an example,
you can get the API guide here
http://www.allapi.net
-
The API Viewer actually views any files, but when you view Win32api.txt you see the declarations, constants and types.
I usually don't use Apiview, instead i'm searching the win32api.txt in wordpad, it's easier to find stuff that way.
-
I think the API Viewer is easier, because you can copy multiple things..... API Viewer is much better :D
-
And you think you can't do that in wordpad?
It's better when you can browse for stuff there, where related consts and declares are on the same place, and the view is too small in the apiview. + you have to click in the small combo to switch between declares and const and types, i hate that. Also if you search there you won't find it unless you search for the beginning chars