Results 1 to 8 of 8

Thread: How to copy files and renaming them in VB??

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Posts
    52

    Unhappy

    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.
    Marci Sarwan

  2. #2
    Guest
    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

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2000
    Location
    Posts
    52

    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])
    Marci Sarwan

  5. #5
    Guest
    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

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Guest
    I think the API Viewer is easier, because you can copy multiple things..... API Viewer is much better

  8. #8
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width