0

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"
Community
  • 1
  • 1
  • This question has a serious case of TL;DR! I'd suggest you edit it to narrow down to the problems you're having with it not the background to why it exists. Also avoid asking multiple questions within a question. – arco444 Nov 05 '14 at 17:43
  • Fixed TL;DR, Sorry about that folks! – Ctheman001 Nov 05 '14 at 17:52

1 Answers1

0

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"
}
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • What version are you working on? I dont seem to have that installed on my Win7. I will try updating PS and will get back to this... Thank you however for the post! – Ctheman001 Nov 06 '14 at 18:37
  • I have used this on PowerShell v3 and v4 on Win7 and PowerShell v4 on Win8.1 – TheMadTechnician Nov 06 '14 at 20:14
  • Sweet! After some debugging (aka when you change something in the script Powershell ISE v4 doesn't treat the updated script with a clean state), I was able to get it to select both MyDST and MySRC and run correctly in about 20 seconds (moving a few known files back and forth). Thank you greatly @TheMadTechnician! – Ctheman001 Nov 07 '14 at 16:50