By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,739 Members | 2,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,739 IT Pros & Developers. It's quick & easy.

Java script for copying files

P: 4
Please help me in writing a java script to Copy a file from source to destination shared folder.
Sep 24 '08 #1
Share this Question
Share on Google+
7 Replies


acoder
Expert Mod 15k+
P: 16,027
This is not possible with JavaScript, though you could use browser-specific code to achieve client-side copying.
Sep 24 '08 #2

rnd me
Expert 100+
P: 427
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.
Sep 25 '08 #3

P: 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.FileSystemObject");
f = myObject.GetFile("C:\\Complaints.txt");
f.Copy("C:\\Sammy\\");

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.
Sep 25 '08 #4

rnd me
Expert 100+
P: 427
Expand|Select|Wrap|Line Numbers
  1.  
  2. function runCode(){
  3.   var myObject, f;
  4.   myObject = new ActiveXObject("Scripting.FileSystemObject");
  5.   f = myObject.GetFile("C:\\Complaints.txt");
  6.   if(!f){ return alert("File Not Found!"); }
  7.   f.Copy("C:\\Sammy\\");
  8. }
  9.  
  10. runCode();
  11.  
  12.  
wrapping the code in a function allows more control over the code. and allows you to bail out without further execution.
Sep 25 '08 #5

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

---------------------------------------------------------------------------------
Expand|Select|Wrap|Line Numbers
  1.  SPATH = "C:\\Complaints.txt";
  2.  DPATH = "C:\\Sammy\\";
  3.  
  4. XBOT_COPY(SPATH,DPATH);
  5.  
  6. //----Function to copy from one location to another
  7. function XBOT_COPY(SRC_PATH,DST_PATH)
  8. {
  9.  
  10.  try {
  11.  
  12.   var myObject, f;
  13.         myObject = new ActiveXObject("Scripting.FileSystemObject");
  14.         f =   myObject.GetFile("C:\\Complaints.txt");
  15.         f.Copy("C:\\Sammy\\");
  16.  
  17. f.close();
  18. myObject.close();
  19. }
  20.  
  21.  
  22. catch(err) {
  23.   var dt = new Date();
  24.   var FER = new ActiveXObject("Scripting.FileSystemObject");
  25.   var fFile = FER.OpenTextFile("C:\\Sammy\\Log\\xBotError.log",8, true,-1);
  26.   fFile.write(err+": "+err.description+" -- "+dt);
  27.   fFile.WriteBlankLines(1);
  28.   fFile.close();
  29.   fFile = null;
  30.   FER = null;
  31.   dt = null;
  32.  }  
  33.  finally {
  34.   f = null;
  35.  myobject = null;
  36.  }
  37. }
  38.  
-------------------------------------------------------------------------

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







Expand|Select|Wrap|Line Numbers
  1.  
  2. function runCode(){
  3.   var myObject, f;
  4.   myObject = new ActiveXObject("Scripting.FileSystemObject");
  5.   f = myObject.GetFile("C:\\Complaints.txt");
  6.   if(!f){ return alert("File Not Found!"); }
  7.   f.Copy("C:\\Sammy\\");
  8. }
  9.  
  10. runCode();
  11.  
  12.  
wrapping the code in a function allows more control over the code. and allows you to bail out without further execution.
Sep 25 '08 #6

rnd me
Expert 100+
P: 427
you dont need to log anything.

here are some cleaner functions i wrote a while back:
Expand|Select|Wrap|Line Numbers
  1.  
  2. function load(filename)  //IE FSO file Loader
  3. {  
  4.           var fso, file;
  5.           fso = new ActiveXObject('Scripting.FileSystemObject');
  6.           file = fso.OpenTextFile(filename, 1, false);
  7.           var name = file.readAll();
  8.           file.Close();
  9.     return name;
  10.  
  11.  
  12. function save(filename, sData) //IE FSO file Saver
  13. {  
  14.           var fso, file;
  15.           fso = new ActiveXObject('Scripting.FileSystemObject');
  16.           file = fso.CreateTextFile(filename, 2, false);
  17.           file.write(sData);
  18.           file.Close();
  19.     return file;
  20.  
  21. function append(filename, sData) {
  22.   var oldData= load(filename);
  23.       return save(oldData+sData);
  24.  
  25.  
Sep 25 '08 #7

P: 4
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.
Sep 25 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.