Frank 177 Posted February 24, 2012 Report Share Posted February 24, 2012 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 3 Quote Link to post Share on other sites
Legend 181 Posted February 24, 2012 Report Share Posted February 24, 2012 Cool! Thanks Frank! Looking forward to #800... http://ubotstudio.com/forum/public/style_emoticons/default/smile.gif Quote Link to post Share on other sites
Hung Le 0 Posted February 25, 2012 Report Share Posted February 25, 2012 Thanks Frank! Cool tips! This is what I was looking for. In Winautomation it's so much easier. Quote Link to post Share on other sites
GazHawkins 0 Posted February 25, 2012 Report Share Posted February 25, 2012 Thanks Frank, That is exactly what I was looking for. Gaz Quote Link to post Share on other sites
Chainsaw 9 Posted February 25, 2012 Report Share Posted February 25, 2012 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. 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") Quote Link to post Share on other sites
Frank 177 Posted July 11, 2012 Author Report Share Posted July 11, 2012 here's funny for you. I needed this code again and searched the forum - voila! The code I posted months back, lol. Frank Quote Link to post Share on other sites
blumi40 222 Posted July 11, 2012 Report Share Posted July 11, 2012 dont know understand why we need regexmy 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") }} 2 Quote Link to post Share on other sites
warriorfullights 0 Posted July 12, 2012 Report Share Posted July 12, 2012 Great share of cool stuff. Thanks a lot. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.