Java script for copying files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Samita Ghose
    New Member
    • Sep 2008
    • 4

    Java script for copying files

    Please help me in writing a java script to Copy a file from source to destination shared folder.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    This is not possible with JavaScript, though you could use browser-specific code to achieve client-side copying.

    Comment

    • rnd me
      Recognized Expert Contributor
      • Jun 2007
      • 427

      #3
      if the remote folder is accessible via http, you can do it using webDAV, or by loading and re-saving with ajax.

      if it's on a file path, only IE can do it.

      Comment

      • Samita Ghose
        New Member
        • Sep 2008
        • 4

        #4
        Yah I could do it however using MS J script.
        I am copying a file from one source path to another destination path.

        Here is the code:

        var myObject, f;
        myObject = new ActiveXObject(" Scripting.FileS ystemObject");
        f = myObject.GetFil e("C:\\Complain ts.txt");
        f.Copy("C:\\Sam my\\");

        I just need to include error handling in this code.Like it will check for source code ,and if not avaiable then it will give alert that "File not found".
        can you help me in inserting the error handling codes in my script?

        Thanks.

        Comment

        • rnd me
          Recognized Expert Contributor
          • Jun 2007
          • 427

          #5
          Code:
          function runCode(){
            var myObject, f;
            myObject = new ActiveXObject("Scripting.FileSystemObject");
            f = myObject.GetFile("C:\\Complaints.txt");
            if(!f){ return alert("File Not Found!"); }
            f.Copy("C:\\Sammy\\");
          }
          
          runCode();
          wrapping the code in a function allows more control over the code. and allows you to bail out without further execution.

          Comment

          • Samita Ghose
            New Member
            • Sep 2008
            • 4

            #6
            Thanks..That will work. However I have added try and catch function and modified the code as follows:

            ---------------------------------------------------------------------------------
            Code:
             SPATH = "C:\\Complaints.txt";
             DPATH = "C:\\Sammy\\";
            
            XBOT_COPY(SPATH,DPATH);
            
            //----Function to copy from one location to another
            function XBOT_COPY(SRC_PATH,DST_PATH)
            {
            
             try {
            
              var myObject, f;
                    myObject = new ActiveXObject("Scripting.FileSystemObject");
                    f =   myObject.GetFile("C:\\Complaints.txt");
                    f.Copy("C:\\Sammy\\");
            
            f.close();
            myObject.close();
            }
            
            
            catch(err) {
              var dt = new Date();
              var FER = new ActiveXObject("Scripting.FileSystemObject");
              var fFile = FER.OpenTextFile("C:\\Sammy\\Log\\xBotError.log",8, true,-1);
              fFile.write(err+": "+err.description+" -- "+dt);
              fFile.WriteBlankLines(1);
              fFile.close();
              fFile = null;
              FER = null;
              dt = null;
             }  
             finally {
              f = null;
             myobject = null;
             }
            }
            -------------------------------------------------------------------------

            This is working and creating a logfile in the path specified - C:\\Sammy\\Log\ \xBotError.log" , if there is any mismatch in filename or path name.
            However if everything is fine also,it is working i.e copying complaints.txt file from "C:\\Complaints .txt" to "C:\\Sammy\ \".
            Still in logfile I am getting the log as [object Error]: Object doesn't support this property or method -- Thu Sep 25 14:43:45 UTC+0530 2008.

            Is this log mandatory or there is any problem in my code?And how can I neglect this object error.
            Can anyone explain?

            Thanks.

            Samita







            Originally posted by rnd me
            Code:
            function runCode(){
              var myObject, f;
              myObject = new ActiveXObject("Scripting.FileSystemObject");
              f = myObject.GetFile("C:\\Complaints.txt");
              if(!f){ return alert("File Not Found!"); }
              f.Copy("C:\\Sammy\\");
            }
            
            runCode();
            wrapping the code in a function allows more control over the code. and allows you to bail out without further execution.
            Last edited by acoder; Sep 25 '08, 11:24 AM. Reason: Please use [code] tags

            Comment

            • rnd me
              Recognized Expert Contributor
              • Jun 2007
              • 427

              #7
              you dont need to log anything.

              here are some cleaner functions i wrote a while back:
              Code:
              function load(filename)  //IE FSO file Loader
              {  
                        var fso, file;
                        fso = new ActiveXObject('Scripting.FileSystemObject');
                        file = fso.OpenTextFile(filename, 1, false);
                        var name = file.readAll();
                        file.Close();
              	return name;
              } 
              
              
              function save(filename, sData) //IE FSO file Saver
              {  
                        var fso, file;
                        fso = new ActiveXObject('Scripting.FileSystemObject');
                        file = fso.CreateTextFile(filename, 2, false);
                        file.write(sData);
                        file.Close();
              	return file;
              } 
              
              function append(filename, sData) {
                var oldData= load(filename);
                    return save(oldData+sData);
              }

              Comment

              • Samita Ghose
                New Member
                • Sep 2008
                • 4

                #8
                Yah this will work if I need to append my file.
                But my exact requirement is like :
                I have a file in one location.
                I need to copy it to another location.
                And it should include error handling concepts using try -catch-finally.

                The code should be similar to the one I had written using try catch and generating logfiles.

                But my doubt is why is it generating the statement
                "[object Error]: Object doesn't support this property or method -- Thu Sep 25 14:43:45 UTC+0530 2008"
                even when everything is going fine and the file is being copied to the desired location.

                Can this Object error be avoided?
                My code should have the format as I have written.

                I hope I am being able to explain my needs. Thanks a lot for the codes however.

                Please just let me know if tht object error can be avoided in my logfile.

                Comment

                Working...