+ Reply to Thread
Results 1 to 16 of 16

Checking if a file exists?

  1. #1
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Checking if a file exists?

    I'm scanning a folder with a few hundred files. Some of the files are "duplicates" that obsolete the originals and have endings such as "blahblah-2.dat" What I need to do is if I find that there is a "blahblah-2.dat", I then have to delete "blahblah.dat" because that one is obsolete. The problem here is that sometimes the original just isn't there. Someone already deleted it. So right now I have:

    Please Login or Register  to view this content.
    The problem is that if I do this, I can no longer use Dir() to jump to the next file in the directory. I'm not sure how I can fix this. Either I need a way to check for a file without using Dir, or find a command or something that will let me go back to scanning my directory as if nothing happened. I'm stumped.

  2. #2
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Checking if a file exists?

    this function will check if a file path exists

    pass it the path and it will return true or false

    EXAMPLE CALL:

    Please Login or Register  to view this content.
    Function:

    Please Login or Register  to view this content.
    Last edited by GaidenFocus; 07-30-2013 at 02:35 PM.

  3. #3
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,138

    Re: Checking if a file exists?

    I would use a search userform. Put in a search string, then have the files found listed in a listbox. Select the ones that are obsolete and click delete.
    David
    (*) Reputation points appreciated.

  4. #4
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    Quote Originally Posted by GaidenFocus View Post
    this function will check if a file path exists

    pass it the path and it will return true or false

    EXAMPLE CALL:

    Please Login or Register  to view this content.
    Function:

    Please Login or Register  to view this content.
    I'm not sure how that's any different than what I have. It still uses Dir, which if I'm not mistaken will mess up scanning for the next file with Dir(). Or does using it in a separate subroutine not affect it?

    Quote Originally Posted by Tinbendr View Post
    I would use a search userform. Put in a search string, then have the files found listed in a listbox. Select the ones that are obsolete and click delete.
    I could just go to the directory myself at that point. But like I said, it's literally hundreds of files to scan through.

  5. #5
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Checking if a file exists?

    The function would happen out side of your sub so it should not affect anything in there. This would happen separately. I have a program that creates lists of files and deletes them if i mark them for deletion, it should work fine.

  6. #6
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    I still get the same error as before: "Invalid procedure call or argument" when I try to do

    fileName = Dir()

    to get the next file.

  7. #7
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Checking if a file exists?

    Post your whole code and i will take a look

  8. #8
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    Please Login or Register  to view this content.
    Here you go.

  9. #9
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Checking if a file exists?

    It is early in the morning, but at a glance i think that

    Please Login or Register  to view this content.
    needs to be at the bottom. you have:

    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    I copied code that previously worked for a different purpose, but also cycled through the files in the folder.

    http://www.techonthenet.com/excel/formulas/dir.php

    They say that using Dir() will iterate over the files in a directory. I'm pretty sure if I use FileN = Dir(FileP & "*.dat") I'll just get the first file in the directory every time.

  11. #11
    Forum Contributor
    Join Date
    10-08-2010
    Location
    Texas
    MS-Off Ver
    Excel 2010
    Posts
    386

    Re: Checking if a file exists?

    You're right i checked a project i made a while back and Dir() is correct. Here is another wild guess, maybe because you are deleting files other than the one targeted it thorws the list off? I would make it reinitialize after each deletion, and use dir only if a deletion has not proced.

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Checking if a file exists?

    Hello TomWolowiec,

    Will the files always end with a letter and then once updated end with either a number or a dash and a number?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  13. #13
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    End goal would be to delete the originals and then rename the newer file to the name the original had. So it will just end in a letter.

    I'm starting to think the best thing to do would be to make an array of String variables, load all of the file names into that, and then figure out which files to delete.

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Checking if a file exists?

    Hello TomWolowiec,

    Will the original file names always end in a letter?

  15. #15
    Registered User
    Join Date
    07-08-2013
    Location
    Everett, Washington
    MS-Off Ver
    Excel 2007
    Posts
    28

    Re: Checking if a file exists?

    Yes, the originals end in a letter and the other "updated" ones end in a number or a dash followed by a number.

  16. #16
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Checking if a file exists?

    Hello TomWolowiec,

    Try this macro. It will only look for DAT files that end with the letter "V", "V" and a one or numbers, or "V-" and one or more numbers. All earlier versions will be deleted.

    NOTE:You should backup your files before running this macro!
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 07-31-2013 at 04:11 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. macro for checking if file exists
    By sieger007 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2011, 06:30 PM
  2. Checking if a file exists
    By duckboy1981 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-12-2008, 05:30 AM
  3. [SOLVED] Checking if file name already exists
    By Rob T in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-16-2007, 05:03 AM
  4. help with checking if dir or file exists
    By greenfalcon in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-08-2005, 03:03 PM
  5. [SOLVED] Routine for checking if file exists
    By Centurion in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-16-2005, 02:43 PM

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