3 Replies - 700 Views - Last Post: 11 May 2011 - 07:33 AM Rate Topic: -----

#1 bediyev   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 28-April 11

A function that used instead of multiple functions

Posted 11 May 2011 - 06:47 AM

Hi everyone.

i have a question. I wrote the code below. This code does some works with a directory named UNV.
        public void UNV()
        {
            string str3 = ConfigurationSettings.AppSettings["AddressofWinrarProgram"];
            string str4 = ConfigurationSettings.AppSettings["DestOfArchivedUNV"];
            string str5 = ConfigurationSettings.AppSettings["AddressOfFile2BeArchivedUNV"];
            string str6 = ConfigurationSettings.AppSettings["ExtractFolderUNV"];
            string xaz = ConfigurationSettings.AppSettings["DestOfArchivedXAZ"];
            string xaz1 = ConfigurationSettings.AppSettings["AddressOfFile2BeArchivedXAZ"];
            string xaz2 = ConfigurationSettings.AppSettings["ExtractFolderXAZ"];
            //for unv
            System.Diagnostics.Process astart = new System.Diagnostics.Process();
            astart.StartInfo.FileName = str3;
            astart.StartInfo.Arguments = "a -r -ep1 " + str4 + " " + str5 + " " + str6 + "\\" + mname + "\\" + day + "\\";
            astart.StartInfo.UseShellExecute = false;
            astart.StartInfo.RedirectStandardOutput = true;
            astart.Start();
            astart.WaitForExit();

            System.Diagnostics.Process estart = new System.Diagnostics.Process();
            estart.StartInfo.FileName = str3;
            estart.StartInfo.Arguments = "x -ac -o+ " + str4 + " *.* " + str6 + "\\" + "database" + "\\" + "bank_day" + "\\" + year + "\\" + mname + "\\" + day + "\\";
            estart.StartInfo.UseShellExecute = false;
            estart.StartInfo.RedirectStandardOutput = true;
            estart.Start();
            estart.WaitForExit();

            string[] files = System.IO.Directory.GetFiles(str6 + "\\database\\bank_day\\2011\\may\\4", "*");
            foreach (string file in files)
            {
                string dest = str6 + "\\database\\Publ_dbf\\" + System.IO.Path.GetFileName(file);
                System.IO.File.Copy(file, dest, true);
            }
            string[] dirs = System.IO.Directory.GetDirectories(str6 + "\\database\\bank_day\\2011\\may\\4", "*");
            foreach (string dir in dirs)
            {
                string destd = str6 + "\\database\\bank_dbf\\" + System.IO.Path.GetDirectoryName(dir);
                System.Diagnostics.Process copydir = new System.Diagnostics.Process();
                copydir.StartInfo.FileName = str3;
                copydir.StartInfo.Arguments = "a -r -ep1 " + str6 + "\\database\\folders.rar " + dir;
                copydir.StartInfo.UseShellExecute = false;
                copydir.StartInfo.RedirectStandardOutput = true;
                copydir.Start();
                copydir.WaitForExit();
            }
            System.Diagnostics.Process extdir = new System.Diagnostics.Process();
            extdir.StartInfo.FileName = str3;
            extdir.StartInfo.Arguments = "x -ac -o+ " + str6 + "\\database\\folders.rar " + str6 + "\\database\\bank_dbf";
            extdir.StartInfo.UseShellExecute = false;
            extdir.StartInfo.RedirectStandardOutput = true;
            extdir.Start();

            extdir.WaitForExit();

            System.IO.File.Delete(str6 + "\\database\\folders.rar");
        }




and i take the addresses from a configuration file :
	<!--This part is for UNV folder -->
    <add key="DestOfArchivedUNV" value="C:\\ARDEF\\DEF\\T24_AR\\UNV.rar"/>
    <add key="AddressOfFile2BeArchivedUNV" value="C:\\ARDEF\\DEF\\T24_AR\\UNV\\"/>
    <add key="ExtractFolderUNV" value="C:\\1"/>



this works good, but what i want is i have many folders like UNV folder and it s needed to do the same operations to these folders as UNV. i don't want to write function for each folder to do these tasks. I want to ask that is it possible to write a general function that does these works for all these folders??
Thanks anyway..

Is This A Good Question/Topic? 0
  • +

Replies To: A function that used instead of multiple functions

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: A function that used instead of multiple functions

Posted 11 May 2011 - 06:58 AM

Write the function so it takes parameters then build paths from that...
Of course that assumes you've built a logical and consistent folder system that this could be applied to. If you've made folders all willy-nilly it could still be done but becomes more complex.

public void UNV(string passed)
{
    string str3 = ConfigurationSettings.AppSettings["AddressofWinrarProgram"];
    string str4 = ConfigurationSettings.AppSettings["DestOfArchived" + passed];
    string str5 = ConfigurationSettings.AppSettings["AddressOfFile2BeArchived" + passed];
    string str6 = ConfigurationSettings.AppSettings["ExtractFolder" + passed"];
    string xaz = ConfigurationSettings.AppSettings["DestOfArchivedXAZ"];
    string xaz1 = ConfigurationSettings.AppSettings["AddressOfFile2BeArchivedXAZ"];

Was This Post Helpful? 1
  • +
  • -

#3 NotarySojac   User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: A function that used instead of multiple functions

Posted 11 May 2011 - 07:11 AM

I don't use the ConfigurationsSettings in my apps, but maybe you could have it like:

public void FolderOperation(string DestOfArchivedUNV, string AddressOfFile2BeArchivedUNV, string ExtractFolderUNV)
{
   <replace your system config things with the appropriate strings above>
}



Then, call your UNV like so:

FolderOperation("C:\\ARDEF\\DEF\\T24_AR\\UNV.rar", 
   "C:\\ARDEF\\DEF\\T24_AR\\UNV\\",
   "C:\\1");

FolderOperation("C:\\ARDEF\\DEF\\T24_AR\\BLA.rar", 
   "C:\\ARDEF\\DEF\\T24_AR\\BLA\\",
   "C:\\1");

...

// cooler method

System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("C:\\ARDEF\\DEF\\T24_AR\\"); // this finds all the directorys inside T24_AR
                DirectoryInfo[] dirs = dir.GetDirectories();

                foreach (DirectoryInfo myDir in dirs)
                {
                    FolderOperation("C:\\ARDEF\\DEF\\T24_AR\\" + myDir.Name + ".rar",
                        "C:\\ARDEF\\DEF\\T24_AR\\" + myDir.Name + "\\",
                        "C:\\1");
                }



Unless I'm mistaken about your question, the way you're doing things is fairly natural way for your applications to evolve, going from solving one concrete problem to a more abstract, solve anything, algorithm.
Was This Post Helpful? 1
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: A function that used instead of multiple functions

Posted 11 May 2011 - 07:33 AM

Wow, that's a lot of code! Find where stuff repeats!

You call System.Diagnostics.Process a lot, with basically the same parameters but two. Perhaps you could give that a function:
private void Exec(string fn, string args) {
	System.Diagnostics.Process p = new System.Diagnostics.Process();
	p.StartInfo.FileName = fn;
	p.StartInfo.Arguments = args;
	p.StartInfo.UseShellExecute = false;
	p.StartInfo.RedirectStandardOutput = true;
	p.Start();
	p.WaitForExit();
}



You pretty much just call winrar in your example. Another function for that:
private void ExecWinRar(string args) {
	Exec(ConfigurationSettings.AppSettings["AddressofWinrarProgram"], args);
}



Now, the rest of your code. You have three basic variable for "UNC". So, the rest of the code is generic?

e.g.
// not the best arg names here.  I'd fix that
private void ProcessFolder(string str4, string str5, string str6) {
	string xaz = ConfigurationSettings.AppSettings["DestOfArchivedXAZ"];
	string xaz1 = ConfigurationSettings.AppSettings["AddressOfFile2BeArchivedXAZ"];
	string xaz2 = ConfigurationSettings.AppSettings["ExtractFolderXAZ"];
	
	ExecWinRar("a -r -ep1 " + str4 + " " + str5 + " " + str6 + "\\" + mname + "\\" + day + "\\");
	ExecWinRar("x -ac -o+ " + str4 + " *.* " + str6 + "\\" + "database" + "\\" + "bank_day" + "\\" + year + "\\" + mname + "\\" + day + "\\");

	foreach (string file in System.IO.Directory.GetFiles(str6 + "\\database\\bank_day\\2011\\may\\4", "*")) {
		System.IO.File.Copy(file, str6 + "\\database\\Publ_dbf\\" + System.IO.Path.GetFileName(file), true);
	}
	
	foreach (string dir in System.IO.Directory.GetDirectories(str6 + "\\database\\bank_day\\2011\\may\\4", "*")) {
		// you don't seem to use this
		// string destd = str6 + "\\database\\bank_dbf\\" + System.IO.Path.GetDirectoryName(dir);
		ExecWinRar("a -r -ep1 " + str6 + "\\database\\folders.rar " + dir);
	}
	
	ExecWinRar("x -ac -o+ " + str6 + "\\database\\folders.rar " + str6 + "\\database\\bank_dbf");
	System.IO.File.Delete(str6 + "\\database\\folders.rar");
}
	

public void UNV() {
	ProcessFolder(
		ConfigurationSettings.AppSettings["DestOfArchivedUNV"],
		ConfigurationSettings.AppSettings["AddressOfFile2BeArchivedUNV"],
		ConfigurationSettings.AppSettings["ExtractFolderUNV"]
	);
}



Now, for your naming convention... I'd go with something like:
<add key="UNV.DestOfArchived" value="C:\\ARDEF\\DEF\\T24_AR\\UNV.rar"/>
<add key="UNV.AddressOfFile2BeArchived" value="C:\\ARDEF\\DEF\\T24_AR\\UNV\\"/>
<add key="UNV.ExtractFolder" value="C:\\1"/>

<add key="XXX.DestOfArchived" value="C:\\ARDEF\\DEF\\T24_AR\\XXX.rar"/>



The reason is, then you can futher break down your code:
public void ProcessFoderForSettings(string folderId) {
	ProcessFolder(
		ConfigurationSettings.AppSettings[folderId + ".DestOfArchived"],
		ConfigurationSettings.AppSettings[folderId + ".AddressOfFile2BeArchived"],
		ConfigurationSettings.AppSettings[folderId + ".ExtractFolder"]
	);
}

public void UNV() { ProcessFoderForSettings("UNV"); }



Hope this helps.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1