+ Reply to Thread
Results 1 to 19 of 19

Check If Files Exist and Return

  1. #1
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Thumbs up Check If Files Exist and Return

    Hi,
    I have been searching here and on the www. for the this but no succes.

    I would like to check/search if (FileName As String) is still exists in its directory.
    --Why as string: because I have a hidden kollom where the links to the files are in txt format.--

    I can't write VBA But maybe I can sketch it?

    NOT A CODE

    Please Login or Register  to view this content.

    (MOD: tags even for psuedo-code please... yes, we're that pedantic!)
    Last edited by Bluewhistler; 02-08-2010 at 08:40 AM. Reason: Solved By DonkeyOte! Thx.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return: (Not)Available

    Could you perhaps use something like:

    Please Login or Register  to view this content.
    the above, stored in a Module (in a Macro Enabled file), could be called from a cell, eg:

    Please Login or Register  to view this content.

    where A1 holds File Name as string (assume full path)

  3. #3
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Thx,
    This works perfect! I'll use this.

    But I was wondering if the same type of code would work to recodnise this input.

    Please Login or Register  to view this content.

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return: (Not)Available

    If you want to pass the string directly

    Please Login or Register  to view this content.

    called from the cell using:

    Please Login or Register  to view this content.

    I'm not sure what your goal is - but if you want to return the filename where it exists else something else then you could adapt the above along the lines of:

    Please Login or Register  to view this content.

    so the above would return the file name if it exists else it will return an alternative value - the alternative value would be "No" if unspecified in the cell call else it would use the specific alternative specified, eg:

    Please Login or Register  to view this content.

    Of course if the drive specified is invalid (eg AA) it will return an error - you could account for that also if nec., I'm not operating on a network environment so I've not tested for full blown server paths.

  5. #5
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Almost there.

    Just one more problem When I add a file to a directory which is to be checked by your code the result isn't updated by F9 nor bye saving/closing/reopening.

    Could you make it possible for these code to refresh with F9?

    Please Login or Register  to view this content.
    Please Login or Register  to view this content.

    I had the same problem before and it got solved this way...

    Quote Originally Posted by vikas.bhandari View Post
    Oh!!!!! I didnt know it

    if you add the following line to your code :

    Please Login or Register  to view this content.
    It will update the values as you want it to. Please use my previous workbook sample and include the above line in the LastSavedTime function. It should be done now.

    Thanks,
    Vikas
    Quote Originally Posted by Bluewhistler View Post
    Than you very much.

    Please Login or Register  to view this content.
    Does the trick!

    I did as you told me and this works lika a charm! now when I press F9 the dates are refreshed!

    Here's the Result & Solution:

    Please Login or Register  to view this content.
    Thx again Vikas


    The cell is just 26pxl wide so The Yes and No if fine.

    What I'm trying to get is 2 collums, one is for Autcad files and the other for E-plan.
    The reason is that there are electrical drawings in both format and just one excel list of both the drawings. these are used by several ppl and the drawings are managad and edited by me only. So the ppl who use the Excel list can see if there is a drawing and if its drawn in acad or E-plan or both.
    Last edited by Bluewhistler; 02-04-2010 at 07:10 AM.

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return: (Not)Available

    Quote Originally Posted by Bluewhistlter
    Just one more problem When I add a file to a directory which is to be checked by your code the result isn't updated by F9 nor bye saving/closing/reopening.

    Could you make it possible for these code to refresh with F9?
    If you add a file to a directory and subsequently recalculate XL via CTR+ALT+F9 the function should update but yes you can add a Volatile setting to the UDF such that it recalcs via F9

    Please Login or Register  to view this content.

    However...

    I'd make the point that adding a file to a folder will not invoke any reaction in XL so the UDF is always susceptible to error in so far as it requires a Volatile action to take place within XL itself before it will update.

  7. #7
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Quote Originally Posted by DonkeyOte View Post


    However...

    I'd make the point that adding a file to a folder will not invoke any reaction in XL so the UDF is always susceptible to error in so far as it requires a Volatile action to take place within XL itself before it will update.
    Sorry don't quite understand this, do you sudgest using Ctrl-Alt-F9 instead of Volatile action?

    FYI This

    Please Login or Register  to view this content.
    seems to work both ways with this
    Please Login or Register  to view this content.
    and with this
    Please Login or Register  to view this content.
    Thanks DonkeyOte

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return: (Not)Available

    Quote Originally Posted by Bluewhistler
    do you sudgest using Ctrl-Alt-F9 instead of Volatile action?
    No, I was just trying to make the point that even with Volatile applied the process of adding / removing a file from the directory would not trigger a recalculation of the UDF - a Volatile action would be required in XL to invoke the Calculation.

    So in short, though using Volatile in this scenario makes sense it does not resolve all issues you face.

    Quote Originally Posted by Bluewhistle
    seems to work both ways with this:

    =FileExists("C:\test.txt")
    and with this

    =FileExists("A1")
    Presumably you meant A1 rather than "A1" ?

    Yes, given the variable strFN is a string either will work, ie the value within A1 is passed to the UDF as the string (or "A1" if that's what you used which would return "No").

    Generally speaking if you were using the 2nd version exclusively (ie always using a range to determine the value) you would use a Range variable in the UDF given you can do more with that variable from within the UDF than you can with just it's value.

  9. #9
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Quote Originally Posted by DonkeyOte View Post
    Presumably you meant A1 rather than "A1" ?.
    Indeed you're right.

    Thx for your time and help.

  10. #10
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Hi,

    I labeled this thread as Solved but I came across a tiny/little problem with the last code.
    Please Login or Register  to view this content.
    When the code checks an empty cel the result is Yes

    Please Login or Register  to view this content.
    Where A1=empty the formula returns Yes in the cel.

  11. #11
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return: (Not)Available

    Either test the content prior to calling the UDF (advised), ie:

    Please Login or Register  to view this content.

    or invoke the UDF and test content

    Please Login or Register  to view this content.

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Check If Files Exist and Return: (Not)Available

    Just a thought I had regarding the file listing updating. Since you're using VBA anyway, why not create a macro and put it on a RELOAD button. The macro saves and reopens the current workbook, thus all values update in the entire workbook.

    Something like:
    Please Login or Register  to view this content.
    Or does the Volatile make all this unnecessary?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  13. #13
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return: (Not)Available

    Quote Originally Posted by JBeaucaire View Post
    Something like:
    Please Login or Register  to view this content.
    Or does the Volatile make all this unnecessary?
    It works perfectly with the Volatile when close/open or without the Volitile but you would have to refresh with Ctr-Alt-F9. thx

    Quote Originally Posted by DonkeyOte View Post
    Either test the content prior to calling the UDF (advised), ie:

    Please Login or Register  to view this content.
    [/code]
    Yes I was thinking about using this test. And now I Will.

    I have another request about this? You mentioned the possibility to return the Dir location of the file itself. So I was wondering if its also possible to return Filename without extention?

  14. #14
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return

    Given you state all the links are to text files could you not simply take strFN and modify ?

    Please Login or Register  to view this content.

    or am I missing something.

    Regards the other code, I'm not sure I see the value of a button related event over a key press of F9 - either way a manual intervention of some sort is required though granted the button may be deemed more intuitive to an end user.

  15. #15
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return

    I'm sorry for not explaining properly I am new to excel and English is not my first language but third

    I attached the file for a better view at what I'm trying to get. With comments in some cells

    You only need to make 2 files on your C drive: UTA01.txt and UTA01.pdf it doesn't matter if file is not real I think. Edit: OK I checked that it dosn't matter

    I also removed the Volatile in the code. So you'll see what happens when you manually refresh in the first sheet UTA.
    Attached Files Attached Files

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return

    I don't understand the reasoning behind the removal of the Volatile statement so can't comment.

    I based the prior suggestion on your initial post:

    Quote Originally Posted by BlueWhister
    I would like to check/search if (FileName As String) is still exists in its directory.
    --Why as string: because I have a hidden kollom where the links to the files are in txt format.--
    If as it seems the file type is not always text (eg pdf etc) and the intention is to return the filename less extension rather than "Yes" then if you assume the extension is always .??? (ie 4 chars) you can use:

    Please Login or Register  to view this content.

    If you can't make that assumption then you could use:

    Please Login or Register  to view this content.

    Other than that I confess I'm lost.

  17. #17
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Re: Check If Files Exist and Return

    Alright I think I know where I misguided you I think I actually need another code or a basic formula for this? New thread perhaps?

    Lets say:
    A2 is =Filexist(A3) Result: Yes
    B2 is =Filexist(B3) Result: “”
    and
    A3 is J:\Work\Tecnical\UTA01.txt
    B3 is S:\Building\UTA01.pdf

    I can use this to display in “UTA01” in A1

    Please Login or Register  to view this content.
    But then I have to do that 400 times changing only the number till 40 same for UTB-C-D-E-F-M-N and P

    But if in some way I could extract the UTA01 from A3 J:\Work\Tecnical\UTA01.txt the problem will be solved. And with the =IF(or… formula I don’t need Cell B3 S:\Building\UTA01.pdf.
    So
    Please Login or Register  to view this content.
    Thx

  18. #18
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Check If Files Exist and Return

    Using your example... assuming:

    a) A1 relates to A2/B2 which are in turn related to A3/B3 respectively via the UDF (returning either Yes or Null)

    b) A3 always contains the .txt file path and B3 the .pdf path

    Then

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    02-02-2010
    Location
    Belgium
    MS-Off Ver
    Excel 2003
    Posts
    47

    Thumbs up Re: Check If Files Exist and Return


    Quote Originally Posted by DonkeyOte View Post
    Please Login or Register  to view this content.
    Thats Creazy...
    But it works! Seriously I'm gonna get me a book of Excel and some lessens.
    Thx again DonkeyOte, you've been a big help.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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