Ihave been working on a script that will move everything from my downloads folder to another folder on a external drive (We will now call this "Backup Drive"), sorting it into folders by extension (found it on StackOverflow for the initial start --Thanks to Nicola Cossu--). Every once in a while I need to use the script to be able to pull stuff from another drive to the Backup Drive.
I would like to have ONE script that will ask me for Source and Destination drive, but would like it to do is have my Downloads Folder and Backup Drive Folder to be predefined.
One of the ways I thought would work would be to have the dialog box that pops up, have the location of each question already filled to allow me to edit it only when I need to. I am not sure on how to do this.
If it needs to be hard coded to variables $MySRC and $MyDST, that is fine. Thanks in advance!!!
Here is my code:
# Get Start Time of the Script
$startDTM = (Get-Date)
# Get Source and Destination -- this is where I am trying to get the predetermine variables to be
$source = Read-Host "Enter for Source"
$dest = Read-Host "Enter for Destination"
# This could be where my variables are located
# $MySRC = "C:\Users\ME\Downloads"
# $MyDST = "E:\Sorted Downloads"
# Logging information for personal reasons
echo ""
echo "This script is starting"
echo ""
echo "The source is "
$source
echo ""
echo "The destination is "
$dest
echo ""
echo ""
# Actual Script to Run
$file = gci -Recurse $source | ? {-not $_.psiscontainer}
$file | group -property extension |
% {if(!(test-path(join-path $dest -child $_.name.replace('.','')))) { new-item -type directory $(join-path $dest -child $_.name.replace('.','')).toupper() }}
$file | % { move-item $_.fullname -destination $(join-path $dest -child $_.extension.replace(".",""))}
# End of Script Information -- Personal Reasons
echo "This Script is Done!"
echo 'Source: ' $source ' Destination: ' $dest
echo ""
# Get End Time:
$endDTM = (Get-Date)
# Echo Time Elapsed for the Script to Run:
"Elapsed Time: $(($endDTM-$startDTM).totalseconds) seconds"
This could be done with windows forms, and a simple function. I keep this on hand just in case I need to prompt for a folder:
Function Get-FolderPath{
[CmdletBinding()]
Param(
[String]$Description,
[String]$InitialDirectory = "C:\",
[Switch]$NewFolderButton = $false)
[void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
$FolderBrowserDialog = New-Object System.Windows.Forms.FolderBrowserDialog
$FolderBrowserDialog.SelectedPath = $InitialDirectory
$FolderBrowserDialog.Description = $Description
$FolderBrowserDialog.ShowNewFolderButton = $NewFolderButton
If($FolderBrowserDialog.ShowDialog() -eq "OK"){
$FolderBrowserDialog.SelectedPath
}
}
Usage is as such:
$MySRC = Get-FolderPath "Select source folder or drive:" "$env:USERPROFILE\Downloads"
That will pop up a windows folder selection dialog box that asks for a source folder. There is also a switch to show a New Folder button on the dialog. It would be used as such:
$MyDST = Get-FolderPath "Select the destination folder:" "E:\Sorted Downloads" -NewFolderButton
I recommend using it in a While loop to force the user to select things, or put in an If to exit the script if $MySRC or $MyDST is blank.
While([string]::IsNullOrWhiteSpace($MySRC)){
$MySRC = Get-FolderPath "Select source folder or drive:" "$env:USERPROFILE\Downloads"
}