Site icon Bernard Aybout's Blog – MiltonMarketing.com

Scripted Harmony: Automating File Sorting into Named Folders

Scripted Harmony Automating File Sorting into Named Folders

Scripted Harmony Automating File Sorting into Named Folders

To use the “Scripted Harmony: Automating File Sorting into Named Folders” program, start by placing the batch script in the folder containing the files you wish to organize. This script is designed to be flexible, allowing you to either organize all files within the directory or focus specifically on files with a particular extension. For general use, simply double-click the batch file to execute it, and it will automatically create new folders named after each file (excluding the file extension) and move the files into their respective folders. This process tidies up your directory, making it easier to navigate and manage your files.

If you want to organize files by a specific extension, run the script through a command prompt window. Open the command prompt, navigate to the directory where the script and your files are located, and then type the name of the batch file followed by the extension you’re interested in organizing, like so: `OrganizeFiles.bat txt` for text files. The script will then provide a preview of the action it will perform, such as showing an example file and the folder it will be moved into, and ask for your confirmation to proceed. Confirm to start the organization process, or cancel if you change your mind. This intuitive script offers a straightforward solution to declutter your digital workspace efficiently.

[wpdm_package id=’23275′]

Here is the code to the batch script your downloading. Step by step explanation. You can edit and view the code that’s running. Its safe.

Copy to Clipboard

Determine File Extension to Process

set "ext=*.*"

This sets a variable ext with the value *.*, which means all files.

The following if statement checks if the first command-line argument (%~1) is not empty:

  • If it’s not empty, the script updates ext to target files with the specified extension and prints a message about looking for files with that extension:
    set "ext=*.%~1"
    echo Looking for files with the '.%~1' extension...
  • If it is empty, it prints a message indicating it’s looking for all files:
    echo Looking for all files...

Check for Files to Process, Excluding the Script Itself

set "filesFound=false"
set "exampleFile="

Initializes filesFound to false and exampleFile to an empty string.

Uses a for loop to iterate over all files matching the extension defined in ext:

for %%F in (%ext%) do (
    if "%%~fF" neq "%~f0" (
        set "filesFound=true"
        if not defined exampleFile set "exampleFile=%%F"
    )
)

Inside the loop, it checks if the file being processed (%%F) is not the script itself (%~f0). If true:

  • Sets filesFound to true.
  • If exampleFile is not defined, sets it to the current file (%%F). This captures the first file that is not the script.

If No Files Found, Explain Usage and Exit

Checks if no files were found (filesFound remains false):

if "!filesFound!"=="false" (
    echo No files found to organize. Here's how you can use this script:
    echo -------------------------------------------------------------------------
    echo - Place this script in a folder with files you want to organize.
    echo - Run it without arguments to organize all files, or with an extension to focus on those files.
    echo   Example to organize only text files: %~nx0 txt
    echo -------------------------------------------------------------------------
    pause
    exit /b
)

If true, prints instructions on how to use the script and then pauses and exits the script.

Show an Example of What Will Happen

If exampleFile is defined (i.e., there is at least one file to process):

set "exampleName=!exampleFile:~0,-4!"
echo For example, if we have a file named '!exampleFile!', it will be moved into a folder named '!exampleName!'.

Prints a message showing what will happen to exampleFile — it will be moved into a folder named after itself minus the extension.

Ask for User Confirmation to Proceed

Prints a prompt asking if the user wants to continue with the operation:

echo.
echo This script will now start organizing files in the same manner as shown.
echo Do you want to continue with this operation? (Y/N)
set /p userChoice=
if /i not "%userChoice%"=="Y" (
    echo Operation cancelled by the user. No changes have been made.
    pause
    exit /b
)

Reads the user’s choice. If the choice is not ‘Y’ (case-insensitively), it cancels the operation, informs the user, and exits.

Main Loop for Organizing Files

If the user confirms to proceed, it enters another for loop over the files:

for %%F in (%ext%) do (
    if "%%~fF" neq "%~f0" (
        set "fileName=%%~nF"
        set "dirName=%%~nF"

        :: Create directory if it does not exist
        if not exist "!dirName!" (
            echo Creating folder: !dirName!
            mkdir "!dirName!"
        )
        
        :: Move the file into its new folder
        echo Organizing: %%F
        move "%%F" "!dirName!"
    )
)

Checks again if the current file is not the script. Sets fileName and dirName to the name of the file (without the extension). Checks if a directory with the name of the file doesn’t exist. If it doesn’t, creates the directory. Moves the file into the newly created directory and prints a message indicating the operation.

Completion Message

After all files have been processed, prints a message indicating that the organization is complete and then pauses.

Exit mobile version