Use regular expression to auto-organize files using PowerShell alternative replacement tool

File organization can be a daunting task, especially when you’re dealing with hundreds or thousands of files. With Copywhiz, you can automate this process efficiently. In this blog post, we’ll walk you through a step-by-step guide to setting up an automated system to organize your files by their names using Copywhiz and PowerShell.

What is Copywhiz?

Copywhiz enhances your file-copy experience by letting you to have more flexibility & control over the file copying and backup process.

Here is how Copywhiz makes file copying a breeze:

  • Copy only new or modified files
  • Easily pick files by name, extension, folder etc from bunch of folders
  • Automatically organize files based on file attributes and metadata
  • Copy files to multiple folders/computers
  • Copy files from multiple folders and paste them at once
  • Sync files between source and destination
  • Pick files from multiple folders and compress in single .zip file
  • Schedule file backups
  • Verify copied files for data integrity
  • Retain security attributes of files & folders (ownership/user access)
  • Several more hidden nuggets designed to make your file-copy task easier.

Let’s take a look at the below video on how we can achieve the above feature:

Video Transcription:

  1. In this video, we will see how we can use expressions to organize files in seperate folders.
  2. As we see, we have a folder having various employee names. Let’s copy the files and we shall create the folder as per the last name.
  3. Right-click and add it to Copywhiz.
  4. Go to the destination folder, right-click and choose Copywhiz Paste Advanced.
  5. Click on Advanced settings button.
  6. In the destination tab, select organize files inside folders based on name, type, date etc.
  7. Then click on the organizer button and select the criteria to organize the files.
  8. Type the expression and click on Ok and paste now.

Using PowerShell Script:

PowerShell can parse file names using regex and organize them into appropriate folders. Here’s a sample script:

# Define source and destination directories
$sourceDir = "C:\SourceFiles"
$destDir = "C:\OrganizedFiles"

# Define regex pattern
$pattern = "^(Invoice|Report)_(\d{4})_(\w+)\.(pdf|docx)$"

# Process each file in the source directory
Get-ChildItem -Path $sourceDir -File | ForEach-Object {
    $fileName = $_.Name

    if ($fileName -match $pattern) {
        $category = $matches[1]  # Invoice or Report
        $year = $matches[2]      # Year

        # Create destination folder path
        $targetPath = Join-Path -Path $destDir -ChildPath "$category\$year"

        # Ensure the target directory exists
        if (!(Test-Path -Path $targetPath)) {
            New-Item -Path $targetPath -ItemType Directory
        }

        # Move the file to the appropriate folder
        Move-Item -Path $_.FullName -Destination $targetPath
    }
}

Conclusion

Copywhiz provides a straightforward GUI for quick and efficient tasks. Choose the tool that fits your specific needs or use both for the best of both worlds!

Learn more about Copywhiz. Download the free trial from here.

For short tutorial videos, Check out this page.

Subscribe to our YouTube channel for interesting videos.

Check out our other cool products.

Have a good day!

Leave a Reply

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