简体   繁体   English

C#申请批处理文件

[英]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. 我的任务是创建一个脚本,该脚本可以检查是否存在72小时的文件夹或文件,如果存在,则需要删除该文件夹。 That means I've got a path with a lot of folders. 这意味着我有很多文件夹。

An example is the folder called ANSYS. 一个示例是名为ANSYS的文件夹。 In ANSYS there are numerous of sub-folders and files. 在ANSYS中,有许多子文件夹和文件。 I must check all ANSYS folder through and see if any of the files in the folder and its subfolders are 72 hours old. 我必须检查所有ANSYS文件夹,并查看该文件夹及其子文件夹中的任何文件是否存在72个小时。 If everything in ANSYS is 72 hours old so, the entire ANSYS have to be deleted. 如果ANSYS中的所有内容都存在72小时,则必须删除整个ANSYS。 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. 我已经制作了一个可以完成我的工作的C#应用​​程序,但是我需要在数百台服务器上使用该应用程序,而且我没有时间在所有服务器上安装.NET框架。 That is why I have to use a bash script that can do the same. 这就是为什么我必须使用可以执行相同操作的bash脚本的原因。

Here is my C# application you can look at in order to understand more of my assignment: 这是我的C#应用​​程序,您可以查看它以了解更多我的作业:

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. 我已经尝试制作脚本,但是如果脚本中的文件已存在72个小时,那么我编写的脚本会删除该子文件夹,这是不应该的。 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: 如果自上一个72小时以来未修改ANSYS中的所有文件和ANSYS中的所有子文件夹,则它仅应删除第一个文件夹(类似于ANSYS):我的脚本:

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 诚挚的问候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 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. 它的工作方式是检查与该文件一起发送的文件是否少于72小时,如果有,则不会删除该文件夹。 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. search.bat文件是可以使用文件夹路径调用的文件,它将根据您指定的要求搜索该父文件夹内的所有文件。 Program.bat does the first step for you. Program.bat为您完成了第一步。

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 莫娜

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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