Announcement

Collapse
No announcement yet.

Creating a new directory and file using File Object

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating a new directory and file using File Object

    Hello,

    Is there anything wrong with creating a new directory and new file using the File Object, but, doing this creation in the declarations section at the top of my EFS code? this is the code Im using:

    // Declarations and Global Variables

    var a = new File ("Gold Strategy 1");
    a.mkDir;

    var b = new File ("Gold Strategy 1/EntryPrice.txt");

    // Declarations and Global Variables


    funtion preMain() {
    }

    function main() {
    return null;
    }

    The reason I wanted to put creating new directory and new file code in the declarations section of my EFS code is that I only wanted to create the new directory and file once, ie when the EFS code initially loads up.

    Is it ok to do it this way? Will my code still be stable doing it this way? or can you see problems with doing it this way?

    A second question is: If I only want to do some action once like read from a file, is it always best to do this action at the top of my EFS code which is outside the main() and preMain() function loops?

    Please excuse my ignorance, as I am new to JavaScipt and not a expert programmer, but I have done some basic programming in other platforms for the last 5 years, and I have read as much as I can in the e-signal resources materials like EFS KnowledgeBase, manuals, etc.

    Thanks again for any help.

    Regards

    Rod
    Last edited by Rocket130713; 08-22-2008, 05:54 AM.

  • #2
    Hi Rod,

    In lieu of commenting on your code, there are working examples with comments in my fileshare here

    Comment


    • #3
      Hello,

      Thanks Steve for your comments and reply. In your file share section, under "file operation" then under "creating directories 10 19 2004", i see that in the EFS you created you typed the following code:

      var f1 = new File ("TRADEFILES");
      if ( ! f1.exists() ) f1.mkDIr() ;

      My question is: I thought that checking to see if the file exists using f1.exists() actually checks to see if the file exists, and does not check to see if the directory exists, and TRADEFILES is a directory and not a file. Is this correct? (i thought a directory is not the same as a file?).

      Thanks for your help and advice. If anyone else would like to comment or reply, including Alexis, that would also be very much appreciated.

      Regards

      Rod

      Comment


      • #4
        Question about the "new File" and "f.exists" commands?

        Hello,

        I have 2 questions about using File Objects:

        (a) If I put this code i.e. f=new File("MyFile.txt"); within the function main() block/loop, does this mean that the EFS attempts to create a new file called "MyFile.txt" on every new tick? Or does the EFS only attempt to create the new file once?

        (b) If I place this code i.e.....

        var g = new File("DirectoryG");
        if ( !g.exists() ) g.mkDir() );

        ......within the function main() loop, does the !g.exists() command check to see if a directory called "DirectoryG" exists? Or does the exists() function only check to see if a file exists and not a directory? I'm assuming that a file is not the same as a directory.

        Thanks in advance for any help or answers. I really appreciate it.

        Regards,

        Rod

        Comment


        • #5
          Rod,

          Before I answer please return the favor and extend me the courtesy of running the efs and indicate if it works? If it does not, I may have to re-structure the directory strings.

          Comment


          • #6
            hi steve,

            yes it does work.

            thank you for your help. much appreciated.

            regards

            rod

            Comment


            • #7
              Hi rod,

              Your welcome. Thanks for the feedback. Communication in a forum environment is not face to face and can be difficult if we assume things.

              Regarding your previous questions, I'll try and answer them as best I can.

              All of the functions (methods) that are used for file and folder manipulation are imbedded in an Object Constructor entitled "File". The only way these functions (methods) are accessible is you create a instance of the File object. Another way to think of a file object is a repository of file and folder commands.



              (a) If I put this code i.e. f=new File("MyFile.txt"); within the function main() block/loop, does this mean that the EFS attempts to create a new file called "MyFile.txt" on every new tick? Or does the EFS only attempt to create the new file once?

              This line of code:

              f=new File("MyFile.txt");

              Creates a file object which will be represented by the variable 'f'. When the methods of the new file object are called to interface with the file/folder structure in our computers, the name that is used for the Windows file interface is "MyFile.txt". All of the methods act on the file or folder with the name provided when the file object is created. The object methods are accessed using the dot format, eg:

              "object.method()"

              In your line of code, the efs will create a brand new file object every tick (very inefficient and not advised). Since you are only creating a file object, and not calling the objects methods, you are not creating a file every tick.


              (b) If I place this code i.e.....

              var g = new File("DirectoryG");
              if ( !g.exists() ) g.mkDir() );

              ......within the function main() loop, does the !g.exists() command check to see if a directory called "DirectoryG" exists? Or does the exists() function only check to see if a file exists and not a directory? I'm assuming that a file is not the same as a directory.

              This line of code:

              var g = new File("DirectoryG");

              What you are doing here is creating a new file object, represented by a local variable 'g' which will use the name "DirectoryG" when interfacing with the Windows file interface.


              This line of code:

              if ( !g.exists() ){g.mkDir();}

              This is a shortcut for the following conditional.

              if (g.exists()==false ){g.mkDir();}

              Where g.exists() is a method of the 'g' file object that looks to the Windows file interface for something named "DirectoryG". If there is something there, the g.exists() method returns a true Boolean value. If not, the g.exists() method returns a false Boolean value. The conditional will evaluate to true if the method returns a false Boolean.

              This method of the 'g' object:

              {g.mkDir();}

              Will be executed if the previous conditional is true. The file object method will create a new folder if there was none previously.

              I hope this helps.

              Comment


              • #8
                hi steve,

                thanks so much for your great explantion and help regarding files and directories.

                FWIW, i now place the lines of code that create a diectory and files at the top of my EFS code, just after my declarations section, which is outside the main() loop. I tried putting this code inside the main() loop and got error messages. So as this code is at the top of my efs code, it only gets executed once when the program is loading up, which is good as i only want to create the file and directory once, and only if it does not exist in the first place.

                Steve, do you also place the lines of code that create directories and files outside the main() loop ?

                Thanks in advance. Really appreciate your help and advice.

                regards

                Rod

                Comment


                • #9
                  hi rod,

                  Your most welcome.

                  Originally posted by Rocket130713

                  ...
                  Steve, do you also place the lines of code that create directories and files outside the main() loop ?

                  Thanks in advance. Really appreciate your help and advice.

                  regards

                  Rod
                  I have accessed and manipulated files both inside and outside of the main() loop. Either way, I usually encapsulate the lines of code in a function and call the function when I want to execute the file access.

                  If I access it inside the main function, I'll encapsulate the function call (to my file operation function) in a conditional statement to ensure it is not called every tick.

                  If I access my file operation function outside of the main function, there can be a gotcha there. Since many efs objects and there associated efs methods are undefined before the main function is called. Trying to use them in this case will result in an error. (this is unrelated to your error that occurred within the main() function).

                  If you need any more help on this, or on any other efs coding issue, please try and post a code example that demonstrates the issue.

                  Here are some of my thoughts on posting code examples, not directed at you (no offense intended), but certainly pertinent to questions on efs coding help.

                  While creating an efs to post that duplicates an issue may take two or three times the effort and time on your part, there is a significant payoff that may not be apparent.

                  It does take a lot of time to answer questions on the forum regarding efs issues, and without code examples, some can take hours to replicate the problem (if at all). Since 99.9% of the people on this forum either do not have the expertise, available time or inclination to respond, your question will either not be answered or the answer will be brief and/or delayed.

                  If you provide an efs example, it becomes a win-win situation. First, your problem will be diagnosed much quicker because the example can be run by other users and guesswork and assumptions are reduced significantly. Sometimes the problem is recognized within minutes by just reading the code. More importantly, at least in my opinion, is that it also saves a ton of time for the moderators and members who answer the majority of difficult forum questions. Finally, you've increased ten-fold the number of forum members that can now address the problem and provide substantive help.



                  Hope this helps and makes sense.

                  Comment

                  Working...
                  X