-
Batch file looping
I have a console program that I am trying ot execute with a batch file. When you click on the batch file a command windows comes up and keeps looping displaying the contents of the batch file until you close the window. You can execute th program manually from within a command window. The only problem is with the using it in a batch file.
I am lost. This is my second C# app so any help would be appriciated.
Code:
using System;
using System.IO;
using System.Collections;
using System.Data;
namespace autoBackUp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
Console.WriteLine("Please enter a path");
string filePath = Console.ReadLine();
//string filePath = "C:\\backUpGui\\guiSettings.osf c:\\tomTets\\new_****";
//Splits file path into to sections the first key is the settings file and the second is
string [] filePathSplit = filePath.Split(' ');
//Opens settings file for reading
StreamReader sr = File.OpenText(filePathSplit[0]);
//Reads setting file
string fileContents = sr.ReadToEnd();
//Splits the file contents into an array
string [] fileContentsSplit = fileContents.Split('\n');
//Starts a new Arraylist to be filled with the file names
ArrayList fileArray = new ArrayList();
if (Directory.Exists(filePathSplit[1]) == true)
{//Check for the directory to see if it exists
} else {
//If the directory does not exist create it
Directory.CreateDirectory(filePathSplit[1]);
}//End of if directory exists
foreach(string fileName in fileContentsSplit)
{//Split each file path into an array of names and extensions
if (fileName != "")
{
//Split string at \ for measuring
string [] filePathSplit2 = fileName.Split('\\');
//Get the length of the last key in the array
int lastKeyLength = (filePathSplit2[filePathSplit.Length - 1].Length);
//Find the position of the last \
int filePos = filePath.IndexOf('\\', ((fileName.Length) - (lastKeyLength + 1)));
//Get a the name and extension
string fileNameAndExt = fileName.Substring(filePos);
//Add to the array containing the names and extensions
fileArray.Add(fileNameAndExt);
}//End of if
}//End of foreach
//Define the new file path
string newFileName = filePathSplit[1];
//Convert the names and extension array into a useable string array
string [] fileNameConvert = (string []) fileArray.ToArray(typeof(string));
//Start loop to copy files that were selected
for (int i = 0; i < fileContentsSplit.Length; i++)
{
if (fileContentsSplit[i] != "")
{
try
{//Copies each file deined in batch file
File.Copy(fileContentsSplit[i], newFileName + fileNameConvert[i], true);
}//End of try
catch (System.IO.IOException)
{
return;
}//End of catch
}//End foreach that copies files
}//End if for copying
//Outputs each file for name that was copied. Mainly for debugging.
foreach (string files in fileContentsSplit)
{
if (files != "")
{
Console.WriteLine(files);
}
}//End of forEach
}
}
}