473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Java script for copying files

Please help me in writing a java script to Copy a file from source to destination shared folder.
Sep 24 '08 #1
7 10437
acoder
16,027 Expert Mod 8TB
This is not possible with JavaScript, though you could use browser-specific code to achieve client-side copying.
Sep 24 '08 #2
rnd me
427 Expert 256MB
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
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
427 Expert 256MB
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
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
427 Expert 256MB
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
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

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

Similar topics

8
by: Sticks | last post by:
ok... im not quite sure how to describe my problem. i have a php script that runs through my entire php site and writes the resulting output to html files. this is necessary as the nature of the...
3
by: Bengan | last post by:
Hi, I have made an java app and i want to do an install wizard for that. What do i need to do to make my java app work at another persons system that doesnt have anything with java installed. Is...
2
by: Stefano Bianchi | last post by:
Ciao, I need to start a java form compiler from linux. The script that comes with the program simply says (after some checks): java -classpath Packager.jar:Filler.jar -jar EPT.jar However,...
21
by: BlackHawke | last post by:
My name is Nick Soutter, I own a small game development company (www.aepoxgames.net) making our first game (www.andromedaonline.net) in java. I am writing because we are having a very...
2
by: enclume42 | last post by:
Hello, I am a pure Linux guy. I wrote a Java program that is going to be used by some Windows people, and I wish the startup of the program to be as simple as possible, namely to double-click on...
6
by: harish | last post by:
Hi, We are developing one application for smartphone. In this we are parsing XMLs and rendering it as UI elements using smartphone SDK. Everything is working fine. Now one more requirement has...
6
by: Rhino | last post by:
I am writing Java UDFs using DB2 V8.2 (Fixpack 8) on Windows XP. I would like to create some common code classes that are visible to the UDFs on my system but I'm not having a lot of luck so far....
34
by: kpg | last post by:
Hello all, I have an asp.net web application with tons of Java script files. I would like to protect the Java Script somehow so it can't be seen by a remote user. I found several 3rd party...
1
by: ajmerasunny | last post by:
Hi All, I have a java web application in which I need to read a configuration file(WebAgent.conf) that has the reference of another configuration file(SmHost.conf). I am placing both these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.