Multi Commander > Script

MultiScript For Recursively Deleting Files Inside Folder (incl. SubDirectories)?

<< < (2/2)

total_annihilation00:
I got 2 scripts written by AI's, was wondering if either of them will work ? Script #1 is by far a more powerful AI:

Script #1::

--- Code: ---@var $i;
@var $item;
@var $path;

// ============================================================
// CONFIGURATION SECTION
// ============================================================

// 1. Folders to EMPTY (Delete all files and subfolders inside, keep folder)
@var $foldersToEmpty = array(
    "D:\\Games\\AOE II HD\\Logs\\",
    "D:\\Games\\AOMX-TOTD\\Logs\\"
);

// 2. Folders to clean specifically (e.g. *.log)
// format: array( "Folder Path", "Mask" )
@var $foldersToCleanMask = array(
    "D:\\Games\\ORA\\Support\\Replays\\cnc\\", "*.log",
    "D:\\Games\\ORA\\Support\\Replays\\d2k\\", "*.log"
);

// 3. Specific Files to Delete
@var $filesToDelete = array(
    "D:\\Games\\Delta Force 3\\DDrawCompat-DFLW.log",
    "D:\\Games\\Delta Force 4\\DDrawCompat-DFTFD.log"
);

// ============================================================
// EXECUTION SECTION
// ============================================================

// --- Task 1: Empty Contents of Folders ---
for( $i = 0; $i < count($foldersToEmpty); $i++ )
{
    $path = $foldersToEmpty[$i];
    if( FileSystem_PathIsFolder( $path ) )
    {
        // Call custom function
        EmptyFolderContents( $path );
    }
}

// --- Task 2: Delete by Mask (*.log) ---
// Note: Array is Key/Value pairs, so we step by 2
for( $i = 0; $i < count($foldersToCleanMask); $i = $i + 2 )
{
    $path = $foldersToCleanMask[$i];
    @var $mask = $foldersToCleanMask[$i+1];
   
    if( FileSystem_PathIsFolder( $path ) )
    {
        DeleteFilesByMask( $path, $mask );
    }
}

// --- Task 3: Delete Specific Files ---
for( $i = 0; $i < count($filesToDelete); $i++ )
{
    $path = $filesToDelete[$i];
    if( FileSystem_PathIsFile( $path ) )
    {
        FileSystem_DeleteFile( $path );
    }
}

// ============================================================
// FUNCTIONS
// ============================================================

// Function to delete all contents (files + dirs) inside a path
@func EmptyFolderContents( $rootPath )
{
    // Ensure path ends with slash
    if( StrIsWildMatch( "*\\", $rootPath ) == 0 )
    {
        $rootPath = $rootPath ^ "\\";
    }

    // 1. Delete ALL Files
    @var $files = MC.Folder.GetItems( $rootPath, "*", "FILES" );
    @var $f;
    @var $count = count($files);
   
    for( $f = 0; $f < $count; $f++ )
    {
        // Full path construction
        @var $fullFilePath = $rootPath ^ $files[$f];
        FileSystem_DeleteFile( $fullFilePath );
    }

    // 2. Delete ALL Sub-Folders
    // We do this after files. FileSystem_DeleteFolder is recursive.
    @var $dirs = MC.Folder.GetItems( $rootPath, "*", "FOLDERS" );
    $count = count($dirs);

    for( $f = 0; $f < $count; $f++ )
    {
        @var $fullDirPath = $rootPath ^ $dirs[$f];
        FileSystem_DeleteFolder( $fullDirPath );
    }
}

// Function to delete specific file masks inside a path
@func DeleteFilesByMask( $rootPath, $fileMask )
{
    // Ensure path ends with slash
    if( StrIsWildMatch( "*\\", $rootPath ) == 0 )
    {
        $rootPath = $rootPath ^ "\\";
    }

    // Get files matching mask
    @var $files = MC.Folder.GetItems( $rootPath, $fileMask, "FILES" );
    @var $f;
    @var $count = count($files);

    for( $f = 0; $f < $count; $f++ )
    {
        @var $fullFilePath = $rootPath ^ $files[$f];
        FileSystem_DeleteFile( $fullFilePath );
    }
}

--- End code ---

Alternate Script #2::

--- Code: ---@var $foldersToEmpty = {
    "D:\Games\AOE II HD\Logs\";
    "D:\Games\AOMX-TOTD\Logs\"
};

@var $logFolders = {
    "D:\Games\ORA\Support\Replays\cnc\";
    "D:\Games\ORA\Support\Replays\d2k\"
};

@var $specificFiles = {
    "D:\Games\Delta Force 3\DDrawCompat-DFLW.log";
    "D:\Games\Delta Force 4\DDrawCompat-DFTFD.log"
};

// ──────────────────────────────────────────────────
// 1. Completely empty selected folders (keep folder itself)
// ──────────────────────────────────────────────────
@var $n = 0;
@var $count = @arraysize($foldersToEmpty);

for( $n = 0; $n < $count; $n++ )
{
    @var $path = $foldersToEmpty[$n];

    // Delete EVERYTHING inside (files + folders + subfolders recursively)
    DeleteFiles $path* /F /S /Q;

    // Optional: show what was cleaned (remove // if you want feedback)
    // Log "Emptied: " + $path;
}

// ──────────────────────────────────────────────────
// 2. Delete only *.log files in specific folders (keep everything else)
// ──────────────────────────────────────────────────
$n = 0;
$count = @arraysize($logFolders);

for( $n = 0; $n < $count; $n++ )
{
    @var $path = $logFolders[$n];
    DeleteFiles $path*.log /F /S /Q;
    // Log "Deleted *.log in: " + $path;
}

// ──────────────────────────────────────────────────
// 3. Delete specific individual files
// ──────────────────────────────────────────────────
$n = 0;
$count = @arraysize($specificFiles);

for( $n = 0; $n < $count; $n++ )
{
    @var $file = $specificFiles[$n];
    if( @fileexists($file) )
    {
        DeleteFile $file;
        // Log "Deleted: " + $file;
    }
}

// ──────────────────────────────────────────────────
// Finished
// ──────────────────────────────────────────────────
MessageBox "info", "All cleanup tasks completed successfully!", "Multi Commander Cleanup";

--- End code ---

P.S: 3 types of Delete Operations: Delete contents inside certain folders, delete specific filetypes inside certain folders, and delete individual files. I want them permanently deleted but /w Confirmation !

Navigation

[0] Message Index

[*] Previous page

Go to full version