Project Description
The goal of this project is to create a dedicated folder that will automatically:
- Detect new files added to this folder.
- Copy those files to a secondary destination folder (e.g.,
E:\Music\1
). - Move those files to a primary destination folder (e.g.,
D:\Music\1
), effectively removing them from the source folder. - 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:
- Files are dragged & dropped into a special folder (e.g.,
C:\DropFilesHere
). - The script automatically detects new files in this folder.
- 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.
- Copied to the secondary destination folder (
- 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
- FileSystemWatcher:
- Detects changes in the folder being monitored.
- Copy-Item:
- Copies the file to the secondary destination (backup).
- Move-Item:
- Moves the file to the primary destination, effectively removing it from the source folder.
- 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:
- FileSystemWatcher:
- Monitors the folder
C:\DropFilesHere
for new files. - The
Created
event is triggered when a new file is added.
- Monitors the folder
- Copy File:
- Files are first copied to the backup location (
E:\Music\1
) usingCopy-Item
. - Reason: The copy ensures that the file remains in the secondary destination even after being moved.
- Files are first copied to the backup location (
- Move File:
- After the file is copied, it is moved to the primary destination (
D:\Music\1
) usingMove-Item
, removing it from the source folder.
- After the file is copied, it is moved to the primary destination (
- 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.
- Infinite Loop:
- The script keeps running with the
while ($true)
loop to continuously monitor the folder.
- The script keeps running with the
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
- Create the folders
C:\DropFilesHere
,D:\Music\1
, andE:\Music\1
for testing. - Run the script and try adding files to
C:\DropFilesHere
. - Verify that the files are successfully:
- Copied to
E:\Music\1
. - Moved to
D:\Music\1
. - Removed from
C:\DropFilesHere
.
- Copied to
7. Troubleshooting
- BurntToast Module Error:
- If BurntToast is not installed, run:
powershell Install-Module -Name BurntToast -Force -SkipPublisherCheck
- If BurntToast is not installed, run:
- Execution Policy Error:
- If you encounter errors related to the Execution Policy, add
-ExecutionPolicy Bypass
when running the script.
- If you encounter errors related to the Execution Policy, add
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! 😊
43 Pro Midjourney Prompts: Uncover the Secret to Creativity!
Script : Definition, Types, Examples & How to Create Them
7 Steps How To Buy Bitcoin on eToro – Act Fast!
49 Stunning Leonardo AI Prompts That Will Blow Your Mind