Author Topic: MultiScript For Recursively Deleting Files Inside Folder (incl. SubDirectories)?  (Read 1251 times)

total_annihilation00

  • Active Member
  • ***
  • Posts: 140
  • Tech Savant\ Envisioneering
    • View Profile
I want to use MC's MultiScript to delete the contents of a folder while keeping the folder itself intact, just delete all files and subdirectories inside it. I tried a lot of AI-generated code but it didn't work ! Is it even possible to do this via MC ? 'Cause while it says D:\\SomeFolder\\* is valid syntax, I think the asterisk (to delete the contents of the folder) is breaking the script, I'am at wits end here ! I can get MC to delete a single file, but a folder /w files and subdirectories is another thing altogether. I'd appreciate all the help I can get ! I'am not certain but I think I read somewhere that MC has removed a lot of scripting commands, especially VBScript, though I'am not sure. P.S: Perhaps using a For loop to delete the files inside ?

I tried this:
Code: [Select]
Delete FILE="C:\Users\dell\Downloads\thumbstick\silotest.log" CONFIRM=YES RECYCLE=YES;
Delete FILE="C:\Users\dell\Downloads\thumbstick\Silo\" RECURSE=YES CONFIRM=YES RECYCLE=YES;

And before that this:
Code: [Select]
@var $filename = "C:\\Users\\dell\\Downloads\\thumbstick\\Silo\\*";
@var $options[] = {"RECYCLE"};
DeleteFile( $filename, $options );

P.P.S: Nevermind, I'am doing it using a PowerShell script, & using MC to call it…
« Last Edit: November 11, 2025, 21:13:03 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~


Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4798
    • View Profile
    • Multi Commander
There is no CustomCommand named "Delete"

MC.FileSystem.Delete - https://multicommander.com/Docs/customcommands_list#mc.filesystem.delete

and for MultiScript DeleteFile and DeleteFiles
https://multicommander.com/Docs/multiscript/functions/filesystem#deletefile
https://multicommander.com/Docs/multiscript/functions/filesystem#deletefiles

If you just what to delete special files. Than use MultiScript. enumerate folder and delete a found file/folder that match criteria for deletion.

total_annihilation00

  • Active Member
  • ***
  • Posts: 140
  • Tech Savant\ Envisioneering
    • View Profile
Could you perhaps give me a rudimentary practical or skeletal code example please ? The Online Documentation is far too vague and only shows how to assign the DeleteFiles to a Variable (incomplete example), nothing about how to Enumerate the Folder and Match File Criteria, then delete. There should be an offline Help Documentation PDF IMO.
« Last Edit: November 15, 2025, 08:58:46 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~


Mathias (Author)

  • Administrator
  • VIP Member
  • *****
  • Posts: 4798
    • View Profile
    • Multi Commander
I don't have a script that does that now. I will check later if I have one that scan filesystem. It a bit complex. need to use function. and call function for each folder recursivly.

I had something that generated pdf before but it stopped working after webpage redesign and I had not had time to look into it.
but all documentation is available on github. at https://github.com/MultiCommander/MultiCommander-Documentation 

total_annihilation00

  • Active Member
  • ***
  • Posts: 140
  • Tech Savant\ Envisioneering
    • View Profile
I don't have a script that does that now. I will check later if I have one that scan filesystem. It a bit complex. need to use function. and call function for each folder recursivly.

Alright, appreciate it ! well hope it's not too complex ! But nevertheless I would love to tweak it to my needs !

I had something that generated pdf before but it stopped working after webpage redesign and I had not had time to look into it.
but all documentation is available on github. at https://github.com/MultiCommander/MultiCommander-Documentation

Thanks a million ! 🙏
« Last Edit: November 15, 2025, 15:33:36 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~


total_annihilation00

  • Active Member
  • ***
  • Posts: 140
  • Tech Savant\ Envisioneering
    • View Profile
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: [Select]
@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 );
    }
}

Alternate Script #2::
Code: [Select]
@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";

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 !
« Last Edit: November 25, 2025, 02:55:06 by total_annihilation00 »
~The World's Deceit Has Raped My Soul, We Melt The Plastic People Down Then We Melt Their Plastic Town~