Javascript Append to Text File - jscript

I have several JavaScript files that when run return no errors, but do not do what is coded inside them. This is one of them. Is there something else I should have installed or enabled on the PC running the .JS files?
function WriteDemo()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = CreateObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
f.Write("Hello world!");
f.Close();
f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
r = f.ReadLine();
return(r);
}

According to the MSDN article on FileSystemObject, for JavaScript you should use
new ActiveXObject
instead of
CreateObject
(that's for VB).
function WriteDemo()
{
var fso, f, r;
var ForReading = 1, ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
f = fso.OpenTextFile("c:\\testfile.txt", ForWriting, true);
f.Write("Hello world!");
f.Close();
f = fso.OpenTextFile("c:\\testfile.txt", ForReading);
r = f.ReadLine();
return(r);
}
And, of course, don't forget to call the function. :)
WriteDemo();

Related

export generated excel variable to real excel file

this is how I generated Excel variable:
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
Microsoft.Office.Interop.Excel.Worksheet excelSheet;
Microsoft.Office.Interop.Excel.Range excelCellrange;
excelworkBook = excel.Application.Workbooks.Add(Type.Missing);
excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
//proper data from xml data field to store in excel
// ************************
//generate a data table and fill excell cell
excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1],
excelSheet.Cells[theDataSet.Tables[0].Rows.Count+1, theDataSet.Tables[0].Columns.Count]];
excelCellrange.Font.ThemeColor =
Microsoft.Office.Interop.Excel.XlThemeColor.xlThemeColorAccent1;
excelCellrange.EntireColumn.AutoFit();
Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border.Weight = 3d;
excel.Columns.AutoFit();
excel.DisplayAlerts = false;
excel.Visible = true;
now the question is how save this excel variable to real excel file to specific destination ?
best regards.
this is how I figure out this problem that maybe help someone else:
Microsoft.Office.Interop.Excel.Application excel = new
Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook excelworkBook;
Microsoft.Office.Interop.Excel.Worksheet excelSheet;
Microsoft.Office.Interop.Excel.Range excelCellrange;
excelworkBook = excel.Application.Workbooks.Add(Type.Missing);
excelSheet = (Microsoft.Office.Interop.Excel.Worksheet)excelworkBook.ActiveSheet;
XmlDocument xml = new XmlDocument();
xml.LoadXml(dtAll.Rows[gridEX1.CurrentRow.RowIndex]["Data"].ToString());
StringReader theReader = new StringReader(xml.InnerXml);
DataSet theDataSet = new DataSet();
theDataSet.ReadXml(theReader);
excel.Caption = dtAll.Rows[gridEX1.CurrentRow.RowIndex]["docID"].ToString() + "_" + dtAll.Rows[gridEX1.CurrentRow.RowIndex]["DocTitle"].ToString() + "_" + dtAll.Rows[gridEX1.CurrentRow.RowIndex]["DocDetails"].ToString();
excel.StandardFont = "B Nazanin";
excel.StatusBar = "col count: " + theDataSet.Tables[0].Rows.Count;
for (int i = 0; i < theDataSet.Tables[0].Columns.Count; i++)
{
excel.Cells[1, i + 1] = theDataSet.Tables[0].Columns[i].ColumnName;
excel.Cells[1, i + 1].Font.Color = System.Drawing.Color.Blue;
for (int j = 0; j < theDataSet.Tables[0].Rows.Count; j++)
{
excel.Cells[j + 2, i + 1].Font.Color = System.Drawing.Color.Black;
excel.Cells[j + 2, i + 1] = theDataSet.Tables[0].Rows[j][i].ToString();
}
}
excelCellrange = excelSheet.Range[excelSheet.Cells[1, 1], excelSheet.Cells[theDataSet.Tables[0].Rows.Count + 1, theDataSet.Tables[0].Columns.Count]];
excelCellrange.Font.ThemeColor = Microsoft.Office.Interop.Excel.XlThemeColor.xlThemeColorAccent1;
excelCellrange.EntireColumn.AutoFit();
Microsoft.Office.Interop.Excel.Borders border = excelCellrange.Borders;
border.LineStyle = Microsoft.Office.Interop.Excel.XlLineStyle.xlContinuous;
border.Weight = 3d;
byte[] output = Encoding.UTF8.GetBytes(xml.InnerXml);
string strfn = GroupTitle + ".xlsx";
if (File.Exists(strfn))
{
File.Delete(strfn);
}
FileStream fs = new FileStream(strfn, FileMode.CreateNew, FileAccess.Write);
fs.Write(output, 0, output.Length);
fs.Flush();
fs.Close();
string dest = Global.DocOutputPath.Substring(0, Global.DocOutputPath.Length - 1) + GroupTitle + ".xlsx";
if (File.Exists(dest))
{
DialogResult dialogResult = MessageBox.Show("are you sure about rewrite on file?", "warnning", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
File.Copy(strfn, dest);
File.Delete(dest);
}
else if (dialogResult == DialogResult.No)
{
return;
}
}
else
{
File.Copy(strfn, dest);
}

Find the matching string and replace the next line

I am new to the Groovy.
If the line contains the string then replace the next line in the file.
Found something in google, but I don't want to write a new file.
newPomFile.withWriter { output ->
jars.eachLine{ line ->
if (!skipFlag)output.write(line)
skipFlag = false;
output.write("\n")
if (line.contains(calArtiName.toString().trim())){
output.write(" <version>"+calArtiVer+"</version>")
skipFlag = true;
}
}
}
You may looking for this :
File a = new File ("fileName.txt");
boolean flag = false;
a.text = a.readLines().collect{ l ->
if(flag){
flag=false;
l += " hw r u" //<=== Your code
}
flag = (l.contains('hi')) // <== Your condtion
l
}.join("\n")

how to read excel in silverlight

For .xls files it's working fine,But .xlsx file i am getting following error
"An exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll but was not handled in user code".Let me know how to read .xlsx file.
OpenFileDialog oFile = new OpenFileDialog();
oFile.Filter = "Excel (*.xls)|*.xls";
if (oFile.ShowDialog() == true)
{
FileStream fs = oFile.File.OpenRead();
Workbook book = Workbook.Open(fs);
Worksheet sheet = book.Worksheets[0];
for (int i = sheet.Cells.FirstRowIndex; i < sheet.Cells.LastRowIndex; i++)
{
for (int j = sheet.Cells.FirstColIndex; j < sheet.Cells.LastColIndex; j++)
{
this.textBox1.Text += sheet.Cells[i, j].StringValue;
//this.textBox1.Text += ",";
}
this.textBox1.Text += Environment.NewLine;
}
}
Try flushing your filestream in the loop, then closing the file stream when you're done with it:
OpenFileDialog oFile = new OpenFileDialog();
oFile.Filter = "Excel (*.xls)|*.xls";
if (oFile.ShowDialog() == true) {
FileStream fs = oFile.File.OpenRead();
Workbook book = Workbook.Open(fs);
Worksheet sheet = book.Worksheets(0);
for (int i = sheet.Cells.FirstRowIndex; i <= sheet.Cells.LastRowIndex - 1; i++) {
for (int j = sheet.Cells.FirstColIndex; j <= sheet.Cells.LastColIndex - 1; j++) {
//this.textBox1.Text += ",";
this.textBox1.Text += sheet.Cells(i, j).StringValue;
fs.Flush();
}
this.textBox1.Text += Environment.NewLine;
}
fs.Close();
}
Also here: oFile.Filter = "Excel (*.xls)|*.xls"; you are just filtering to xls files

Count Instance of text and run bat if a number is reached

I am using indexOf to count "200" in a string, I need to find a way to count the 200 as 1 instead of (if string was "200") 3. is there a way to do this? here is what i have so far...
var objIE = new ActiveXObject("InternetExplorer.Application");
WshShell = new ActiveXObject("WScript.Shell");
objIE.Navigate("URL of site");
WScript.Sleep(2000);
var str = objIE.Document.documentElement.innerHTML;
var n = str.indexOf("200");
if (n = 3){
WshShell.Run("killexplorer.bat");
}
else {
WshShell.Run("domanual.vbs")
}
note: the internalHTML has a total of one (2) and five zero's.

How to recursively check for a file's existence and rename if it exist, using groovy?

How do recursively check and rename the file if it already exist by appending some incrementing number?
I wrote the below function but it gives me an exception
org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'E:\Projects\repo1\in_conv1.xml' with class 'java.lang.String' to class 'java.io.File'
Code
//newFilePath = E:\Projects\repo1\old\testcustprops.xml
String newFilePath = checkForExistenceAndRename(newFilePath,false)
private String checkForExistenceAndRename(String newFilePath, boolean flag){
File f = new File(newFilePath)
if(!flag){
if(f.exists()){
//renaming file
newFilePath=newFilePath[0..-5]+"_conv${rename_count++}.xml"
f = checkForExistenceAndRename(newFilePath,false)
}
else
f = checkForExistenceAndRename(newFilePath,true)
}
else
return newFilePath
}
You are trying to do:
f = checkForExistenceAndRename(newFilePath,false)
Where f is a File. But your function returns a String
Not sure if it works or not (I haven't tested your function), but you could try:
private String checkForExistenceAndRename(String newFilePath, boolean flag){
File f = new File(newFilePath)
if(!flag){
if(f.exists()){
//renaming file
newFilePath = newFilePath[0..-5]+"_conv${rename_count++}.xml"
newFilePath = checkForExistenceAndRename(newFilePath,false)
}
else
newFilePath = checkForExistenceAndRename(newFilePath,true)
}
return newFilePath
}
Also, there's no need to use recursion...
Why not just do:
private String getUniqueName( String filename ) {
new File( filename ).with { f ->
int count = 1
while( f.exists() ) {
f = new File( "${filename[ 0..-5 ]}_conv${count++}.xml" )
}
f.absolutePath
}
}

Resources