PowerShell File Copy & Directory Check


        <# ------------------------------------------------------------------------------------------------------------------------

        So ... a massive FTP server with a huge Dir structure needed coped to new server.

        Dir structure was initially copied over for testing and put in production.

        Somehow... someone still had access to the old server via FTP and client/s were uploading files. 

        My task was to create a script that would copy file from old server to the new server.

        Seems pretty simple but someone had changed the Dir structure/s      

        So now I had to copy files from point A to Point B but only to matching Dir on new server

         ---------------------------------------------------------------------------------------------------------------------------#>
        cls
        
#----------------------------------
        #File Log for Good and Bad Results
        #----------------------------------

        $LogFile = "C:\Temp\MoveitLog-" + "_$(get-date -format "yyyyMMdd_hhmmsstt").txt"
        $LogFileError = "C:\Temp\MoveitLogError-" + "_$(get-date -format "yyyyMMdd_hhmmsstt").txt"
        
#-----------------------------------------------------------------------
        #      Gets the list of all files from folder and all sub folers
        #----------------------------------------------------------------------- 

        $Files=Get-ChildItem -file C:\temp2 -Recurse | select fullname, directory 
        #Counter
        $x=1
 
      #-------------------------
        # Loops through All Files
        #-------------------------

        Foreach($File in $Files) {        
        #------------------------------------------------------------------
        # Returns the path / directory of the file - drops the file name
        #------------------------------------------------------------------

        $New_String = $File.Directory.ToString()        
        #-----------------------------------------------------------
        # Drops the start of the directoy path - not needed for UNC 
        #-----------------------------------------------------------

        $New_String = $New_String.Trim("C:\")        
        
        Try {

                #--------------------------------------------------------------------------
                #Actually attempts to move item from current location to remote location  
                #--------------------------------------------------------------------------

                move-item $File.fullname "\\YourServer\Test_Share\$New_String\"  -ErrorAction Stop                
                "$x. $Dates Success- File Name: " + $File.fullname.ToString() + " move to location: \\YourServer\Test_Share\$New_String\" | Out-File $LogFile -Append
                Write-host "$x. $Dates Success- File Name: " + $File.fullname.ToString() + " move to location: \\YourServer\Test_Share\$New_String\"
            }
        Catch
            {
                #For Date time Stamping 
                $Dates=Get-Date
                #Error message of file and remote location of where its trying to place it.
                "$x. Error $Dates - File Name: " + $File.fullname + " move to location: \\YourServer\Test_Share\$New_String\ - Error Message: $_ " | Out-File $LogFileError -Append
                write-host "Error $Dates - File Name: " + $File.fullname + " move to location: \\YourServer\Test_Share\$New_String\ - Error Message: $_ "
        }        
        #Counter Incremental
        $x=$x+1
        }