Jump to content
UBot Underground

Checking for the Presence of a file


Recommended Posts

This is a question that is asked time and time again and I'm truly not too sure why it's never been built into ubot as a command, but no more frustration!

 

If you are running version 4.x and need some quick source code to check for the presence of a file before you try to open it, this code is for you!

 

define $does the file exist(#filename_local) {
   set(#filename_path_local, $find regular expression(#filename_local, "^.+?\\\\(?=[^\\\\]*$)"), "Local")
   set(#filename_file_local, $find regular expression(#filename_local, "(?<=\\\\)[^\\\\]*$"), "Local")
   clear list(%filename_checker)
   add list to list(%filename_checker, $get files(#filename_path_local, "No"), "Don\'t Delete", "Local")
   set(#file_found_local, "False", "Local")
   loop($list total(%filename_checker)) {
       if($eval($next list item(%filename_checker) = #filename_file_local)) {
           then {
               set(#file_found_local, "True", "Local")
           }
       }
   }
   return(#file_found_local)
}

What this define does is it will accept a full path to a file with the filename attached. This will be something that we'll have when doing a 'file save'. But if you are going to pull from the file first and then append the file, and it's not there ubot will throw an error.

 

So you give this define something like this c:\docs\bananas\home.txt

 

It will return back to you either a True or False. In other words, True means that the file currently exists or False, it doesn't. Helpful? Not a bad post for the 700th aye?

 

Frank

  • Like 3
Link to post
Share on other sites

Nice code Frank. I've been working on something similar but hit the same snag. If the file path doesn't exist then Ubot will halt with a Script Error: "Could not find a part of the path..."

 

The only work around I can think of is writing a dummy file first to create the path before checking it the file exists. :blink:

 

 

 

Here's my version which now borrows your nice regexes Frank.

 

define $FileExists(#file) {
   set(#path, $find regular expression(#file, "^.+?\\\\(?=[^\\\\]*$)"), "Local")
   set(#filename, $find regular expression(#file, "(?<=\\\\)[^\\\\]*$"), "Local")
   set(#files, $get files(#path, "No"), "Local")
   return($contains("{$new line}{#files}{$new line}", "{$new line}{#filename}{$new line}"))
}
set(#filefound, $FileExists("data\\test.txt"), "Global")

Link to post
Share on other sites
  • 4 months later...

dont know understand why we need regex

my solution is this

 

set(#yourphat, $get files($special folder("Application"), "No"), "Global")

set(#yourfile, "testfile.csv", "Global")

if($contains(#yourphat, #yourfile)) {

then {

set(#found, $true, "Global")

}

else {

set(#found, $false, "Global")

}

}

  • Like 2
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...