Folder Gremlin 5000 | A file processing automator

Folder Gremlin 5000 — Developer Devlog

Repository

๐Ÿ”— GitHub: Sewmina7/FolderGremlin5000


⚙️ Overview

Folder Gremlin 5000 is a lightweight automation utility built in C# (.NET) to sequentially process files from a designated directory using customizable execution commands. Originally created to queue and render Blender (.blend) projects automatically, it has evolved into a general-purpose automation tool for any file-based workflow that requires continuous monitoring, execution, and logging.

At its core, the program continuously watches a specified folder for new files and executes user-defined commands for each file sequentially — ensuring no overlap, missed jobs, or manual intervention.


๐Ÿงฉ System Architecture

The program consists of a single entry point (Program.cs) encapsulating all core logic:

  • Configuration Management
  • File Watching Loop
  • Process Execution & Output Handling
  • Logging System

Key Components

1. Configuration Files

All runtime parameters are stored in plain text files located in the application’s base directory:

File Purpose
file.txt Path to the executable (e.g., Blender)
command.txt Template for command-line arguments with placeholders
queue_dir.txt Directory to monitor for new files
output.txt Output file type or directory suffix
log.txt Auto-generated runtime log file

This design promotes flexibility and user customization without recompilation — users can modify behavior through text files rather than modifying code.


๐Ÿ” Execution Flow

1. Initialization

On startup, the program verifies all configuration files.
Missing files are automatically generated with default content and instructions.
This approach minimizes runtime errors due to missing configurations.

if (!File.Exists("file.txt")) {
    File.WriteAllText("file.txt", "C:/blender/");
    Console.WriteLine("File setting not found, created new file.");
}

2. Continuous File Watcher Loop

Rather than using FileSystemWatcher, the system employs a manual polling loop with recursion (goto FileWatcher) to maintain deterministic control and sequential execution.

  • The directory is scanned every second (Thread.Sleep(1000)).
  • If no files are found, the loop waits and retries.
  • When files are detected, the first file in the queue is processed.

This simple but effective design ensures one-file-at-a-time processing and avoids concurrency issues.


๐Ÿง  Command Templating Engine

Commands defined in command.txt can include placeholders:

  • {File} → full path to the input file
  • {FileName} → filename without extension
  • {output} → user-defined output type or path

These are dynamically replaced before command execution:

string commandToExecute = cmd
    .Replace("{File}", files[0])
    .Replace("{output}", File.ReadAllText("output.txt"))
    .Replace("{FileName}", Path.GetFileNameWithoutExtension(files[0]));

Example:

render {File} -output {FileName}.{output}

→ becomes

render "C:/queue/scene.blend" -output "scene.png"

⚡ Process Execution and Output Handling

The System.Diagnostics.Process API powers command execution.
Key configuration choices:

  • UseShellExecute = false and RedirectStandardOutput = true allow real-time capture of console output.
  • OutputDataReceived event handles live output for logging or trigger-based responses.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = file;
p.StartInfo.Arguments = commandToExecute;
p.OutputDataReceived += new DataReceivedEventHandler(OutputHandler);

The OutputHandler listens for specific output keywords (e.g., “Saving”) to detect process states and gracefully terminate after a buffer delay:

if (outLine.Data.Contains("Saving")) {
    Thread.Sleep(10000);
    p.Kill();
}

This feature was inspired by Blender’s console logs to automatically detect render completion.


๐Ÿงพ Logging System

A lightweight timestamped logging system captures all program events, including:

  • Session initialization
  • File discovery
  • Command execution
  • Process completion and cleanup

Example log entry:

[12:41:27.843] Task finished!
[12:41:27.844] Deleted file C:/queue/scene.blend

The logs are written to log.txt using:

File.AppendAllText("log.txt", "[" + DateTime.Now.TimeOfDay + "] " + txt + Environment.NewLine);

๐Ÿงฑ Design Decisions & Tradeoffs

Challenge Solution Reasoning
Missing Configurations Auto-create default files Prevents runtime crashes and guides user setup
Process Synchronization Sequential queue via recursion Simplifies logic, avoids threading complexity
Dynamic Command Templates Placeholder replacement Provides flexibility for various workflows
File Monitoring Manual polling More predictable than FileSystemWatcher for sequential workloads
Output Management Log-based feedback Transparency for troubleshooting and auditing

๐Ÿšง Future Improvements

  • Configuration Validation: Detect invalid syntax or missing placeholders.
  • Enhanced Error Handling: Add try/catch layers for process and file I/O.
  • Async File Watching: Replace recursion with async loop for cleaner control flow.
  • GUI or CLI Interface: Simplify setup and provide live status feedback.
  • Post-Processing Hooks: Move or rename output files after completion.

๐Ÿง  Developer Reflection

Building Folder Gremlin 5000 was a deep dive into process management, file I/O, and automation patterns in C#.
While the implementation is compact, it demonstrates strong modular thinking:

  • Self-healing configuration system
  • Reliable sequential file processing
  • Dynamic command injection
  • Real-time process output parsing

Originally designed as a render queue for Blender, it has matured into a universal file automation daemon that could be adapted for encoding pipelines, data conversion, or any repetitive command-line task.


๐Ÿ Conclusion

Folder Gremlin 5000 stands as both a personal milestone and a functional automation framework.
It captures the essence of practical software engineering: solving real problems with concise, flexible, and reliable code.
Future iterations aim to refine its architecture into a robust, user-friendly automation service — but even now, it performs its duties with the reliability of a true digital gremlin.

Comments

Popular Posts