Skip to content

Instantly share code, notes, and snippets.

@DMeurer
Last active September 2, 2024 06:36
Show Gist options
  • Save DMeurer/63cf73685375d481ff40b3ed5e576316 to your computer and use it in GitHub Desktop.
Save DMeurer/63cf73685375d481ff40b3ed5e576316 to your computer and use it in GitHub Desktop.
A wrapper for robocopy asking for the paths and filling in the rest. Can be used as automatable cli or just startet with some prompts.
$paramMode = $args[0]
$paramSource = $args[1]
$paramDestination = $args[2]
$paramSilent = $args[3]
$silent = (($paramSilent -eq "-s") -or ($paramSilent -eq "--silent"))
if ($silent -eq $false)
{
Write-Host "$( [char]0x1b )[38;5;51m ______ ___ _______________ $( [char]0x1b )[38;5;84m _________ $( [char]0x1b )[0m
$( [char]0x1b )[38;5;51m ___ |/ /___ ____ /_ /___(_) $( [char]0x1b )[38;5;84m __ ____/__________________ __$( [char]0x1b )[0m
$( [char]0x1b )[38;5;51m __ /|_/ /_ / / /_ /_ __/_ / $( [char]0x1b )[38;5;84m _ / _ __ \__ __ \_ / / /$( [char]0x1b )[0m
$( [char]0x1b )[38;5;51m _ / / / / /_/ /_ / / /_ _ / $( [char]0x1b )[38;5;84m / /___ / /_/ /_ /_/ / /_/ / $( [char]0x1b )[0m
$( [char]0x1b )[38;5;51m /_/ /_/ \__,_/ /_/ \__/ /_/ $( [char]0x1b )[38;5;84m \____/ \____/_ .___/_\__, / $( [char]0x1b )[0m
$( [char]0x1b )[38;5;51m $( [char]0x1b )[38;5;84m /_/ /____/ $( [char]0x1b )[0m"
Write-Host "Script by: Dominik Meurer"
Write-Host "-------------------------------------"
Write-Host ""
Write-Host "This script uses RoboCopy to copy or empty folders."
Write-Host "Please make sure to test if it works like you expect before ."
Write-Host ""
Write-Host "Direct CLI usage: (ignore if you only want to use it like this)"
Write-Host "MultiCopy.ps1 [C,e] [source] [destination] [-s]"
Write-Host ""
Write-Host "Options:"
Write-Host "C: Copy folders"
Write-Host "e: Empty folders"
Write-Host "-s: Silent mode"
Write-Host "-------------------------------------"
Write-Host ""
}
function Copy-Folders
{
param (
[string]$source,
[string]$destination
)
if (($source -eq "") -or (-not (Test-Path $source)))
{
Write-Host "$( [char]0x1b )[31mSource folder does not exist$( [char]0x1b )[0m"
exit
}
if (($destination -eq "") -or (-not (Test-Path $destination)))
{
Write-Host "$( [char]0x1b )[31mDestination folder does not exist$( [char]0x1b )[0m"
exit
}
robocopy $source $destination /mt:16 /e /log:copylog.txt
if ($silent -eq $false)
{
Get-Content copylog.txt -tail 8
}
Remove-Item copylog.txt
}
function Remove-Folders
{
param (
[string]$folder
)
if (($folder -eq "") -or (-not (Test-Path $folder)))
{
Write-Host "$( [char]0x1b )[31mFolder does not exist$( [char]0x1b )[0m"
exit
}
$folder_name = "MultiCopy_empty_folder_$( Get-Date -Format "MM_dd_yyyy__HH_mm_ssss" )"
New-Item -Name $folder_name -ItemType "directory"
robocopy $folder_name $folder /mt:16 /mir /log:copylog.txt
if ($silent -eq $false)
{
Get-Content copylog.txt -tail 7
}
Remove-Item copylog.txt
If (Test-Path $folder_name)
{
Remove-Item $folder_name
}
}
# check if parameters are set
if (($null -ne $paramMode) -and ($null -ne $paramSource) -and ($null -ne $paramDestination))
{
# Papameters are set, so these are used
if ($paramMode -eq "C")
{
Copy-Folders $paramSource $paramDestination
}
elseif ($paramMode -eq "e")
{
Remove-Folders $paramDestination
}
else
{
Write-Host "$( [char]0x1b )[31mInvalid choice$( [char]0x1b )[0m"
exit
}
exit
}
else
{
# Parameters are not set, so ask the user
$copy_or_remove = Read-Host "Do you want to copy or empty a folder? [C,e]"
if (($copy_or_remove -eq "C") -or ($copy_or_remove -eq ""))
{
$source = Read-Host "Enter the source folder path"
$destination = Read-Host "Enter the destination folder path"
Copy-Folders $source $destination
}
elseif ($copy_or_remove -eq "e")
{
$folder = Read-Host "Enter the folder path"
Remove-Folders $folder
}
else
{
Write-Host "$( [char]0x1b )[31mInvalid choice$( [char]0x1b )[0m"
exit
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment