MultiScript Examples

This page provides comprehensive examples of MultiScript usage, demonstrating the powerful scripting capabilities available in Multi Commander. These examples showcase various functionality areas from basic file operations to advanced automation workflows.

About MultiScript

MultiScript is Multi Commander's built-in scripting language that provides advanced automation capabilities. Scripts can access file system functions, manipulate strings and arrays, interact with the user interface, and integrate with external applications. All examples can be used as User Defined Commands and assigned to hotkeys or menu items.

Basic Examples

Simple scripts demonstrating fundamental MultiScript concepts and common use cases.

Search on Google from Command Line

Usage: Create this command and assign alias 'g' in the Alias Manager. Then type g search terms in the command line.

function CreateGoogleSearchQuery()
{
  @var $query = "";
  @var $n = 0;
  for( $n = 0; $n < $argcount; $n = $n + 1 )
  {
    if( $n > 0 )
    {
      $query = $query + "+";
    }
    $query = $query + $arg($n);
  }
  return $query;
}

@var $searchUrl = "http://www.google.com/search?q=" + CreateGoogleSearchQuery();
MC.Run CMD="{$searchUrl}" SHELL
View First Text File

Usage: Assign to hotkey (e.g., Alt+V) to view the first *.txt file in the focused folder.

@var $file = FindFirstFile( GetSourcePath() ^ "*.txt" );
if( StrLen($file) > 0 )
{
  MC.DataViewer.View FILE="{$file}"
}
else
{
  MessageBox("No Files", "No .txt files found in this folder", 0);
}
Play First Video File

Usage: Assign to hotkey (e.g., Alt+P) to play the first video file found in the focused folder.

@var $file;
$file = FindFirstFile( GetSourcePath() ^ "*.avi" );
if( StrLen($file) == 0 )
{
  $file = FindFirstFile( GetSourcePath() ^ "*.mkv" );
}
if( StrLen($file) == 0 )
{
  $file = FindFirstFile( GetSourcePath() ^ "*.mp4" );
}

if( StrLen($file) > 0 )
{
  MC.Run CMD="{$file}" SHELL
}
else
{
  MessageBox("No Videos", "No video files found", 0);
}

File Operations Examples

Scripts demonstrating file system operations, batch processing, and file management tasks.

Batch File Organizer by Date

Purpose: Organize selected files into folders based on their creation date (YYYY-MM format).

@var $files = GetSourceSelectedPaths();
@var $sourceDir = GetSourcePath();
@var $count = arrayCount($files);

if( $count == 0 )
{
  MessageBox("No Selection", "Please select files to organize", 0);
  return;
}

@var $n;
for( $n = 0; $n < $count; $n = $n + 1 )
{
  @var $file = $files[$n];
  @var $fileTime = GetFileTime($file);
  @var $dateFolder = FormatDate("yyyy-MM", $fileTime);
  
  @var $targetDir = $sourceDir ^ $dateFolder;
  
  // Create target directory if it doesn't exist
  if( FileExists($targetDir) == 0 )
  {
    MakeDir($targetDir, "LOCAL");
  }
  
  // Move file to date folder
  @var $fileName = PathGetNamePart($file);
  MoveFile($targetDir, $file, "NODIALOG");
}

MessageBox("Complete", "Organized " + $count + " files by date", 0);
Create Project Folder Structure

Purpose: Create a standardized project folder structure.

@var $projectName = AskText("Project Name", "", 0);
if( StrLen($projectName) == 0 )
{
  return;
}

@var $basePath = GetSourcePath() ^ $projectName;
@var $folders[] = {
  "Documents",
  "Source Code",
  "Resources\\Images",
  "Resources\\Data",
  "Output\\Debug",
  "Output\\Release",
  "Tests"
};

// Create main project folder
MakeDir($basePath, "LOCAL");

// Create subfolders
@var $i;
@var $folderCount = arrayCount($folders);
for( $i = 0; $i < $folderCount; $i = $i + 1 )
{
  @var $folderPath = $basePath ^ $folders[$i];
  MakeDir($folderPath, "LOCAL,RECURSIVE");
}

MessageBox("Success", "Project structure created for: " + $projectName, 0);
Backup Selected Files

Purpose: Create dated backup archive of selected files.

@var $files = GetSourceSelectedPaths();
@var $count = arrayCount($files);

if( $count == 0 )
{
  MessageBox("No Selection", "Select files to backup", 0);
  return;
}

@var $timestamp = FormatDate("yyyy-MM-dd_HH-mm-ss", GetTime());
@var $backupName = "Backup_" + $timestamp + ".zip";
@var $backupPath = GetSourcePath() ^ $backupName;

// Create backup archive
@var $i;
for( $i = 0; $i < $count; $i = $i + 1 )
{
  @var $file = $files[$i];
  if( $i == 0 )
  {
    // Create new archive with first file
    PackFile($backupPath, $file, "*.*", "zip", "SILENT");
  }
  else
  {
    // Add remaining files to existing archive
    PackFile($backupPath, $file, "*.*", "zip", "SILENT");
  }
}

MessageBox("Backup Complete", "Created backup: " + $backupName, 0);

Text Processing Examples

Scripts demonstrating string manipulation, text file processing, and data transformation.

Process Log File - Extract Error Lines

Purpose: Extract all error lines from a log file and save to a separate file.

@var $logFile = GetSourceFocusPath();
if( StrLen($logFile) == 0 )
{
  MessageBox("No File", "Please select a log file", 0);
  return;
}

// Load log file content
@var $content = LoadStringFromFile($logFile);
@var $lines = StrLines2Array($content);
@var $errorLines[];
@var $lineCount = arrayCount($lines);

@var $i;
for( $i = 0; $i < $lineCount; $i = $i + 1 )
{
  @var $line = $lines[$i];
  // Check for error keywords (case insensitive)
  @var $upperLine = StrToUpper($line);
  if( StrFind($upperLine, "ERROR", 0) >= 0 || 
      StrFind($upperLine, "EXCEPTION", 0) >= 0 ||
      StrFind($upperLine, "FAILED", 0) >= 0 )
  {
    arrayAdd($errorLines, $line);
  }
}

// Save error lines to new file
@var $errorCount = arrayCount($errorLines);
if( $errorCount > 0 )
{
  @var $outputFile = PathGetPathPart($logFile) ^ "errors_" + FormatDate("yyyy-MM-dd", GetTime()) + ".txt";
  @var $errorContent = StrLinesArray2String($errorLines);
  SaveStringToFile($outputFile, $errorContent, 1); // UTF8 format
  
  MessageBox("Error Extraction Complete", "Found " + $errorCount + " error lines\nSaved to: " + PathGetNamePart($outputFile), 0);
}
else
{
  MessageBox("No Errors", "No error lines found in the log file", 0);
}
Generate File List Report

Purpose: Create a detailed report of files in the current folder.

@var $currentPath = GetSourcePath();
@var $files = GetSourceItems(1); // Get full paths
@var $report[];
@var $totalSize = 0;

// Add header
arrayAdd($report, "=== FILE REPORT ===");
arrayAdd($report, "Path: " + $currentPath);
arrayAdd($report, "Generated: " + FormatDate("yyyy-MM-dd", GetTime()) + " " + FormatTime("HH:mm:ss", GetTime()));
arrayAdd($report, "");

@var $fileCount = arrayCount($files);
@var $i;
for( $i = 0; $i < $fileCount; $i = $i + 1 )
{
  @var $file = $files[$i];
  if( IsFolder($file) == 0 ) // Only process files, not folders
  {
    @var $size = GetFileSize($file);
    @var $fileTime = GetFileTime($file);
    @var $name = PathGetNamePart($file);
    
    @var $sizeKB = $size / 1024;
    @var $dateStr = FormatDate("yyyy-MM-dd", $fileTime);
    
    @var $line = $name + " | " + $sizeKB + " KB | " + $dateStr;
    arrayAdd($report, $line);
    
    $totalSize = $totalSize + $size;
  }
}

// Add summary
arrayAdd($report, "");
arrayAdd($report, "=== SUMMARY ===");
arrayAdd($report, "Total Files: " + (arrayCount($report) - 5)); // Subtract header lines
arrayAdd($report, "Total Size: " + ($totalSize / 1024) + " KB");

// Save report
@var $reportFile = $currentPath ^ "file_report_" + FormatDate("yyyy-MM-dd", GetTime()) + ".txt";
@var $reportContent = StrLinesArray2String($report);
SaveStringToFile($reportFile, $reportContent, 1);

MessageBox("Report Generated", "File report saved as: " + PathGetNamePart($reportFile), 0);
Batch Rename with Pattern

Purpose: Rename files using pattern replacement with user input.

@var $files = GetSourceSelectedFileNames();
@var $count = arrayCount($files);

if( $count == 0 )
{
  MessageBox("No Selection", "Select files to rename", 0);
  return;
}

@var $searchPattern = AskText("Search Pattern", "Enter text to find", 0);
if( StrLen($searchPattern) == 0 )
{
  return;
}

@var $replaceWith = AskText("Replace With", "Enter replacement text", 0);

@var $renamed = 0;
@var $i;
for( $i = 0; $i < $count; $i = $i + 1 )
{
  @var $oldName = $files[$i];
  @var $newName = StrReplace($oldName, $searchPattern, $replaceWith);
  
  if( StrIsEqual($oldName, $newName) == 0 ) // Names are different
  {
    @var $sourcePath = GetSourcePath();
    @var $fullOldPath = $sourcePath ^ $oldName;
    
    @var $result = RenameFile($fullOldPath, $newName, "SILENT");
    if( $result == 1 )
    {
      $renamed = $renamed + 1;
    }
  }
}

MessageBox("Rename Complete", "Renamed " + $renamed + " files", 0);

User Interaction Examples

Scripts demonstrating user input, dialog boxes, and interactive workflows.

Interactive File Processor

Purpose: Process files with user choices for each operation.

@var $files = GetSourceSelectedPaths();
@var $count = arrayCount($files);

if( $count == 0 )
{
  MessageBox("No Selection", "Select files to process", 0);
  return;
}

@var $operations[] = {
  "Copy to Backup Folder",
  "Move to Archive Folder", 
  "Delete Files",
  "Rename with Timestamp",
  "Cancel"
};

@var $choice = AskOption("Choose Operation", $operations, 0);
if( $choice == -1 || $choice == 4 ) // Cancel or last option
{
  return;
}

@var $processed = 0;
@var $i;
for( $i = 0; $i < $count; $i = $i + 1 )
{
  @var $file = $files[$i];
  @var $fileName = PathGetNamePart($file);
  
  if( $choice == 0 ) // Copy to Backup
  {
    @var $backupDir = GetSourcePath() ^ "Backup";
    MakeDir($backupDir, "LOCAL");
    CopyFile($backupDir, $file, "NODIALOG");
    $processed = $processed + 1;
  }
  else if( $choice == 1 ) // Move to Archive
  {
    @var $archiveDir = GetSourcePath() ^ "Archive";
    MakeDir($archiveDir, "LOCAL");
    MoveFile($archiveDir, $file, "NODIALOG");
    $processed = $processed + 1;
  }
  else if( $choice == 2 ) // Delete
  {
    @var $confirmDelete = MessageBox("Confirm Delete", "Delete " + $fileName + "?", 4); // Yes/No
    if( $confirmDelete == 6 ) // Yes
    {
      DeleteFile($file, "RECYCLE");
      $processed = $processed + 1;
    }
  }
  else if( $choice == 3 ) // Rename with timestamp
  {
    @var $timestamp = FormatDate("yyyy-MM-dd_HH-mm-ss", GetTime());
    @var $nameOnly = PathGetNamePart($file, 1);
    @var $ext = PathGetFileExtPart($file);
    @var $newName = $nameOnly + "_" + $timestamp + $ext;
    RenameFile($file, $newName, "SILENT");
    $processed = $processed + 1;
  }
}

MessageBox("Processing Complete", "Processed " + $processed + " files", 0);
Folder Size Calculator

Purpose: Calculate and display detailed folder size information.

@var $targetPath = GetSourcePath();
@var $allFiles = FindFiles($targetPath ^ "*.*");
@var $fileCount = arrayCount($allFiles);

@var $totalSize = 0;
@var $fileTypes = {};
@var $sizeBuckets[] = {0, 0, 0, 0, 0}; // <1KB, 1KB-1MB, 1MB-10MB, 10MB-100MB, >100MB

@var $i;
for( $i = 0; $i < $fileCount; $i = $i + 1 )
{
  @var $file = $allFiles[$i];
  if( IsFolder($file) == 0 ) // Only count files
  {
    @var $size = GetFileSize($file);
    $totalSize = $totalSize + $size;
    
    // Categorize by size
    if( $size < 1024 )
    {
      $sizeBuckets[0] = $sizeBuckets[0] + 1;
    }
    else if( $size < 1048576 ) // 1MB
    {
      $sizeBuckets[1] = $sizeBuckets[1] + 1;
    }
    else if( $size < 10485760 ) // 10MB
    {
      $sizeBuckets[2] = $sizeBuckets[2] + 1;
    }
    else if( $size < 104857600 ) // 100MB
    {
      $sizeBuckets[3] = $sizeBuckets[3] + 1;
    }
    else
    {
      $sizeBuckets[4] = $sizeBuckets[4] + 1;
    }
  }
}

// Build report
@var $sizeKB = $totalSize / 1024;
@var $sizeMB = $sizeKB / 1024;
@var $sizeGB = $sizeMB / 1024;

@var $report = "FOLDER SIZE ANALYSIS\n";
$report = $report + "Path: " + $targetPath + "\n\n";
$report = $report + "Total Files: " + $fileCount + "\n";
$report = $report + "Total Size: " + $totalSize + " bytes\n";
$report = $report + "            " + $sizeKB + " KB\n";
$report = $report + "            " + $sizeMB + " MB\n";
$report = $report + "            " + $sizeGB + " GB\n\n";
$report = $report + "SIZE DISTRIBUTION:\n";
$report = $report + "< 1 KB: " + $sizeBuckets[0] + " files\n";
$report = $report + "1 KB - 1 MB: " + $sizeBuckets[1] + " files\n";
$report = $report + "1 MB - 10 MB: " + $sizeBuckets[2] + " files\n";
$report = $report + "10 MB - 100 MB: " + $sizeBuckets[3] + " files\n";
$report = $report + "> 100 MB: " + $sizeBuckets[4] + " files\n";

MessageBox("Folder Size Analysis", $report, 0);

Advanced Examples

Complex scripts demonstrating advanced MultiScript features, error handling, and sophisticated automation workflows.

Comprehensive File Synchronizer

Purpose: Synchronize files between source and target directories with conflict resolution.

function CompareDates($file1, $file2)
{
  @var $time1 = GetFileTime($file1);
  @var $time2 = GetFileTime($file2);
  
  if( $time1 > $time2 )
  {
    return 1; // file1 is newer
  }
  else if( $time1 < $time2 )
  {
    return -1; // file2 is newer
  }
  else
  {
    return 0; // same date
  }
}

function SyncFiles($sourceDir, $targetDir)
{
  @var $sourceFiles = FindFiles($sourceDir ^ "*.*");
  @var $fileCount = arrayCount($sourceFiles);
  @var $copied = 0;
  @var $skipped = 0;
  @var $conflicts = 0;
  
  @var $i;
  for( $i = 0; $i < $fileCount; $i = $i + 1 )
  {
    @var $sourceFile = $sourceFiles[$i];
    if( IsFolder($sourceFile) == 0 ) // Only process files
    {
      @var $fileName = PathGetNamePart($sourceFile);
      @var $targetFile = $targetDir ^ $fileName;
      
      if( FileExists($targetFile) == 0 )
      {
        // File doesn't exist in target, copy it
        CopyFile($targetDir, $sourceFile, "NODIALOG");
        $copied = $copied + 1;
      }
      else
      {
        // File exists, check dates
        @var $comparison = CompareDates($sourceFile, $targetFile);
        if( $comparison > 0 )
        {
          // Source is newer, ask user
          @var $choice = MessageBox("File Conflict", $fileName + " exists in target but source is newer.\nOverwrite?", 4); // Yes/No
          if( $choice == 6 ) // Yes
          {
            CopyFile($targetDir, $sourceFile, "NODIALOG,OVERWRITE_ALL");
            $copied = $copied + 1;
          }
          else
          {
            $skipped = $skipped + 1;
          }
        }
        else if( $comparison == 0 )
        {
          // Same date, skip
          $skipped = $skipped + 1;
        }
        else
        {
          // Target is newer, log conflict
          $conflicts = $conflicts + 1;
        }
      }
    }
  }
  
  @var $result = "SYNC RESULTS:\n";
  $result = $result + "Files copied: " + $copied + "\n";
  $result = $result + "Files skipped: " + $skipped + "\n";
  $result = $result + "Conflicts (target newer): " + $conflicts;
  
  return $result;
}

// Main execution
@var $sourceDir = GetSourcePath();
@var $targetDir = GetTargetPath();

if( StrLen($targetDir) == 0 )
{
  MessageBox("Error", "Please select target directory in right panel", 0);
  return;
}

@var $confirm = MessageBox("Confirm Sync", "Sync from:\n" + $sourceDir + "\n\nTo:\n" + $targetDir, 4); // Yes/No

if( $confirm == 6 ) // Yes
{
  @var $result = SyncFiles($sourceDir, $targetDir);
  MessageBox("Sync Complete", $result, 0);
}
Database-Style File Query

Purpose: Search files using multiple criteria like a database query.

function CreateFilter()
{
  @var $filter = FilterCreate();
  
  // Get search criteria from user
  @var $namePattern = AskText("Name Pattern", "Enter filename pattern (* for any)", 0);
  if( StrLen($namePattern) > 0 )
  {
    FilterAddRule($filter, "fullname", "wildcard", $namePattern);
  }
  
  @var $extensions[] = {"Any", "*.txt", "*.doc", "*.pdf", "*.jpg", "*.mp3"};
  @var $extChoice = AskOption("File Extension", $extensions, 0);
  if( $extChoice > 0 )
  {
    FilterAddRule($filter, "ext", "wildcard", $extensions[$extChoice]);
  }
  
  @var $sizes[] = {"Any Size", "< 1 MB", "1-10 MB", "> 10 MB"};
  @var $sizeChoice = AskOption("File Size", $sizes, 0);
  if( $sizeChoice == 1 )
  {
    FilterAddRule($filter, "size", "lessthan", "1048576");
  }
  else if( $sizeChoice == 2 )
  {
    FilterAddRule($filter, "size", "morethan", "1048576");
    FilterAddRule($filter, "size", "lessthan", "10485760");
  }
  else if( $sizeChoice == 3 )
  {
    FilterAddRule($filter, "size", "morethan", "10485760");
  }
  
  return $filter;
}

// Main query execution
@var $filter = CreateFilter();
@var $searchPath = GetSourcePath();
@var $allFiles = FindFiles($searchPath ^ "*.*");
@var $matches[];

@var $fileCount = arrayCount($allFiles);
@var $i;
for( $i = 0; $i < $fileCount; $i = $i + 1 )
{
  @var $file = $allFiles[$i];
  if( FilterIsMatch($filter, $file) == 1 )
  {
    arrayAdd($matches, $file);
  }
}

@var $matchCount = arrayCount($matches);
if( $matchCount > 0 )
{
  // Generate results file
  @var $results = "QUERY RESULTS\n";
  $results = $results + "Search Path: " + $searchPath + "\n";
  $results = $results + "Matches Found: " + $matchCount + "\n\n";
  
  for( $i = 0; $i < $matchCount; $i = $i + 1 )
  {
    @var $file = $matches[$i];
    @var $size = GetFileSize($file);
    @var $date = FormatDate("yyyy-MM-dd", GetFileTime($file));
    $results = $results + PathGetNamePart($file) + " | " + $size + " bytes | " + $date + "\n";
  }
  
  @var $resultsFile = $searchPath ^ "query_results.txt";
  SaveStringToFile($resultsFile, $results, 1);
  
  MessageBox("Query Complete", "Found " + $matchCount + " matches\nResults saved to query_results.txt", 0);
}
else
{
  MessageBox("No Matches", "No files matched your query criteria", 0);
}
System Information Logger

Purpose: Collect and log detailed system and Multi Commander information.

function GetSystemInfo()
{
  @var $info[];
  
  // Basic system information
  arrayAdd($info, "=== SYSTEM INFORMATION ===");
  arrayAdd($info, "Timestamp: " + FormatDate("yyyy-MM-dd", GetTime()) + " " + FormatTime("HH:mm:ss", GetTime()));
  arrayAdd($info, "Windows Directory: " + TranslateEnvString("%WINDIR%"));
  arrayAdd($info, "User Profile: " + TranslateEnvString("%USERPROFILE%"));
  arrayAdd($info, "Computer Name: " + TranslateEnvString("%COMPUTERNAME%"));
  arrayAdd($info, "");
  
  // Multi Commander information
  arrayAdd($info, "=== MULTI COMMANDER ===");
  arrayAdd($info, "Install Path: " + GetTagValue("${mcinstallpath}"));
  arrayAdd($info, "Config Path: " + GetTagValue("${mcconfigpath}"));
  arrayAdd($info, "");
  
  // Current session information
  arrayAdd($info, "=== CURRENT SESSION ===");
  arrayAdd($info, "Source Path: " + GetSourcePath());
  arrayAdd($info, "Target Path: " + GetTargetPath());
  arrayAdd($info, "Active Panel: " + TabActivePanelSide());
  arrayAdd($info, "Source Tab Count: " + TabCount("source"));
  arrayAdd($info, "Target Tab Count: " + TabCount("target"));
  arrayAdd($info, "");
  
  // File system information
  arrayAdd($info, "=== CURRENT FOLDER STATS ===");
  @var $currentFiles = GetSourceItems(1);
  @var $fileCount = 0;
  @var $folderCount = 0;
  @var $totalSize = 0;
  
  @var $itemCount = arrayCount($currentFiles);
  @var $i;
  for( $i = 0; $i < $itemCount; $i = $i + 1 )
  {
    @var $item = $currentFiles[$i];
    if( IsFolder($item) == 1 )
    {
      $folderCount = $folderCount + 1;
    }
    else
    {
      $fileCount = $fileCount + 1;
      $totalSize = $totalSize + GetFileSize($item);
    }
  }
  
  arrayAdd($info, "Files: " + $fileCount);
  arrayAdd($info, "Folders: " + $folderCount);
  arrayAdd($info, "Total Size: " + $totalSize + " bytes (" + ($totalSize / 1024) + " KB)");
  
  return $info;
}

// Generate and save system information
@var $sysInfo = GetSystemInfo();
@var $infoText = StrLinesArray2String($sysInfo);

@var $logPath = GetSourcePath() ^ "system_info_" + FormatDate("yyyy-MM-dd_HH-mm-ss", GetTime()) + ".txt";
SaveStringToFile($logPath, $infoText, 1);

// Also save to clipboard
SetClipboardText($infoText);

MessageBox("System Info Collected", "System information saved to:\n" + PathGetNamePart($logPath) + "\n\nAlso copied to clipboard", 0);
MultiScript Best Practices

Error Handling: Always check return values and validate user inputs. User Experience: Provide clear feedback through MessageBox dialogs and progress indicators. Code Organization: Use functions for reusable code blocks and meaningful variable names. Performance: Consider file counts and sizes when processing large datasets. Safety: Use "SILENT" and "NODIALOG" options carefully, and consider backup operations for destructive actions. Testing: Test scripts on small datasets before running on important files.

Related MultiScript Documentation

Enhance your MultiScript knowledge with: Language Syntax, Core Functions, File System Functions, String Functions, Array Functions, User Defined Commands, and MultiScript Debugger.