Skip to content

Effortlessly organize files by extension across platforms using this versatile script

Notifications You must be signed in to change notification settings

hackThacker/folder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Best Cross-Platform File Organizer Script

Certainly, I'd be happy to explain the code step by step:

  1. $rootDirectory = Get-Location: This line retrieves the current location or path where the script is executed and assigns it to the variable $rootDirectory.

  2. function Move-FilesByExtension { ... }: This line defines a PowerShell function named Move-FilesByExtension that takes a single parameter, $rootDirectory. The purpose of this function is to organize files in the specified directory based on their file extensions into different subfolders.

  3. Get-ChildItem -Path $rootDirectory -Directory | ForEach-Object { ... }: This part of the code gets a list of directories within the $rootDirectory and iterates through each one. It checks if the directory is located outside of the $rootDirectory and deletes it if so. This is done to clean up any stray directories that are not within the specified root.

  4. $mainFolders = @('Images', 'Documents', 'Audio', 'Videos', 'Zip', 'Other'): An array named $mainFolders is created, containing the names of the main folders where files will be organized.

  5. $mainFolders | ForEach-Object { ... }: This loop iterates through each main folder name in the $mainFolders array. It checks if each main folder exists within the $rootDirectory and creates it if it doesn't.

  6. $fileExtensions = @{}, $logEntries = @{}, and $totalFilesMoved = 0: These variables are initialized. $fileExtensions will be used to store file extensions, $logEntries to store log entries, and $totalFilesMoved to keep track of the number of files moved.

  7. Get-ChildItem -Path $rootDirectory -File -Recurse | ForEach-Object { ... }: This loop goes through each file within the $rootDirectory and its subdirectories. For each file, it determines the appropriate destination folder based on its file extension and moves the file to that folder.

    The code uses a series of if and elseif statements to categorize files into image, document, audio, video, zip, or other types, based on their extensions. For example, image files have extensions like 'jpg', 'png', 'gif', etc.

    It also handles cases where files with certain extensions need to be further organized into subfolders within the main folders, based on their extensions.

  8. Within each category of files, if a file has a valid extension, it moves the file to the appropriate subfolder, creates subfolders if needed, and logs the file move operation with a timestamp and details.

  9. If files have been moved, the script adds various log entries, like "Files moved successfully," "Total files moved," and "Logging process completed," to the log file.

  10. Start-Sleep -Seconds 10 and exit: These lines make the script pause for 10 seconds before exiting. This might be intended to allow time to read the log messages before the script terminates.

  11. Move-FilesByExtension -rootDirectory $rootDirectory: This line actually calls the Move-FilesByExtension function, passing in the $rootDirectory variable that was set initially.

In summary, the script is designed to organize files within a specified directory ($rootDirectory) into subfolders based on their file extensions. It categorizes files into main folders like Images, Documents, Audio, Videos, Zip, and Others, and then further organizes files within those categories into subfolders based on their extensions. Additionally, it logs the file move operations along with timestamps and details to a log file.

To run a PowerShell script (.ps1) on Windows, Linux, and macOS, you'll need to use different methods on each platform. Here's how you can do it:

  1. Running PowerShell Scripts on Windows:

    On Windows, PowerShell is natively available, so running a .ps1 script is straightforward.

    • Open PowerShell (you can search for it in the Start menu).
    • Use the cd command to navigate to the directory where your script is located.
    • Run the script using its full path or by typing .\scriptname.ps1.
  2. Running PowerShell Scripts on Linux and macOS:

    On Linux and macOS, PowerShell Core (also known as PowerShell 7+) is available, which is a cross-platform version of PowerShell. Here's how you can run the script:

    • Install PowerShell Core if you haven't already. You can find installation instructions for your specific Linux distribution or macOS version on the official PowerShell GitHub repository.
    • Open a terminal.
    • Use the cd command to navigate to the directory where your script is located.
    • Run the script using its full path or by typing ./scriptname.ps1. Make sure to prefix the script with ./.

It's important to note that while PowerShell Core provides cross-platform compatibility, there might still be differences in behavior between platforms due to system-specific features and commands used in the script. Be sure to test your script thoroughly on each platform to ensure it works as expected.

Additionally, some security settings might prevent scripts from running on certain platforms. You might need to adjust the execution policy to allow script execution. On PowerShell, you can change the execution policy using the Set-ExecutionPolicy command. For example:

Set-ExecutionPolicy RemoteSigned

This sets the execution policy to allow locally created scripts to run. However, be cautious when adjusting execution policies, as it can affect the security of your system.

Always be mindful of the potential security risks when running scripts, especially from untrusted sources.

About

Effortlessly organize files by extension across platforms using this versatile script

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published