Coder Social home page Coder Social logo

hackthacker / folder Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 10 KB

Effortlessly organize files by extension across platforms using this versatile script

Home Page: https://hackthacker.blogspot.com

PowerShell 100.00%
folder-management folder-structure folder-with-files-management

folder's Introduction

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.

folder's People

Contributors

hackthacker avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.