Monday, 9 September 2013

C# Application to Bash script

C# Application to Bash script

I need a bit help and I hope one of you guys can help me.
I have a path(directory) in which there is a lot of folders in. In these
folders there are a lot of sub-folders and files which I do not know
anything about. So I do not know how many sub-folders and sub-folders
under them and files there is. It is a large tree of folders. My task is
to create a script that can check for on a folder or file is 72 hours old,
and if it is, the folder need to be deleted. That means I've got a path
with a lot of folders.
An example is the folder called ANSYS. In ANSYS there are numerous of
sub-folders and files. I must check all ANSYS folder through and see if
any of the files in the folder and its subfolders are 72 hours old. If
everything in ANSYS is 72 hours old so, the entire ANSYS have to be
deleted. I have made a C# application which can do my work, but I need to
use application on several hundred servers and I do not have time to
install the .NET framework on all servers. That is why I have to use a
bash script that can do the same.
Here is my C# application you can look at in order to understand more of
my assignment:
using System;
using System.IO;
namespace Scripts
{
internal class Program
{
private static void Main(string[] args)
{
//Defines the main path.
var topPath = @"C:\Ansys_RSM_Work_DIR\";
//Converts the first argument written as a parameter in
TaskScheduler or CMD, called 'hours'.
string argument = args[0];
int hours = Int32.Parse(argument);
//Defines the timespan in which a file has to be modified.
var deleteIfNotModifiedIn = new TimeSpan(hours, 0, 0);
//Is set to true if the file file has been modified within the
timespan.
bool fileTooOld = false;
//Searches through all directories in topPath, one at a time.
foreach (var dir in Directory.GetDirectories(topPath))
{
//Searches through all files in directory, one at a time.
foreach (var file in Directory.GetFiles(dir, "*",
SearchOption.AllDirectories))
{
//Calculate the difference between now and lastWriteTime =
fileLastModified.
var fileLastModified = DateTime.Now -
File.GetLastWriteTime(file);
if (fileLastModified < deleteIfNotModifiedIn)
fileTooOld = true;
}
if (fileTooOld == false)
Directory.Delete(dir, true);
else
fileTooOld = false;
}
}
}
}
Can anyone please help me?
With Kind Regards Shaban Mahmood

No comments:

Post a Comment