Copy a directry from a network drive to a users desktop - Visual Basic

Michael Smith 1 Reputation point
2022-12-21T04:58:38.973+00:00

Hi,
I have been working on a small script to help with the setting up of users' workstations and have hit a brick wall.

What I am wanting to accomplish is that by selecting a role in the Listbox and clicking on button1 a network directory gets copied to the current user's desktop.

I am sure I am missing something easy and would greatly appreciate any and all help.
yes, I do love my .bat scripts

Please see the below code I have been working on

Imports System.Net.Mail  
Public Class Form1  
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        ' add items to the listbox  
        With lbDepartments  
            .Items.Add("Bookings")  
            .Items.Add("Logistics")  
            .Items.Add("Materials")  
            .Items.Add("Programming")  
            .Items.Add("Traffic")  
  
        End With  
    End Sub  
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
        Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)  
        If lbDepartments.SelectedIndex = 0 Then  
            My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\BOOKING SETUPS\", "desktop\setup\", True)  
        End If  
  
    End Sub  
    'If lbDepartments.SelectedIndex = 1 Then  
  
  
    'If bDepartments.SelectedIndex = 2 Then  
  
  
    'If lbDepartments.SelectedIndex = 3 Then  
  
  
    'If lbDepartments.SelectedIndex = 4 Then  
  
    'End Sub  
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click  
        ' Change language with batch file  
        Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\region.bat")  
    End Sub  
  
    Private Sub Button3_click(sender As Object, e As EventArgs) Handles Button3.Click  
        ' change list seperator with batch file  
        Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\list_seperator.bat")  
  
    End Sub  
  
    Private Sub Button4_click(sender As Object, e As EventArgs) Handles Button3.Click  
        ' change region with batch file  
        Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\region.bat")  
  
    End Sub  
  
    Private Sub Button5_click(sender As Object, e As EventArgs) Handles Button3.Click  
        ' change time and date with batch file  
        Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\time_and_date.bat")  
  
    End Sub  
End Class  
  
Windows for business | Windows Client for IT Pros | User experience | Other
0 comments No comments
{count} votes

12 answers

Sort by: Most helpful
  1. EckiS 916 Reputation points
    2022-12-21T07:49:29.513+00:00

    "yes, I do love my .bat scripts" that aint no batch file.

    you should add the tag "dotnet-visual-basic" to increase the chances of getting an answer.

    0 comments No comments

  2. MotoX80 36,406 Reputation points
    2022-12-21T14:51:36.177+00:00

    Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
    If lbDepartments.SelectedIndex = 0 Then
    My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\BOOKING SETUPS\", "desktop\setup\", True)

    You define a variable named "s" that contains the path to the user's desktop folder, but you don't use the variable anywhere. The destination folder is "desktop\setup\" which would be relative to whatever the current working directory for the program is.

    This below might be what you want, I'm not sure of your requirements. Search the file system for the files that you are copying.

    My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\BOOKING SETUPS\", s & "\setup\", True)  
    
    0 comments No comments

  3. MotoX80 36,406 Reputation points
    2022-12-22T00:22:22.09+00:00

    Well, if you are running a bat file, we can't help you until you share its contents. So I assume that your question is about the CopyDirectory statement.

    Add diagnostic displays to verify that you looking at the folder names that you think you are processing.

         Dim strDestFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test\"  
            Dim dirinfo As IO.DirectoryInfo  
            Console.WriteLine("The destination folder is " & strDestFolder)  
            If My.Computer.FileSystem.DirectoryExists(strDestFolder) Then  
                Console.WriteLine("The folder exists!")  
                dirinfo = My.Computer.FileSystem.GetDirectoryInfo(strDestFolder)  
                Console.WriteLine("Current file count is " & dirinfo.GetFiles.Count)  
            Else  
                Console.WriteLine("The folder does not exists!")  
            End If  
            My.Computer.FileSystem.CopyDirectory("c:\temp\tdrive\", strDestFolder, True)  
            dirinfo = My.Computer.FileSystem.GetDirectoryInfo(strDestFolder)  
            Console.WriteLine("File count is now " & dirinfo.GetFiles.Count)  
            Console.ReadLine()  
      
      
    

    273081-image.png

    273072-image.png

    0 comments No comments

  4. Michael Smith 1 Reputation point
    2022-12-21T23:02:15.1+00:00

    I still cannot get the folders to copy to the desktop or even C:\Users\michael.smith\Desktop\setup
    any and all help is greatly appreciated

    Imports System.Net.Mail  
    Public Class Form1  
      
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
            ' add items to the listbox  
            With lbDepartments  
                .Items.Add("Bookings")  
                .Items.Add("Logistics")  
                .Items.Add("Materials")  
                .Items.Add("Programming")  
                .Items.Add("Traffic")  
      
            End With  
        End Sub  
        Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click  
            'Move setup files to desktop  
            Dim s As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)  
            If lbDepartments.SelectedIndex = 0 Then  
                My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\BOOKING SETUPS\", s & "\setup\", True)  
            End If  
            'Copy Logistics files to desktop  
            If lbDepartments.SelectedIndex = 1 Then  
                My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\LOGISTICS SETUPS", s & "\setup\", True)  
            End If  
            'Copy Materials files to desktop  
            If lbDepartments.SelectedIndex = 2 Then  
                My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\MATERIALS SETUPS\", s & "\setup\", True)  
            End If  
            'Copy Programming files to desktop  
            If lbDepartments.SelectedIndex = 3 Then  
                My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\PROGRAMMING SETUPS\", s & "\setup\", True)  
            End If  
            'Copy Traffic files to desktop  
            If lbDepartments.SelectedIndex = 4 Then  
                My.Computer.FileSystem.CopyDirectory("O:\Common\COMPUTER_SETUPS\1-DEPARTMENTS\TRAFFIC SETUPS\", s & "\setup\", True)  
            End If  
      
        End Sub  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button2.Click  
            ' Change language with batch file  
            Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\region.bat")  
        End Sub  
      
        Private Sub Button2_click(sender As Object, e As EventArgs) Handles Button3.Click  
            ' change list seperator with batch file  
            Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\list_seperator.bat")  
      
        End Sub  
      
        Private Sub Button3_click(sender As Object, e As EventArgs) Handles Button3.Click  
            ' change region with batch file  
            Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\region.bat")  
      
        End Sub  
      
        Private Sub Button4_click(sender As Object, e As EventArgs) Handles Button3.Click  
            ' change time and date with batch file  
            Shell("O:\Common\COMPUTER_SETUPS\Setup_Scripts\time_and_date.bat")  
      
        End Sub  
      
      
    End Class  
      
    
    0 comments No comments

  5. Michael Smith 1 Reputation point
    2022-12-22T03:49:45.777+00:00

    I still can't seem to get it to work, sorry about all the noob questions.
    I am still learning and really appreciate all the help and tips

        Private Sub Button0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button0.Click  
            'Move setup files to desktop  
            Dim strDestFolder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\test\"  
            Dim dirinfo As IO.DirectoryInfo  
            'Move Bookings files to desktop  
            If lbDepartments.SelectedIndex = 0 Then  
                Console.WriteLine("The destination folder is " & strDestFolder)  
                My.Computer.FileSystem.DirectoryExists(strDestFolder)  
                Console.WriteLine("The folder exists!")  
            Else  
                dirinfo = My.Computer.FileSystem.GetDirectoryInfo(strDestFolder)  
                Console.WriteLine("Current file count is " & dirinfo.GetFiles.Count)  
                Console.WriteLine("The folder does not exists!")  
                Else  
                My.Computer.FileSystem.CopyDirectory("c:\temp\tdrive\", strDestFolder, True) Then  
                    dirinfo = My.Computer.FileSystem.GetDirectoryInfo(strDestFolder)  
                    Console.WriteLine("File count is now " & dirinfo.GetFiles.Count)  
                    Console.ReadLine()  
                End If  
            'If lbDepartments.SelectedIndex = 0 Then  
            'End If  
            'Copy Logistics files to desktop  
            'If lbDepartments.SelectedIndex = 1 Then  
            'End If  
            'Copy Materials files to desktop  
            ' If lbDepartments.SelectedIndex = 2 Then  
            'End If  
            'Copy Programming files to desktop  
            ' If lbDepartments.SelectedIndex = 3 Then  
            ' End If  
            'Copy Traffic files to desktop  
            ' If lbDepartments.SelectedIndex = 4 Then  
            'End If  
      
        End Sub  
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.