Automated File Management Script
Windows

Creating a Folder with Automation Script to Move, Copy, and Delete Files

Project Description

The goal of this project is to create a dedicated folder that will automatically:

  1. Detect new files added to this folder.
  2. Copy those files to a secondary destination folder (e.g., E:\Music\1).
  3. Move those files to a primary destination folder (e.g., D:\Music\1), effectively removing them from the source folder.
  4. Provide system tray notifications to the user about the success or failure of the process.

This is useful for automating repetitive tasks using a PowerShell script on Windows.


Steps

1. Concept and Process Flow

Why Is This Step Important?

Understanding the workflow is essential to ensure that the designed process meets its objectives efficiently.

Workflow followed by this script:

  1. Files are dragged & dropped into a special folder (e.g., C:\DropFilesHere).
  2. The script automatically detects new files in this folder.
  3. Files are:
    • Copied to the secondary destination folder (E:\Music\1) for backup.
    • Moved to the primary destination folder (D:\Music\1), removing them from the source folder.
  4. Notifications are displayed to provide feedback to the user.

Why this sequence?

  • Files must be copied first because if they are moved immediately, the source location will no longer exist, making the copy impossible.

2. Preparing the PowerShell Script

Why Use PowerShell?

  • PowerShell is a built-in Windows tool that supports robust scripting for file system management, folder monitoring, and providing notifications.

Script Components

  1. FileSystemWatcher:
    • Detects changes in the folder being monitored.
  2. Copy-Item:
    • Copies the file to the secondary destination (backup).
  3. Move-Item:
    • Moves the file to the primary destination, effectively removing it from the source folder.
  4. BurntToast Module:
    • Used to display system tray notifications, making the process more interactive.

3. Final Script

Save this script as a file with a .ps1 extension (e.g., FileWatcher.ps1).

# Ensure the BurntToast module is installed
Import-Module BurntToast

# Initialize FileSystemWatcher to monitor the folder
$watcher = New-Object System.IO.FileSystemWatcher
$watcher.Path = "C:\DropFilesHere"  # Change this to your drag-and-drop folder
$watcher.Filter = "*.*"  # Monitor all file types
$watcher.IncludeSubdirectories = $false
$watcher.EnableRaisingEvents = $true

# Define the action performed when a new file is detected
$action = {
    try {
        $path = $Event.SourceEventArgs.FullPath
        $fileName = Split-Path $path -Leaf

        # First, copy the file to E:\Music\1
        Copy-Item -Path $path -Destination "E:\Music\1" -ErrorAction Stop

        # Then, move the file to D:\Music\1
        Move-Item -Path $path -Destination "D:\Music\1" -ErrorAction Stop

        # Display a system tray notification
        New-BurntToastNotification -Text "File successfully processed!", "$fileName has been copied and moved."
    } catch {
        # Display a notification if there is an error
        New-BurntToastNotification -Text "File processing failed!", $_.Exception.Message
    }
}

# Register the watcher to monitor the Created event
Register-ObjectEvent $watcher "Created" -Action $action

# Keep the script running
while ($true) {
    Start-Sleep -Seconds 5
}

4. Script Explanation

Key Steps:

  1. FileSystemWatcher:
    • Monitors the folder C:\DropFilesHere for new files.
    • The Created event is triggered when a new file is added.
  2. Copy File:
    • Files are first copied to the backup location (E:\Music\1) using Copy-Item.
    • Reason: The copy ensures that the file remains in the secondary destination even after being moved.
  3. Move File:
    • After the file is copied, it is moved to the primary destination (D:\Music\1) using Move-Item, removing it from the source folder.
  4. Notifications:
    • If successful, the user receives a system tray notification with a message like “File successfully processed!“.
    • If an error occurs, the user will see an error message in the notification.
  5. Infinite Loop:
    • The script keeps running with the while ($true) loop to continuously monitor the folder.

5. How to Run the Script

Step 1: Save the Script

Save the script as a .ps1 file, e.g., FileWatcher.ps1.

Step 2: Run the Script

  • Open PowerShell as Administrator.
  • Navigate to the directory where the script is saved:
cd C:\Path\To\Folder
  • Run the script:
.\FileWatcher.ps1

Step 3: Create a Shortcut (Optional)

To make it easier, create a shortcut to run the script directly:

  • Right-click on the desktop > New > Shortcut.
  • Enter:
powershell.exe -ExecutionPolicy Bypass -File C:\Path\To\FileWatcher.ps1
  • Save and name the shortcut.

6. Testing

  1. Create the folders C:\DropFilesHere, D:\Music\1, and E:\Music\1 for testing.
  2. Run the script and try adding files to C:\DropFilesHere.
  3. Verify that the files are successfully:
    • Copied to E:\Music\1.
    • Moved to D:\Music\1.
    • Removed from C:\DropFilesHere.

7. Troubleshooting

  • BurntToast Module Error:
    • If BurntToast is not installed, run:
      powershell Install-Module -Name BurntToast -Force -SkipPublisherCheck
  • Execution Policy Error:
    • If you encounter errors related to the Execution Policy, add -ExecutionPolicy Bypass when running the script.

Conclusion

This script is designed to efficiently automate repetitive tasks while providing clear feedback to users.

Its simple structure and modifiability make it a flexible solution for various needs.

Share it with anyone who might benefit from a similar solution! 😊

Leave a Reply

Your email address will not be published. Required fields are marked *