1 Replies - 4078 Views - Last Post: 27 November 2012 - 10:29 PM Rate Topic: -----

#1 badri2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 27-November 12

when i run code in visual studio i get error [identifier expected\

Posted 27 November 2012 - 10:13 PM

public object MyFunction()
{
	objFSO = Interaction.CreateObject("Scripting.FileSystemObject");
	objDictionary = Interaction.CreateObject("Scripting.Dictionary");
	objNetwork = Interaction.CreateObject("Wscript.Network");
	strLogFolder = "c:\\Logs";
	strInputfile = "C:\\Logs\\Comp.txt";
	strLogfile = "c:\\Logs\\listlocaladmin" + System.DateTime() + ".log";
	strComputer = objNetwork.ComputerName;
	const object ForReading = 1;
	 // ERROR: Not supported in C#: onerrorStatement

	if (ReportFileStatus(strInputfile) == "False") {
		Wscript.Echo("Input file not found");
		WScript.Quit();
	}

	if (ReportFolderStatus(strLogFolder) == false) {
		objFSO.CreateFolder(strLogFolder);
	}
	if (ReportFileStatus(strLogfile) == "False")
          {
		logs = objFso.CreateTextFile(strLogfile);
		logs.close();
	   }

	objTextFile = objFSO.OpenTextFile(strInputFile, ForReading);
	i = 0;

	while (!(objTextFile.AtEndOfStream)) {
		strNextLine = objTextFile.Readline;
		if (!(Strings.Left(strNextLine, 1) == "#")) {
			objDictionary.Add(i, strNextLine);
			i = i + 1;
		}
	}

	foreach ( objItem in objDictionary) {
		StrComputerName = objDictionary.Item(objItem);
		if (DeadOrAlive(StrComputerName) == "True") {
			objGroup = Interaction.GetObject("WinNT://" + StrComputerName + "/Administrators,group");
			foreach ( objUser in objGroup.Members) {
				members = members + ";" + objUser.Name;
			}
			logs = objFso.OpenTextFile(strLogfile, 8);
			logs.writeline(" " + now() + ";" + "Alive;" + StrComputerName + members);
			logs.close();
			members = " ";
		} else {
			logs = objFso.OpenTextFile(strLogfile, 8);
			logs.writeline(" " + now() + ";" + "Dead;" + strComputerName);
			logs.close();
		}
	}
}



public object ReportFileStatus(System.Object filespec)
{
	object functionReturnValue = null;
	object fso = null, msg = null;
	objfso = Interaction.CreateObject("Scripting.FileSystemObject");
	if ((objfso.FileExists(filespec))) {
		functionReturnValue = true;
		return;
	} else {
		functionReturnValue = false;
		return;
	}
	return functionReturnValue;
}


public object DeadOrAlive(System.Object ComputerName)
{
	object functionReturnValue = null;
	objShell = Interaction.CreateObject("Wscript.Shell");
	objScriptExec = objShell.Exec("ping -n 2 -w 1000 " + ComputerName);

	if (Strings.InStr(objScriptExec.StdOut.ReadAll, "Reply") > 0) {
		functionReturnValue = true;
	} else {
		functionReturnValue = false;
	}
	return functionReturnValue;
}


public object ReportFolderStatus(System.Object folderspec)
{
	object functionReturnValue = null;
	object fso = null;
	fso = Interaction.CreateObject("Scripting.FileSystemObject");
	if ((fso.FolderExists(folderspec))) {
		functionReturnValue = true;
		return;
	} else {
		functionReturnValue = false;
		return;
	}
	return functionReturnValue;
}

This post has been edited by tlhIn`toq: 27 November 2012 - 10:18 PM
Reason for edit:: code tags


Is This A Good Question/Topic? 0
  • +

Replies To: when i run code in visual studio i get error [identifier expected\

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5632
  • View blog
  • Posts: 12,078
  • Joined: 02-June 10

Re: when i run code in visual studio i get error [identifier expected\

Posted 27 November 2012 - 10:29 PM

First: Since none of the many variables at the start of the method aren't defined by type you must have them as class-wide variables. A couple is ok - this many is probably a bad start.

Second: This is a horrible way to do logging. Mainly because you have the function micromanaging the logging. Make a separate logging class that has a single purpose: To log. Give it a method like AddEntry(string message) that other classes would use. The other classes don't need to know where the file is written, if it even is a file or a database entry, or an XML file... That's the job of the logging class not the parent program's logic.

I can't beleive you hard-coded paths like that. What happens if there is no "C:" drive or the user permissions don't allow writing to the root level like that?

Third: Wstript.echo??? WTF is this and where did you copy it from? It looks like someone's attempt to take 1970-1990's scripting or maybe something from Win95 and port it into modern C#.


As for the error - hard to say since you just dumped a bunch of code and didn't say what line the error was on.



badri2: What this shows us is that you aren't familiar with breakpoints and how to debug your own code.


tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated NOV 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1