+ Reply to Thread
Results 1 to 15 of 15

Extracting/copying files from a folder using VBA

  1. #1
    Bhupinder Rayat
    Guest

    Extracting/copying files from a folder using VBA

    Hi all,

    I have a folder (i.e. c:\current) that contains around 5000 csv files, and I
    have a list of around 200 off these files that I need to copy and paste to a
    new folder i.e. (c:\new).

    Rather than find each file manually or use the search function, is there a
    way i can tell vba to search through c:\current for the filenames that I
    specify and then copy and paste them to c:\new?

    Any help would be much appreciated.

    Kind Regards,

    Bhupinder.

  2. #2
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    assume the list of 200 is in column A of the activesheet and the file names
    are like a.csv or aaa.csv rather than c:\current\aaa.csv

    Sub copysomefiles()
    Dim cell as Range
    for each cell in Range("A1:A200")
    filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    Next
    end Sub

    --
    Regards,
    Tom Ogilvy


    "Bhupinder Rayat" <[email protected]> wrote in
    message news:[email protected]...
    > Hi all,
    >
    > I have a folder (i.e. c:\current) that contains around 5000 csv files, and

    I
    > have a list of around 200 off these files that I need to copy and paste to

    a
    > new folder i.e. (c:\new).
    >
    > Rather than find each file manually or use the search function, is there a
    > way i can tell vba to search through c:\current for the filenames that I
    > specify and then copy and paste them to c:\new?
    >
    > Any help would be much appreciated.
    >
    > Kind Regards,
    >
    > Bhupinder.




  3. #3
    Bhupinder Rayat
    Guest

    Re: Extracting/copying files from a folder using VBA

    Tom,

    excel doesn't like line 4 (filecopy....), it returns an error message saying
    'Application defined or object defined error'.

    I double-checked the names and filepaths, which are correct. Any ideas?

    Regards,

    Bhupinder.

    "Tom Ogilvy" wrote:

    > assume the list of 200 is in column A of the activesheet and the file names
    > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    >
    > Sub copysomefiles()
    > Dim cell as Range
    > for each cell in Range("A1:A200")
    > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > Next
    > end Sub
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Bhupinder Rayat" <[email protected]> wrote in
    > message news:[email protected]...
    > > Hi all,
    > >
    > > I have a folder (i.e. c:\current) that contains around 5000 csv files, and

    > I
    > > have a list of around 200 off these files that I need to copy and paste to

    > a
    > > new folder i.e. (c:\new).
    > >
    > > Rather than find each file manually or use the search function, is there a
    > > way i can tell vba to search through c:\current for the filenames that I
    > > specify and then copy and paste them to c:\new?
    > >
    > > Any help would be much appreciated.
    > >
    > > Kind Regards,
    > >
    > > Bhupinder.

    >
    >
    >


  4. #4
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    there was a typo in my code:

    Sub copysomefiles()
    Dim cell as Range
    for each cell in Range("A1:A200")
    filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    Next
    end Sub


    Just for confidence, As you can see from this demo in the immediate window:

    ? dir("c:\data\A*.xls")
    aaa_date_test.xls
    filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    ? dir("c:\data1\aaa_date_test.xls")
    aaa_date_test.xls

    filecopy works very well when properly constructed.
    --
    Regards,
    Tom Ogilvy


    "Bhupinder Rayat" <[email protected]> wrote in
    message news:[email protected]...
    > Tom,
    >
    > excel doesn't like line 4 (filecopy....), it returns an error message

    saying
    > 'Application defined or object defined error'.
    >
    > I double-checked the names and filepaths, which are correct. Any ideas?
    >
    > Regards,
    >
    > Bhupinder.
    >
    > "Tom Ogilvy" wrote:
    >
    > > assume the list of 200 is in column A of the activesheet and the file

    names
    > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > >
    > > Sub copysomefiles()
    > > Dim cell as Range
    > > for each cell in Range("A1:A200")
    > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > Next
    > > end Sub
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Bhupinder Rayat" <[email protected]> wrote in
    > > message news:[email protected]...
    > > > Hi all,
    > > >
    > > > I have a folder (i.e. c:\current) that contains around 5000 csv files,

    and
    > > I
    > > > have a list of around 200 off these files that I need to copy and

    paste to
    > > a
    > > > new folder i.e. (c:\new).
    > > >
    > > > Rather than find each file manually or use the search function, is

    there a
    > > > way i can tell vba to search through c:\current for the filenames that

    I
    > > > specify and then copy and paste them to c:\new?
    > > >
    > > > Any help would be much appreciated.
    > > >
    > > > Kind Regards,
    > > >
    > > > Bhupinder.

    > >
    > >
    > >




  5. #5
    Bhupinder Rayat
    Guest

    Re: Extracting/copying files from a folder using VBA

    Tom,

    I picked up on your typo before and tried to run it, but still the same
    error message came up.

    What does that error message relate to, my limited vba knowledge doesn't
    allow me to decipher microsoft help's interpretation of it.

    Thanks for your help.

    Bhupinder.
    "Tom Ogilvy" wrote:

    > there was a typo in my code:
    >
    > Sub copysomefiles()
    > Dim cell as Range
    > for each cell in Range("A1:A200")
    > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > Next
    > end Sub
    >
    >
    > Just for confidence, As you can see from this demo in the immediate window:
    >
    > ? dir("c:\data\A*.xls")
    > aaa_date_test.xls
    > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > ? dir("c:\data1\aaa_date_test.xls")
    > aaa_date_test.xls
    >
    > filecopy works very well when properly constructed.
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Bhupinder Rayat" <[email protected]> wrote in
    > message news:[email protected]...
    > > Tom,
    > >
    > > excel doesn't like line 4 (filecopy....), it returns an error message

    > saying
    > > 'Application defined or object defined error'.
    > >
    > > I double-checked the names and filepaths, which are correct. Any ideas?
    > >
    > > Regards,
    > >
    > > Bhupinder.
    > >
    > > "Tom Ogilvy" wrote:
    > >
    > > > assume the list of 200 is in column A of the activesheet and the file

    > names
    > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > >
    > > > Sub copysomefiles()
    > > > Dim cell as Range
    > > > for each cell in Range("A1:A200")
    > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > Next
    > > > end Sub
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > > "Bhupinder Rayat" <[email protected]> wrote in
    > > > message news:[email protected]...
    > > > > Hi all,
    > > > >
    > > > > I have a folder (i.e. c:\current) that contains around 5000 csv files,

    > and
    > > > I
    > > > > have a list of around 200 off these files that I need to copy and

    > paste to
    > > > a
    > > > > new folder i.e. (c:\new).
    > > > >
    > > > > Rather than find each file manually or use the search function, is

    > there a
    > > > > way i can tell vba to search through c:\current for the filenames that

    > I
    > > > > specify and then copy and paste them to c:\new?
    > > > >
    > > > > Any help would be much appreciated.
    > > > >
    > > > > Kind Regards,
    > > > >
    > > > > Bhupinder.
    > > >
    > > >
    > > >

    >
    >
    >


  6. #6
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    Application Defined or Object defined error:
    the only object in the command is Cell which is defined in my sample.

    As I demonstrated, supplied with proper arguments, filecopy works fine.

    I tried several different flawed constructs for filecopy and never received
    that error.

    filecopy does require that the workbook not be opened or the file not in use
    at the time it is copied.

    --
    Regards,
    Tom Ogilvy


    "Bhupinder Rayat" <[email protected]> wrote in
    message news:[email protected]...
    > Tom,
    >
    > I picked up on your typo before and tried to run it, but still the same
    > error message came up.
    >
    > What does that error message relate to, my limited vba knowledge doesn't
    > allow me to decipher microsoft help's interpretation of it.
    >
    > Thanks for your help.
    >
    > Bhupinder.
    > "Tom Ogilvy" wrote:
    >
    > > there was a typo in my code:
    > >
    > > Sub copysomefiles()
    > > Dim cell as Range
    > > for each cell in Range("A1:A200")
    > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > Next
    > > end Sub
    > >
    > >
    > > Just for confidence, As you can see from this demo in the immediate

    window:
    > >
    > > ? dir("c:\data\A*.xls")
    > > aaa_date_test.xls
    > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > ? dir("c:\data1\aaa_date_test.xls")
    > > aaa_date_test.xls
    > >
    > > filecopy works very well when properly constructed.
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Bhupinder Rayat" <[email protected]> wrote in
    > > message news:[email protected]...
    > > > Tom,
    > > >
    > > > excel doesn't like line 4 (filecopy....), it returns an error message

    > > saying
    > > > 'Application defined or object defined error'.
    > > >
    > > > I double-checked the names and filepaths, which are correct. Any

    ideas?
    > > >
    > > > Regards,
    > > >
    > > > Bhupinder.
    > > >
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > assume the list of 200 is in column A of the activesheet and the

    file
    > > names
    > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > >
    > > > > Sub copysomefiles()
    > > > > Dim cell as Range
    > > > > for each cell in Range("A1:A200")
    > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > Next
    > > > > end Sub
    > > > >
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > >
    > > > > "Bhupinder Rayat" <[email protected]> wrote

    in
    > > > > message news:[email protected]...
    > > > > > Hi all,
    > > > > >
    > > > > > I have a folder (i.e. c:\current) that contains around 5000 csv

    files,
    > > and
    > > > > I
    > > > > > have a list of around 200 off these files that I need to copy and

    > > paste to
    > > > > a
    > > > > > new folder i.e. (c:\new).
    > > > > >
    > > > > > Rather than find each file manually or use the search function, is

    > > there a
    > > > > > way i can tell vba to search through c:\current for the filenames

    that
    > > I
    > > > > > specify and then copy and paste them to c:\new?
    > > > > >
    > > > > > Any help would be much appreciated.
    > > > > >
    > > > > > Kind Regards,
    > > > > >
    > > > > > Bhupinder.
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  7. #7
    Bhupinder Rayat
    Guest

    Re: Extracting/copying files from a folder using VBA

    Hi,

    this worked for me......

    Sub copysomefiles()
    Dim fname As Range
    For Each fname In Range("fileRange")
    FileCopy "c:\current\" & fname, "c:\new\" & fname
    Next
    End Sub

    I omitted the .Name and it worked.

    Thanks for all your help,

    Best regards,


    Bhupinder
    "Tom Ogilvy" wrote:

    > Application Defined or Object defined error:
    > the only object in the command is Cell which is defined in my sample.
    >
    > As I demonstrated, supplied with proper arguments, filecopy works fine.
    >
    > I tried several different flawed constructs for filecopy and never received
    > that error.
    >
    > filecopy does require that the workbook not be opened or the file not in use
    > at the time it is copied.
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    >
    > "Bhupinder Rayat" <[email protected]> wrote in
    > message news:[email protected]...
    > > Tom,
    > >
    > > I picked up on your typo before and tried to run it, but still the same
    > > error message came up.
    > >
    > > What does that error message relate to, my limited vba knowledge doesn't
    > > allow me to decipher microsoft help's interpretation of it.
    > >
    > > Thanks for your help.
    > >
    > > Bhupinder.
    > > "Tom Ogilvy" wrote:
    > >
    > > > there was a typo in my code:
    > > >
    > > > Sub copysomefiles()
    > > > Dim cell as Range
    > > > for each cell in Range("A1:A200")
    > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > Next
    > > > end Sub
    > > >
    > > >
    > > > Just for confidence, As you can see from this demo in the immediate

    > window:
    > > >
    > > > ? dir("c:\data\A*.xls")
    > > > aaa_date_test.xls
    > > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > aaa_date_test.xls
    > > >
    > > > filecopy works very well when properly constructed.
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > > "Bhupinder Rayat" <[email protected]> wrote in
    > > > message news:[email protected]...
    > > > > Tom,
    > > > >
    > > > > excel doesn't like line 4 (filecopy....), it returns an error message
    > > > saying
    > > > > 'Application defined or object defined error'.
    > > > >
    > > > > I double-checked the names and filepaths, which are correct. Any

    > ideas?
    > > > >
    > > > > Regards,
    > > > >
    > > > > Bhupinder.
    > > > >
    > > > > "Tom Ogilvy" wrote:
    > > > >
    > > > > > assume the list of 200 is in column A of the activesheet and the

    > file
    > > > names
    > > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > > >
    > > > > > Sub copysomefiles()
    > > > > > Dim cell as Range
    > > > > > for each cell in Range("A1:A200")
    > > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > > Next
    > > > > > end Sub
    > > > > >
    > > > > > --
    > > > > > Regards,
    > > > > > Tom Ogilvy
    > > > > >
    > > > > >
    > > > > > "Bhupinder Rayat" <[email protected]> wrote

    > in
    > > > > > message news:[email protected]...
    > > > > > > Hi all,
    > > > > > >
    > > > > > > I have a folder (i.e. c:\current) that contains around 5000 csv

    > files,
    > > > and
    > > > > > I
    > > > > > > have a list of around 200 off these files that I need to copy and
    > > > paste to
    > > > > > a
    > > > > > > new folder i.e. (c:\new).
    > > > > > >
    > > > > > > Rather than find each file manually or use the search function, is
    > > > there a
    > > > > > > way i can tell vba to search through c:\current for the filenames

    > that
    > > > I
    > > > > > > specify and then copy and paste them to c:\new?
    > > > > > >
    > > > > > > Any help would be much appreciated.
    > > > > > >
    > > > > > > Kind Regards,
    > > > > > >
    > > > > > > Bhupinder.
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    >
    >


  8. #8
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    I guess I will have to take a hit on that one. I did put in cell.name
    rather than cell.value. I will have to admit to a typo. Sorry about that.

    --
    Regards,
    Tom Ogilvy

    "Bhupinder Rayat" <[email protected]> wrote in
    message news:[email protected]...
    > Hi,
    >
    > this worked for me......
    >
    > Sub copysomefiles()
    > Dim fname As Range
    > For Each fname In Range("fileRange")
    > FileCopy "c:\current\" & fname, "c:\new\" & fname
    > Next
    > End Sub
    >
    > I omitted the .Name and it worked.
    >
    > Thanks for all your help,
    >
    > Best regards,
    >
    >
    > Bhupinder
    > "Tom Ogilvy" wrote:
    >
    > > Application Defined or Object defined error:
    > > the only object in the command is Cell which is defined in my sample.
    > >
    > > As I demonstrated, supplied with proper arguments, filecopy works fine.
    > >
    > > I tried several different flawed constructs for filecopy and never

    received
    > > that error.
    > >
    > > filecopy does require that the workbook not be opened or the file not in

    use
    > > at the time it is copied.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Bhupinder Rayat" <[email protected]> wrote in
    > > message news:[email protected]...
    > > > Tom,
    > > >
    > > > I picked up on your typo before and tried to run it, but still the

    same
    > > > error message came up.
    > > >
    > > > What does that error message relate to, my limited vba knowledge

    doesn't
    > > > allow me to decipher microsoft help's interpretation of it.
    > > >
    > > > Thanks for your help.
    > > >
    > > > Bhupinder.
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > there was a typo in my code:
    > > > >
    > > > > Sub copysomefiles()
    > > > > Dim cell as Range
    > > > > for each cell in Range("A1:A200")
    > > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > > Next
    > > > > end Sub
    > > > >
    > > > >
    > > > > Just for confidence, As you can see from this demo in the immediate

    > > window:
    > > > >
    > > > > ? dir("c:\data\A*.xls")
    > > > > aaa_date_test.xls
    > > > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > > aaa_date_test.xls
    > > > >
    > > > > filecopy works very well when properly constructed.
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > >
    > > > > "Bhupinder Rayat" <[email protected]> wrote

    in
    > > > > message news:[email protected]...
    > > > > > Tom,
    > > > > >
    > > > > > excel doesn't like line 4 (filecopy....), it returns an error

    message
    > > > > saying
    > > > > > 'Application defined or object defined error'.
    > > > > >
    > > > > > I double-checked the names and filepaths, which are correct. Any

    > > ideas?
    > > > > >
    > > > > > Regards,
    > > > > >
    > > > > > Bhupinder.
    > > > > >
    > > > > > "Tom Ogilvy" wrote:
    > > > > >
    > > > > > > assume the list of 200 is in column A of the activesheet and the

    > > file
    > > > > names
    > > > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > > > >
    > > > > > > Sub copysomefiles()
    > > > > > > Dim cell as Range
    > > > > > > for each cell in Range("A1:A200")
    > > > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > > > Next
    > > > > > > end Sub
    > > > > > >
    > > > > > > --
    > > > > > > Regards,
    > > > > > > Tom Ogilvy
    > > > > > >
    > > > > > >
    > > > > > > "Bhupinder Rayat" <[email protected]>

    wrote
    > > in
    > > > > > > message

    news:[email protected]...
    > > > > > > > Hi all,
    > > > > > > >
    > > > > > > > I have a folder (i.e. c:\current) that contains around 5000

    csv
    > > files,
    > > > > and
    > > > > > > I
    > > > > > > > have a list of around 200 off these files that I need to copy

    and
    > > > > paste to
    > > > > > > a
    > > > > > > > new folder i.e. (c:\new).
    > > > > > > >
    > > > > > > > Rather than find each file manually or use the search

    function, is
    > > > > there a
    > > > > > > > way i can tell vba to search through c:\current for the

    filenames
    > > that
    > > > > I
    > > > > > > > specify and then copy and paste them to c:\new?
    > > > > > > >
    > > > > > > > Any help would be much appreciated.
    > > > > > > >
    > > > > > > > Kind Regards,
    > > > > > > >
    > > > > > > > Bhupinder.
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >




  9. #9
    Dave Peterson
    Guest

    Re: Extracting/copying files from a folder using VBA

    I think that there was another typo in Tom's code:

    Sub copysomefiles()
    Dim cell as Range
    for each cell in Range("A1:A200")
    filecopy "c:\current\" & cell.value, "c:\new\" & cell.value
    Next
    end Sub

    (.value instead of .name)

    Bhupinder Rayat wrote:
    >
    > Hi,
    >
    > this worked for me......
    >
    > Sub copysomefiles()
    > Dim fname As Range
    > For Each fname In Range("fileRange")
    > FileCopy "c:\current\" & fname, "c:\new\" & fname
    > Next
    > End Sub
    >
    > I omitted the .Name and it worked.
    >
    > Thanks for all your help,
    >
    > Best regards,
    >
    > Bhupinder
    > "Tom Ogilvy" wrote:
    >
    > > Application Defined or Object defined error:
    > > the only object in the command is Cell which is defined in my sample.
    > >
    > > As I demonstrated, supplied with proper arguments, filecopy works fine.
    > >
    > > I tried several different flawed constructs for filecopy and never received
    > > that error.
    > >
    > > filecopy does require that the workbook not be opened or the file not in use
    > > at the time it is copied.
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > >
    > > "Bhupinder Rayat" <[email protected]> wrote in
    > > message news:[email protected]...
    > > > Tom,
    > > >
    > > > I picked up on your typo before and tried to run it, but still the same
    > > > error message came up.
    > > >
    > > > What does that error message relate to, my limited vba knowledge doesn't
    > > > allow me to decipher microsoft help's interpretation of it.
    > > >
    > > > Thanks for your help.
    > > >
    > > > Bhupinder.
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > there was a typo in my code:
    > > > >
    > > > > Sub copysomefiles()
    > > > > Dim cell as Range
    > > > > for each cell in Range("A1:A200")
    > > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > > Next
    > > > > end Sub
    > > > >
    > > > >
    > > > > Just for confidence, As you can see from this demo in the immediate

    > > window:
    > > > >
    > > > > ? dir("c:\data\A*.xls")
    > > > > aaa_date_test.xls
    > > > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > > aaa_date_test.xls
    > > > >
    > > > > filecopy works very well when properly constructed.
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > >
    > > > > "Bhupinder Rayat" <[email protected]> wrote in
    > > > > message news:[email protected]...
    > > > > > Tom,
    > > > > >
    > > > > > excel doesn't like line 4 (filecopy....), it returns an error message
    > > > > saying
    > > > > > 'Application defined or object defined error'.
    > > > > >
    > > > > > I double-checked the names and filepaths, which are correct. Any

    > > ideas?
    > > > > >
    > > > > > Regards,
    > > > > >
    > > > > > Bhupinder.
    > > > > >
    > > > > > "Tom Ogilvy" wrote:
    > > > > >
    > > > > > > assume the list of 200 is in column A of the activesheet and the

    > > file
    > > > > names
    > > > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > > > >
    > > > > > > Sub copysomefiles()
    > > > > > > Dim cell as Range
    > > > > > > for each cell in Range("A1:A200")
    > > > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > > > Next
    > > > > > > end Sub
    > > > > > >
    > > > > > > --
    > > > > > > Regards,
    > > > > > > Tom Ogilvy
    > > > > > >
    > > > > > >
    > > > > > > "Bhupinder Rayat" <[email protected]> wrote

    > > in
    > > > > > > message news:[email protected]...
    > > > > > > > Hi all,
    > > > > > > >
    > > > > > > > I have a folder (i.e. c:\current) that contains around 5000 csv

    > > files,
    > > > > and
    > > > > > > I
    > > > > > > > have a list of around 200 off these files that I need to copy and
    > > > > paste to
    > > > > > > a
    > > > > > > > new folder i.e. (c:\new).
    > > > > > > >
    > > > > > > > Rather than find each file manually or use the search function, is
    > > > > there a
    > > > > > > > way i can tell vba to search through c:\current for the filenames

    > > that
    > > > > I
    > > > > > > > specify and then copy and paste them to c:\new?
    > > > > > > >
    > > > > > > > Any help would be much appreciated.
    > > > > > > >
    > > > > > > > Kind Regards,
    > > > > > > >
    > > > > > > > Bhupinder.
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > >
    > >


    --

    Dave Peterson

  10. #10
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    > I think that there was another typo in Tom's code:
    I think there was one typo done twice (.name vice .value). What other error
    did you see? Maybe I missed something else?? Gads!!

    --
    Regards,
    Tom Ogilvy

    "Dave Peterson" <[email protected]> wrote in message
    news:[email protected]...
    > I think that there was another typo in Tom's code:
    >
    > Sub copysomefiles()
    > Dim cell as Range
    > for each cell in Range("A1:A200")
    > filecopy "c:\current\" & cell.value, "c:\new\" & cell.value
    > Next
    > end Sub
    >
    > (.value instead of .name)
    >
    > Bhupinder Rayat wrote:
    > >
    > > Hi,
    > >
    > > this worked for me......
    > >
    > > Sub copysomefiles()
    > > Dim fname As Range
    > > For Each fname In Range("fileRange")
    > > FileCopy "c:\current\" & fname, "c:\new\" & fname
    > > Next
    > > End Sub
    > >
    > > I omitted the .Name and it worked.
    > >
    > > Thanks for all your help,
    > >
    > > Best regards,
    > >
    > > Bhupinder
    > > "Tom Ogilvy" wrote:
    > >
    > > > Application Defined or Object defined error:
    > > > the only object in the command is Cell which is defined in my sample.
    > > >
    > > > As I demonstrated, supplied with proper arguments, filecopy works

    fine.
    > > >
    > > > I tried several different flawed constructs for filecopy and never

    received
    > > > that error.
    > > >
    > > > filecopy does require that the workbook not be opened or the file not

    in use
    > > > at the time it is copied.
    > > >
    > > > --
    > > > Regards,
    > > > Tom Ogilvy
    > > >
    > > >
    > > > "Bhupinder Rayat" <[email protected]> wrote in
    > > > message news:[email protected]...
    > > > > Tom,
    > > > >
    > > > > I picked up on your typo before and tried to run it, but still the

    same
    > > > > error message came up.
    > > > >
    > > > > What does that error message relate to, my limited vba knowledge

    doesn't
    > > > > allow me to decipher microsoft help's interpretation of it.
    > > > >
    > > > > Thanks for your help.
    > > > >
    > > > > Bhupinder.
    > > > > "Tom Ogilvy" wrote:
    > > > >
    > > > > > there was a typo in my code:
    > > > > >
    > > > > > Sub copysomefiles()
    > > > > > Dim cell as Range
    > > > > > for each cell in Range("A1:A200")
    > > > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > > > Next
    > > > > > end Sub
    > > > > >
    > > > > >
    > > > > > Just for confidence, As you can see from this demo in the

    immediate
    > > > window:
    > > > > >
    > > > > > ? dir("c:\data\A*.xls")
    > > > > > aaa_date_test.xls
    > > > > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > > > aaa_date_test.xls
    > > > > >
    > > > > > filecopy works very well when properly constructed.
    > > > > > --
    > > > > > Regards,
    > > > > > Tom Ogilvy
    > > > > >
    > > > > >
    > > > > > "Bhupinder Rayat" <[email protected]> wrote

    in
    > > > > > message news:[email protected]...
    > > > > > > Tom,
    > > > > > >
    > > > > > > excel doesn't like line 4 (filecopy....), it returns an error

    message
    > > > > > saying
    > > > > > > 'Application defined or object defined error'.
    > > > > > >
    > > > > > > I double-checked the names and filepaths, which are correct.

    Any
    > > > ideas?
    > > > > > >
    > > > > > > Regards,
    > > > > > >
    > > > > > > Bhupinder.
    > > > > > >
    > > > > > > "Tom Ogilvy" wrote:
    > > > > > >
    > > > > > > > assume the list of 200 is in column A of the activesheet and

    the
    > > > file
    > > > > > names
    > > > > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > > > > >
    > > > > > > > Sub copysomefiles()
    > > > > > > > Dim cell as Range
    > > > > > > > for each cell in Range("A1:A200")
    > > > > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > > > > Next
    > > > > > > > end Sub
    > > > > > > >
    > > > > > > > --
    > > > > > > > Regards,
    > > > > > > > Tom Ogilvy
    > > > > > > >
    > > > > > > >
    > > > > > > > "Bhupinder Rayat" <[email protected]>

    wrote
    > > > in
    > > > > > > > message

    news:[email protected]...
    > > > > > > > > Hi all,
    > > > > > > > >
    > > > > > > > > I have a folder (i.e. c:\current) that contains around 5000

    csv
    > > > files,
    > > > > > and
    > > > > > > > I
    > > > > > > > > have a list of around 200 off these files that I need to

    copy and
    > > > > > paste to
    > > > > > > > a
    > > > > > > > > new folder i.e. (c:\new).
    > > > > > > > >
    > > > > > > > > Rather than find each file manually or use the search

    function, is
    > > > > > there a
    > > > > > > > > way i can tell vba to search through c:\current for the

    filenames
    > > > that
    > > > > > I
    > > > > > > > > specify and then copy and paste them to c:\new?
    > > > > > > > >
    > > > > > > > > Any help would be much appreciated.
    > > > > > > > >
    > > > > > > > > Kind Regards,
    > > > > > > > >
    > > > > > > > > Bhupinder.
    > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > >
    > > >

    >
    > --
    >
    > Dave Peterson




  11. #11
    GregR
    Guest

    Re: Extracting/copying files from a folder using VBA

    What if I want to do the same thing, but rather than copy only files
    with a specific name, copy only files dated after a certain date, such
    as 10/1/2005. TIA

    Greg


  12. #12
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    Sub copyFiles()
    Dim dte as Date, fName as String
    fname = dir("C:\Myfolder\" & *.xls)
    do while fname <> ""
    dte = FileDateTime("C:\Myfolder\" & fName)
    if clng(dte) > clng(DateValue("10/1/2005")) then
    filecopy "C:\MyFolder\" & fName, "C:\New\" & fName
    end if
    fName = Dir()
    Loop
    end Sub

    code is untested and may contain typos which you can feel free to correct
    yourself.

    --
    Regards,
    Tom Ogilvy


    "GregR" <[email protected]> wrote in message
    news:[email protected]...
    > What if I want to do the same thing, but rather than copy only files
    > with a specific name, copy only files dated after a certain date, such
    > as 10/1/2005. TIA
    >
    > Greg
    >




  13. #13
    GregR
    Guest

    Re: Extracting/copying files from a folder using VBA

    Tom, thank you


  14. #14
    Dave Peterson
    Guest

    Re: Extracting/copying files from a folder using VBA

    Nope that was it.

    Another counted from the previous and original post:

    filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"



    Tom Ogilvy wrote:
    >
    > > I think that there was another typo in Tom's code:

    > I think there was one typo done twice (.name vice .value). What other error
    > did you see? Maybe I missed something else?? Gads!!
    >
    > --
    > Regards,
    > Tom Ogilvy
    >
    > "Dave Peterson" <[email protected]> wrote in message
    > news:[email protected]...
    > > I think that there was another typo in Tom's code:
    > >
    > > Sub copysomefiles()
    > > Dim cell as Range
    > > for each cell in Range("A1:A200")
    > > filecopy "c:\current\" & cell.value, "c:\new\" & cell.value
    > > Next
    > > end Sub
    > >
    > > (.value instead of .name)
    > >
    > > Bhupinder Rayat wrote:
    > > >
    > > > Hi,
    > > >
    > > > this worked for me......
    > > >
    > > > Sub copysomefiles()
    > > > Dim fname As Range
    > > > For Each fname In Range("fileRange")
    > > > FileCopy "c:\current\" & fname, "c:\new\" & fname
    > > > Next
    > > > End Sub
    > > >
    > > > I omitted the .Name and it worked.
    > > >
    > > > Thanks for all your help,
    > > >
    > > > Best regards,
    > > >
    > > > Bhupinder
    > > > "Tom Ogilvy" wrote:
    > > >
    > > > > Application Defined or Object defined error:
    > > > > the only object in the command is Cell which is defined in my sample.
    > > > >
    > > > > As I demonstrated, supplied with proper arguments, filecopy works

    > fine.
    > > > >
    > > > > I tried several different flawed constructs for filecopy and never

    > received
    > > > > that error.
    > > > >
    > > > > filecopy does require that the workbook not be opened or the file not

    > in use
    > > > > at the time it is copied.
    > > > >
    > > > > --
    > > > > Regards,
    > > > > Tom Ogilvy
    > > > >
    > > > >
    > > > > "Bhupinder Rayat" <[email protected]> wrote in
    > > > > message news:[email protected]...
    > > > > > Tom,
    > > > > >
    > > > > > I picked up on your typo before and tried to run it, but still the

    > same
    > > > > > error message came up.
    > > > > >
    > > > > > What does that error message relate to, my limited vba knowledge

    > doesn't
    > > > > > allow me to decipher microsoft help's interpretation of it.
    > > > > >
    > > > > > Thanks for your help.
    > > > > >
    > > > > > Bhupinder.
    > > > > > "Tom Ogilvy" wrote:
    > > > > >
    > > > > > > there was a typo in my code:
    > > > > > >
    > > > > > > Sub copysomefiles()
    > > > > > > Dim cell as Range
    > > > > > > for each cell in Range("A1:A200")
    > > > > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > > > > Next
    > > > > > > end Sub
    > > > > > >
    > > > > > >
    > > > > > > Just for confidence, As you can see from this demo in the

    > immediate
    > > > > window:
    > > > > > >
    > > > > > > ? dir("c:\data\A*.xls")
    > > > > > > aaa_date_test.xls
    > > > > > > filecopy "c:\data\aaa_date_test.xls", "c:\data1\aaa_date_test.xls"
    > > > > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > > > > aaa_date_test.xls
    > > > > > >
    > > > > > > filecopy works very well when properly constructed.
    > > > > > > --
    > > > > > > Regards,
    > > > > > > Tom Ogilvy
    > > > > > >
    > > > > > >
    > > > > > > "Bhupinder Rayat" <[email protected]> wrote

    > in
    > > > > > > message news:[email protected]...
    > > > > > > > Tom,
    > > > > > > >
    > > > > > > > excel doesn't like line 4 (filecopy....), it returns an error

    > message
    > > > > > > saying
    > > > > > > > 'Application defined or object defined error'.
    > > > > > > >
    > > > > > > > I double-checked the names and filepaths, which are correct.

    > Any
    > > > > ideas?
    > > > > > > >
    > > > > > > > Regards,
    > > > > > > >
    > > > > > > > Bhupinder.
    > > > > > > >
    > > > > > > > "Tom Ogilvy" wrote:
    > > > > > > >
    > > > > > > > > assume the list of 200 is in column A of the activesheet and

    > the
    > > > > file
    > > > > > > names
    > > > > > > > > are like a.csv or aaa.csv rather than c:\current\aaa.csv
    > > > > > > > >
    > > > > > > > > Sub copysomefiles()
    > > > > > > > > Dim cell as Range
    > > > > > > > > for each cell in Range("A1:A200")
    > > > > > > > > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    > > > > > > > > Next
    > > > > > > > > end Sub
    > > > > > > > >
    > > > > > > > > --
    > > > > > > > > Regards,
    > > > > > > > > Tom Ogilvy
    > > > > > > > >
    > > > > > > > >
    > > > > > > > > "Bhupinder Rayat" <[email protected]>

    > wrote
    > > > > in
    > > > > > > > > message

    > news:[email protected]...
    > > > > > > > > > Hi all,
    > > > > > > > > >
    > > > > > > > > > I have a folder (i.e. c:\current) that contains around 5000

    > csv
    > > > > files,
    > > > > > > and
    > > > > > > > > I
    > > > > > > > > > have a list of around 200 off these files that I need to

    > copy and
    > > > > > > paste to
    > > > > > > > > a
    > > > > > > > > > new folder i.e. (c:\new).
    > > > > > > > > >
    > > > > > > > > > Rather than find each file manually or use the search

    > function, is
    > > > > > > there a
    > > > > > > > > > way i can tell vba to search through c:\current for the

    > filenames
    > > > > that
    > > > > > > I
    > > > > > > > > > specify and then copy and paste them to c:\new?
    > > > > > > > > >
    > > > > > > > > > Any help would be much appreciated.
    > > > > > > > > >
    > > > > > > > > > Kind Regards,
    > > > > > > > > >
    > > > > > > > > > Bhupinder.
    > > > > > > > >
    > > > > > > > >
    > > > > > > > >
    > > > > > >
    > > > > > >
    > > > > > >
    > > > >
    > > > >
    > > > >

    > >
    > > --
    > >
    > > Dave Peterson


    --

    Dave Peterson

  15. #15
    Tom Ogilvy
    Guest

    Re: Extracting/copying files from a folder using VBA

    Thanks!
    --
    Regards,
    Tom Ogilvy

    "Dave Peterson" <[email protected]> wrote in message
    news:[email protected]...
    > Nope that was it.
    >
    > Another counted from the previous and original post:
    >
    > filecopy "c:\current\" & cell.name, "c:\new\ & cell.name"
    >
    >
    >
    > Tom Ogilvy wrote:
    > >
    > > > I think that there was another typo in Tom's code:

    > > I think there was one typo done twice (.name vice .value). What other

    error
    > > did you see? Maybe I missed something else?? Gads!!
    > >
    > > --
    > > Regards,
    > > Tom Ogilvy
    > >
    > > "Dave Peterson" <[email protected]> wrote in message
    > > news:[email protected]...
    > > > I think that there was another typo in Tom's code:
    > > >
    > > > Sub copysomefiles()
    > > > Dim cell as Range
    > > > for each cell in Range("A1:A200")
    > > > filecopy "c:\current\" & cell.value, "c:\new\" & cell.value
    > > > Next
    > > > end Sub
    > > >
    > > > (.value instead of .name)
    > > >
    > > > Bhupinder Rayat wrote:
    > > > >
    > > > > Hi,
    > > > >
    > > > > this worked for me......
    > > > >
    > > > > Sub copysomefiles()
    > > > > Dim fname As Range
    > > > > For Each fname In Range("fileRange")
    > > > > FileCopy "c:\current\" & fname, "c:\new\" & fname
    > > > > Next
    > > > > End Sub
    > > > >
    > > > > I omitted the .Name and it worked.
    > > > >
    > > > > Thanks for all your help,
    > > > >
    > > > > Best regards,
    > > > >
    > > > > Bhupinder
    > > > > "Tom Ogilvy" wrote:
    > > > >
    > > > > > Application Defined or Object defined error:
    > > > > > the only object in the command is Cell which is defined in my

    sample.
    > > > > >
    > > > > > As I demonstrated, supplied with proper arguments, filecopy works

    > > fine.
    > > > > >
    > > > > > I tried several different flawed constructs for filecopy and never

    > > received
    > > > > > that error.
    > > > > >
    > > > > > filecopy does require that the workbook not be opened or the file

    not
    > > in use
    > > > > > at the time it is copied.
    > > > > >
    > > > > > --
    > > > > > Regards,
    > > > > > Tom Ogilvy
    > > > > >
    > > > > >
    > > > > > "Bhupinder Rayat" <[email protected]> wrote

    in
    > > > > > message news:[email protected]...
    > > > > > > Tom,
    > > > > > >
    > > > > > > I picked up on your typo before and tried to run it, but still

    the
    > > same
    > > > > > > error message came up.
    > > > > > >
    > > > > > > What does that error message relate to, my limited vba knowledge

    > > doesn't
    > > > > > > allow me to decipher microsoft help's interpretation of it.
    > > > > > >
    > > > > > > Thanks for your help.
    > > > > > >
    > > > > > > Bhupinder.
    > > > > > > "Tom Ogilvy" wrote:
    > > > > > >
    > > > > > > > there was a typo in my code:
    > > > > > > >
    > > > > > > > Sub copysomefiles()
    > > > > > > > Dim cell as Range
    > > > > > > > for each cell in Range("A1:A200")
    > > > > > > > filecopy "c:\current\" & cell.name, "c:\new\" & cell.name
    > > > > > > > Next
    > > > > > > > end Sub
    > > > > > > >
    > > > > > > >
    > > > > > > > Just for confidence, As you can see from this demo in the

    > > immediate
    > > > > > window:
    > > > > > > >
    > > > > > > > ? dir("c:\data\A*.xls")
    > > > > > > > aaa_date_test.xls
    > > > > > > > filecopy "c:\data\aaa_date_test.xls",

    "c:\data1\aaa_date_test.xls"
    > > > > > > > ? dir("c:\data1\aaa_date_test.xls")
    > > > > > > > aaa_date_test.xls
    > > > > > > >
    > > > > > > > filecopy works very well when properly constructed.
    > > > > > > > --
    > > > > > > > Regards,
    > > > > > > > Tom Ogilvy
    > > > > > > >
    > > > > > > >
    > > > > > > > "Bhupinder Rayat" <[email protected]>

    wrote
    > > in
    > > > > > > > message

    news:[email protected]...
    > > > > > > > > Tom,
    > > > > > > > >
    > > > > > > > > excel doesn't like line 4 (filecopy....), it returns an

    error
    > > message
    > > > > > > > saying
    > > > > > > > > 'Application defined or object defined error'.
    > > > > > > > >
    > > > > > > > > I double-checked the names and filepaths, which are correct.

    > > Any
    > > > > > ideas?
    > > > > > > > >
    > > > > > > > > Regards,
    > > > > > > > >
    > > > > > > > > Bhupinder.
    > > > > > > > >
    > > > > > > > > "Tom Ogilvy" wrote:
    > > > > > > > >
    > > > > > > > > > assume the list of 200 is in column A of the activesheet

    and
    > > the
    > > > > > file
    > > > > > > > names
    > > > > > > > > > are like a.csv or aaa.csv rather than

    c:\current\aaa.csv
    > > > > > > > > >
    > > > > > > > > > Sub copysomefiles()
    > > > > > > > > > Dim cell as Range
    > > > > > > > > > for each cell in Range("A1:A200")
    > > > > > > > > > filecopy "c:\current\" & cell.name, "c:\new\ &

    cell.name"
    > > > > > > > > > Next
    > > > > > > > > > end Sub
    > > > > > > > > >
    > > > > > > > > > --
    > > > > > > > > > Regards,
    > > > > > > > > > Tom Ogilvy
    > > > > > > > > >
    > > > > > > > > >
    > > > > > > > > > "Bhupinder Rayat"

    <[email protected]>
    > > wrote
    > > > > > in
    > > > > > > > > > message

    > > news:[email protected]...
    > > > > > > > > > > Hi all,
    > > > > > > > > > >
    > > > > > > > > > > I have a folder (i.e. c:\current) that contains around

    5000
    > > csv
    > > > > > files,
    > > > > > > > and
    > > > > > > > > > I
    > > > > > > > > > > have a list of around 200 off these files that I need to

    > > copy and
    > > > > > > > paste to
    > > > > > > > > > a
    > > > > > > > > > > new folder i.e. (c:\new).
    > > > > > > > > > >
    > > > > > > > > > > Rather than find each file manually or use the search

    > > function, is
    > > > > > > > there a
    > > > > > > > > > > way i can tell vba to search through c:\current for the

    > > filenames
    > > > > > that
    > > > > > > > I
    > > > > > > > > > > specify and then copy and paste them to c:\new?
    > > > > > > > > > >
    > > > > > > > > > > Any help would be much appreciated.
    > > > > > > > > > >
    > > > > > > > > > > Kind Regards,
    > > > > > > > > > >
    > > > > > > > > > > Bhupinder.
    > > > > > > > > >
    > > > > > > > > >
    > > > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > > > >
    > > > > >
    > > > > >
    > > > > >
    > > >
    > > > --
    > > >
    > > > Dave Peterson

    >
    > --
    >
    > Dave Peterson




+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1