I am trying to create a textfile but I get a Path not found error.
What am I doing wrong?
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("\\logs")
var a = fo.CreateTextFile("testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
I am using javascript in IE8. Thanks!
You can try first making sure that the logs directory exists. Next, try including the entire path of the logs directory within the source. This is since the default path of FileSystemObject is the current working directory.
For example, if logs is under C:\logs\
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fo = fso.GetFolder("C:\\logs")
var a = fo.CreateTextFile("testfile.txt", true);
a.WriteLine("This is a test.");
a.Close();
Related
Can I create a folder using Java script program like
var fso, fldr;
fso = new ActiveXObject("Scripting.FileSystemObject");
fldr = fso.CreateFolder("C:\\MyTest");
ActiveXObject is only available within Internet Explorer, also the correct permissions must be set before this is possible.
More info:
http://www.ezineasp.net/post/Javascript-FSO-CreateFolder-Method.aspx
Yes you can create, however it will only run IE browser and you need to perform IE - setting too.
var folder = "C:\\testing";
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FolderExists(folder)) {
fso.CreateFolder(folder);
}
fso=null;
It's not possible with other Browsers! And that's good!
I have a Javascript function that saves JSON data locally, using an ActiveXObject in IE9. It links into FileSystemObject or FSO scripting for file access.
If this Javascript function is run more than once, I get an error in IE debugger: "SCRIPT70: Permission denied" pointing to ts = savefile.OpenAsTextStream(2);
Why will it run just fine the first time, but not after that? My best guess is that something's not being released properly, although I can find no information on MSDN (or here).
Here's the code:
function SaveMyJSON() {
var ts;
var fso = new ActiveXObject("Scripting.FileSystemObject");
var savefilepath = "C:\\MyFolder\\saveFile.json"
var savefile = fso.GetFile(savefilepath);
// open for writing only, value 2, overwriting the previous
// contents of the file
ts = savefile.OpenAsTextStream(2);
var myTestJson = {"id1" : "one", "id2" : "two"};
// copy to json
ts.WriteLine(myTestJson);
ts.Close;
}
The Close method needs empty parenthesis after it, like so:
ts.Close();
Ref here for more info.
I am new in javascript. What I am trying to do is:
write a script which will read an php or txt file, take the info (a number), and replace it on the page like a banner. It will be something like a rating, and the number of this rating will be taking on the local machine.
I need some script which will work with most of browsers.
Thank you for your help!!!
Only Internet Explorer supports this via ActiveXObject. You can access the file system using ActiveXObject and then read the file.
See http://msdn.microsoft.com/en-us/library/2z9ffy99(v=vs.84).aspx
Sample Code:
function ReadFiles()
{
var fso, f1, ts, s;
var ForReading = 1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
// Write a line.
Response.Write("Writing file <br>");
f1.WriteLine("Hello World");
f1.WriteBlankLines(1);
f1.Close();
// Read the contents of the file.
Response.Write("Reading file <br>");
ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
s = ts.ReadLine();
Response.Write("File contents = '" + s + "'");
ts.Close();
}
I create workbook from web page by:
var thisTable = document.getElementById("mytable3").innerHTML;
window.clipboardData.setData("Text", thisTable);
var objExcel = new ActiveXObject ("Excel.Application");
objExcel.visible = true;
var objWorkbook = objExcel.Workbooks.Add();
var objWorksheet = objWorkbook.Worksheets(1);
objWorkbook.Worksheets(1).Activate;
objWorksheet.name = "test";
objWorksheet.Paste;
objWorksheet.columns.autofit;
window.clipboardData.setData("Text","");
objWorkbook.Worksheets(1).SaveAs("%USERPROFILE%\\Desktop\\xxx.xls");
But for objWorkbook.Worksheets(1).SaveAs("%USERPROFILE%\\Desktop\\xxx.xls"); — it doesn't save to desktop and gives this error:
SCRIPT1004: Microsoft Excel cannot access the file 'C:\Users\user\Documents\%USERPROFILE%\Desktop\B612F000'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
From #PA. Answer
I finally got an answer by
//For Expand environment
var wshShell = new ActiveXObject("WScript.Shell");
var userProfile = wshShell.ExpandEnvironmentStrings("%USERPROFILE%\\Desktop\\test.xls");
//For Save
objWorkbook.Worksheets(1).SaveAs(userProfile);
Thak You #PA.
You assume that javascript expands environment variables inside an string. Unfortunately for you, it does not.
you'll need to expand it in your code. Use the Environment property of the wscript.shell object to access any environment variable.
first, access the wscript.shell object. In WSH, use
var wshell = WScript.CreateObject("wscript.shell");
or in the browser use
var wshell = new ActiveXObject("WScript.Shell");
and then
var userProfile = wshell.Environment("USERPROFILE");
or you can expand the variable inside your string by using ExpandEnvironmentStrings method
objWorkbook.Worksheets(1).SaveAs(
wshell.ExpandEnvironmentStrings("%USERPROFILE%\\Desktop\\xxx.xls")
);
I am working on an Asp .net project and i have a textarea in an aspx page, and i am trying to save textarea contents on a file on the server by clicking a button with the following code:
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.CreateTextFile("c:\\temp1\\testfile.txt", true);
a.WriteLine(saveData);
a.Close();
The issue is if the file does not exists , then it creates it. But if it exists it dodes not overwrite it. Any help pls ? (I have to mention that Localy runing the application with visual studio then it rewrites it, but it doesnt works on the published version )
Use rather OpenTextFile() than CreateTextFile(). It also creates non-existing file if needed.
var fso = new ActiveXObject("Scripting.FileSystemObject");
var a = fso.OpenTextFile("c:\\temp1\\testfile.txt",2, true);
a.WriteLine(saveData);
a.Close();