简体   繁体   中英

C# Application to batch file

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;
        }
    }
}
}

I'd already tried to make a script, but the script I've made deletes a subsubfolder if the files in it is 72 hours old, which it should not do. It should only delete the firstfolder (meaning like ANSYS) if all the files in ANSYS and all the subfolders in ANSYS is not modified since last 72 hours: My script:

FORFILES /S /D -3 /C "cmd /c IF @isdir==TRUE rd /S /Q @path" 
for /f "delims=" %i in ('dir /s /b /ad ^| sort /r') do rd "%i"

Can anyone please help me?

With Kind Regards Shaban Mahmood

Ok, try something like this:

Program.bat

@echo off
pushd  C\...[Taret folder containg folders to check, that is containing ANSYS]

set /p check="Date after which to check: "
Rem When prompted with above line type the date 3 days ago.

forfiles /c "cmd /c (IF @isdir==TRUE search "@path" "%check%")"
popd

And in the same directory:

Search.bat

set del=TRUE
forfiles /p %1 /d +%2 /s /m *.* /c "cmd /c (set del=FALSE)"
Pause |Echo Deleting %1... Exit to cancel
if %del%==True (rd /s %1)

That should work, Note it will pause before deleting each file. The way it works is it will check if any files withing the filder are younger than 72 hours and if there are, it will not delete the folder. It will do this for every folder in the specified folder. The search.bat file is a file that can be called with a folders path, and it will search all files within that parent folder, with the requirements you specified. Program.bat does the first step for you.

I made it pause in case it malfunctioned (or didn't work at all), and if this is the case please tell me where it didn't work. Also, comment if you want an explanation.

Mona

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM