Skip to content

Instantly share code, notes, and snippets.

@RavinduSachintha
Last active February 27, 2022 22:47
Show Gist options
  • Save RavinduSachintha/05f6611e887931d4321a0fd91f9f81d0 to your computer and use it in GitHub Desktop.
Save RavinduSachintha/05f6611e887931d4321a0fd91f9f81d0 to your computer and use it in GitHub Desktop.
Files bulk moving to own folders script using batch programming for Windows and bash scripting for Linux
@echo off
rem For each file in your folder
for %%a in (".\*") do (
rem Check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~xna" NEQ "FileSorter.bat" (
rem Check if folder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Move the file to directory
move "%%a" "%%~dpa%%~xa\"
)
)
#!/bin/bash
# full directory name of the script no matter where it is being called from
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
command() {
fullname=$(basename -- "$0")
filename="${fullname%.*}"
dirname=${filename// /$''}
mkdir "$dirname" && mv "$fullname" "$dirname"
};
export -f command;
# find files except scripts and execute command method
find $DIR -maxdepth 1 -type f -not -name "*.sh" -exec bash -c 'command "$0"' {} \;
echo "all files moved to folders successfully."
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment