none
Script for migrating Folders RRS feed

  • Question

  • I need help with a script, we are moving our file structure around. We have thousands of folders with tons of data in them but we want to sort the folders based off of letter of alphabet. Each folder is a name, example Last Name, First Name

    We want to move each folder based off of first letter of last name into folders that are A-B, C-D, E-F, etc

    For example:

    Current Location

    \\fileserver\folder\Dawson, Allen

    New Location:

    \\fileserver\newfolder\C-D\Dawson, Allen

    I have a PowerShell script that does most of this but it doesn’t keep the folder permissions like robocopy does.

    Attached is the script I currently have

    #This script will display a folder dialogue to choose a "Clients" folder
    #It will then look at what the first letter of each folder, and copy that 
    #folder to it's new home.
    
    #create misc variables
    $datetime = Get-Date -Format "yyyyMMddhhmmss";
    $Copydestination = "Insert Copy Destination Here"
    
    Function Get-FolderName()
    {
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    
    $OpenFolderDialog = New-Object System.Windows.Forms.FolderBrowserDialog
    $OpenFolderDialog.ShowDialog() | Out-Null
    $OpenFolderDialog.SelectedPath 
    
    get-childitem -path $OpenFolderDialog.SelectedPath -filter *.* | foreach-object -process { 
    	#get the first letter of the folder name
    	$first_letter = ($_.name.Substring(0,1))
    
    	#if the first letter of the folder is A, COPY the folder and its contents to the new folder
    	if($first_letter -eq "a") {Copy-item $_.FullName -destination "$Copydestination\A-B" -recurse}
    
    	#if the first letter of the folder is B, COPY the folder and its contents to the new folder
    	elseif($first_letter -eq "b") {Copy-item $_.FullName -destination "$Copydestination\A-B" -recurse}
    	elseif($first_letter -eq "c") {Copy-item $_.FullName -destination "$Copydestination\C-D" -recurse}
        	elseif($first_letter -eq "d") {Copy-item $_.FullName -destination "$Copydestination\C-D" -recurse}
        	elseif($first_letter -eq "e") {Copy-item $_.FullName -destination "$Copydestination\E-F" -recurse}
        	elseif($first_letter -eq "f") {Copy-item $_.FullName -destination "$Copydestination\E-F" -recurse}
    	elseif($first_letter -eq "g") {Copy-item $_.FullName -destination "$Copydestination\G-H" -recurse}
        	elseif($first_letter -eq "h") {Copy-item $_.FullName -destination "$Copydestination\G-H" -recurse}
        	elseif($first_letter -eq "i") {Copy-item $_.FullName -destination "$Copydestination\I-J" -recurse}
        	elseif($first_letter -eq "j") {Copy-item $_.FullName -destination "$Copydestination\I-J" -recurse}
    	elseif($first_letter -eq "k") {Copy-item $_.FullName -destination "$Copydestination\K-L" -recurse}
        	elseif($first_letter -eq "l") {Copy-item $_.FullName -destination "$Copydestination\K-L" -recurse}
        	elseif($first_letter -eq "m") {Copy-item $_.FullName -destination "$Copydestination\M-N" -recurse}
        	elseif($first_letter -eq "n") {Copy-item $_.FullName -destination "$Copydestination\M-N" -recurse}
    	elseif($first_letter -eq "o") {Copy-item $_.FullName -destination "$Copydestination\O-P" -recurse}
        	elseif($first_letter -eq "p") {Copy-item $_.FullName -destination "$Copydestination\O-P" -recurse}
        	elseif($first_letter -eq "q") {Copy-item $_.FullName -destination "$Copydestination\Q-R" -recurse}
        	elseif($first_letter -eq "r") {Copy-item $_.FullName -destination "$Copydestination\Q-R" -recurse}
    	elseif($first_letter -eq "s") {Copy-item $_.FullName -destination "$Copydestination\S-T" -recurse}
        	elseif($first_letter -eq "t") {Copy-item $_.FullName -destination "$Copydestination\S-T" -recurse}
        	elseif($first_letter -eq "u") {Copy-item $_.FullName -destination "$Copydestination\U-V" -recurse}
        	elseif($first_letter -eq "v") {Copy-item $_.FullName -destination "$Copydestination\U-V" -recurse}
    	elseif($first_letter -eq "w") {Copy-item $_.FullName -destination "$Copydestination\W-X" -recurse}
        	elseif($first_letter -eq "x") {Copy-item $_.FullName -destination "$Copydestination\W-X" -recurse}
        	elseif($first_letter -eq "y") {Copy-item $_.FullName -destination "$Copydestination\Y-Z" -recurse}
        	elseif($first_letter -eq "z") {Copy-item $_.FullName -destination "$Copydestination\Y-Z" -recurse}
    	
    	#and just keep on going down the alphabet
    	#elseif... (and so on)
    								
    	}
    } #end function Get-FolderName
    
    # *** Entry Point to Script ***
    Get-FolderName 

    Saturday, February 2, 2019 4:04 PM

Answers

  • Copying never maintains folder permissions because the file is a new file which gains the permissions set by the destination folder.  "Moving" a file retains permissions on moves within the current volume but loses them in moves to different volumes.  This is normal Windows behavior.  RoboCopy circumvents this by copying the permissions if requested.

    You can use Copy-Item and then set the permissions on the target or just use RoboCopy.

    From MS docs:

    "By default, an object inherits permissions from its parent object, either at the time of creation or when it is copied or moved to its parent folder. The only exception to this rule occurs when you move an object to a different folder on the same volume. In this case, the original permissions are retained."

    For a complete explanation see; https://support.microsoft.com/en-us/help/310316/how-permissions-are-handled-when-you-copy-and-move-files-and-folders


    \_(ツ)_/


    • Proposed as answer by Tony MCP Sunday, February 3, 2019 12:25 PM
    • Marked as answer by Richard MuellerMVP, Moderator Saturday, February 9, 2019 11:01 AM
    • Edited by jrv Saturday, February 9, 2019 11:06 AM
    Saturday, February 2, 2019 4:39 PM