How to check if a file exists

Discussions and Tech Support related to the iMacros Firefox Add-on, including the built-in Javascript scripting interface for playing .js files.
Forum rules
Before asking a question or reporting an issue:
1. Please review the list of FAQ's.
2. Use the Google search box (at the top of each forum page) to see if a similar problem or question has already been addressed. This will search the entire contents of the forums as well as the iMacros Wiki.
3. We can respond much faster to your posts if you include the following information:

CLICK HERE FOR IMPORTANT INFORMATION TO INCLUDE IN YOUR POST

Answering your own posts (e.g. attempting to "bump" your topic) drops your topic from the list of unanswered threads, so it may actually receive less views.

How to check if a file exists

by access2vivek on Thu Nov 08, 2018 12:01 am

Introduction

With iMacros, we have the capability to work with files. The most commonly used files with iMacros are .txt and .csv format files. When creating a code for ourselves, often times, we require to check if a file exists on the system. If the file exists, we wish to run a certain code and if it does not exist, alert the user and handle the error ourselves accordingly.

Let's have a look at how we can use iMacros to check if a file exists on the system.

Code

iMacros requires you to use "<SP>" instead of spaces in your paths. This has been taken care in the code. Also, iMacros supports back-slash("\") for file path separators. The scripts takes care of that and gives you the freedom to use both forward-slash("/") and backward-slash("\").

The address of the file can be taken as input from the user or it can be saved in a string.

Here are two implementations of a code that checks whether a file specified by a path exists.

Code: Select all
var address=prompt("Enter the file address");
address=address.replace(/ /g,"<SP>");
address=address.replace(/\//g,"\\");

if(iimPlayCode("SET !DATASOURCE "+address)<0)
{
 alert("The File does not exist");
}
else
{
 alert("The File Exists");
}


Code: Select all
var fileExists=true;
var address=prompt("Enter the file address");
address=address.replace(/ /g,"<SP>");
address=address.replace(/\//g,"\\");

if(iimPlayCode("SET !DATASOURCE "+address)<0)
 fileExists=false;

alert("The file Exists = "+fileExists);


How it works
The first step is to get the address of the file. In both these scripts, the absolute path of the file is asked from the user. It can be handled any way that you wish. For demonstration purposes, the input is being taken using the "prompt" method.

Once we get the input, we change all the spaces to "<SP>" using the replace() command. Next, we change all the front-slash("/") to back-slash("\") since iMacros supports only back-slash. It is possible to string both these replace statements together, but just to make it easy to understand, they have been done in two separate lines.

After that, we run a small snippet of code using iimPlayCode() method provided by iMacros. This is the only line of iMacros used for the entire script :lol: . The SET command is used to SET a variable to any specified value. So, we use the in-built DATASOURCE variable using !DATASOURCE. This variable is used by iMacros to keep track of the current file that is being used. So, using the SET command, we set the Data source to the address that we just got.

When iMacros runs any piece of code, it returns a value. If it is successful, it returns 1 and if it is unsuccessful, it returns a negative value which specifies the type of error. You can have a look at iMacros error return codes to handle different errors differently. However, a negative value indicates an error, so that is what has been used here.

We check if the command - "SET !DATASOURCE"+address returns a negative value when passed in iimPlayCode() method. If it is a negative value, we know the file does not exist and we can either execute some code as in the first example or save it in a variable as in the second example. You can simply copy and paste this code anywhere in your JavaScript code with the version of iMacros that supports JavaScript.

Have a wonderful day :D
access2vivek
 
Posts: 35
Joined: Wed Dec 20, 2017 3:00 am

Re: How to check if a file exists

by chivracq on Fri Nov 09, 2018 3:10 am

access2vivek wrote:Introduction

With iMacros, we have the capability to work with files. The most commonly used files with iMacros are .txt and .csv format files. When creating a code for ourselves, often times, we require to check if a file exists on the system. If the file exists, we wish to run a certain code and if it does not exist, alert the user and handle the error ourselves accordingly.

Let's have a look at how we can use iMacros to check if a file exists on the system.

Code

iMacros requires you to use "<SP>" instead of spaces in your paths. This has been taken care in the code. Also, iMacros supports back-slash("\") for file path separators. The scripts takes care of that and gives you the freedom to use both forward-slash("/") and backward-slash("\").

The address of the file can be taken as input from the user or it can be saved in a string.

Here are two implementations of a code that checks whether a file specified by a path exists.

Code: Select all
var address=prompt("Enter the file address");
address=address.replace(/ /g,"<SP>");
address=address.replace(/\//g,"\\");

if(iimPlayCode("SET !DATASOURCE "+address)<0)
{
 alert("The File does not exist");
}
else
{
 alert("The File Exists");
}


Code: Select all
var fileExists=true;
var address=prompt("Enter the file address");
address=address.replace(/ /g,"<SP>");
address=address.replace(/\//g,"\\");

if(iimPlayCode("SET !DATASOURCE "+address)<0)
 fileExists=false;

alert("The file Exists = "+fileExists);


How it works
The first step is to get the address of the file. In both these scripts, the absolute path of the file is asked from the user. It can be handled any way that you wish. For demonstration purposes, the input is being taken using the "prompt" method.

Once we get the input, we change all the spaces to "<SP>" using the replace() command. Next, we change all the front-slash("/") to back-slash("\") since iMacros supports only back-slash. It is possible to string both these replace statements together, but just to make it easy to understand, they have been done in two separate lines.

After that, we run a small snippet of code using iimPlayCode() method provided by iMacros. This is the only line of iMacros used for the entire script :lol: . The SET command is used to SET a variable to any specified value. So, we use the in-built DATASOURCE variable using !DATASOURCE. This variable is used by iMacros to keep track of the current file that is being used. So, using the SET command, we set the Data source to the address that we just got.

When iMacros runs any piece of code, it returns a value. If it is successful, it returns 1 and if it is unsuccessful, it returns a negative value which specifies the type of error. You can have a look at iMacros error return codes to handle different errors differently. However, a negative value indicates an error, so that is what has been used here.

We check if the command - "SET !DATASOURCE"+address returns a negative value when passed in iimPlayCode() method. If it is a negative value, we know the file does not exist and we can either execute some code as in the first example or save it in a variable as in the second example. You can simply copy and paste this code anywhere in your JavaScript code with the version of iMacros that supports JavaScript.

Have a wonderful day :D

Nice Sharing :D , even if it's a good old known Technique/Trick that I already applied (a bit deeper, 3 years ago), but..., oh jee, take it easy with with the 'Lettersize=200', this is ridiculously big, not nice at all to read... :cry:

=> Mini-Reaction only as your Post is not very "pleasant" to read, but your "Statement": [if (rectCode < 0) => File does not exist] is not "completely" correct (see..., I can use Size=200 also... :wink: ), only the Contrary is True..., which will be "good enough" I guess in 95% of the "Cases" for most Users but you'll be missing some "Cases"..., think about it... :idea:

=> You could better check on the exact Error CodeS (yep, plural...!) => '-930' etc..., or the Length of the Var. which makes it also usable in pure '.iim' and not only with '.js'..., and makes it also usable for other Formats than Text Files, like Images for example prior to uploading them to some Site... Your Script wouldn't work with Images..., nor for '.PDF' I reckon, prior to downloading or uploading...

But OK, I guess your Script is already perfect for the "Principle", other Users can adapt it a bit if they need it to a bit more "precise" and if they "fall" in the 5% Usecases not covered by the "<0" Implementation... :wink:

There are also 2 other Techniques on listing the Folder Content using the old DOS Command 'dir' or its automatic Equivalent from the Browser through the 'file:///' Protocol when listing a Directory, + similar to your Technique on all '!FOLDER_xxx' Vars that all 3 can be used directly in pure '.iim'. :idea:
(And everything that works in '.iim' can also be converted to '.js' of course... :wink: )
- (F)CIM = (Full) Config Info Missing: iMacros + Browser + OS with all 3 Versions...
- I usually don't even read the Question if that (required) Info is not mentioned...
- Script & URL usually help a lot for a more "educated" Help...
chivracq
 
Posts: 7712
Joined: Sat Apr 13, 2013 6:07 am
Location: Amsterdam (NL)


Return to iMacros for Firefox

Who is online

Users browsing this forum: No registered users and 11 guests

-->