Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27061

VS 2012 Recursively copy file to directory shortcuts within a selected directory

$
0
0
Hello, looking for some help. I'm trying to fix some code I gathered from various sites to recursively copy a selected file into all the subdirectories of a selected folder. The program works when the subdirectories are the actual folders however I'm having trouble getting it to work when there are shortcuts to the directories. Reason being: Computer lab teachers have created shortcuts to all of their student's home folders on the servers and organized them by class. I wanted to save them time and give them a tool to easily copy files to all their students. I've been searching to figure out how to get it to work with shortcuts and I'm having trouble. I can piece things together, but writing code is not a strength.

Here's what I began with not knowing shortcuts to directories would behave differently:

Code:

Imports System.IO

Public Class Form1

' Select the file '
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim ofd As New OpenFileDialog
        ofd.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        If ofd.ShowDialog = DialogResult.OK Then
            TextBox1.Text = ofd.FileName
        End If
    End Sub

' Select class to copy to '
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim fbd As New FolderBrowserDialog
        fbd.RootFolder = Environment.SpecialFolder.Desktop
        If fbd.ShowDialog = DialogResult.OK Then
            TextBox2.Text = fbd.SelectedPath
        End If
    End Sub

' Copy selected file into all folders of selected class '
    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

        Dim dirs As DirectoryInfo() = New DirectoryInfo(TextBox2.Text).GetDirectories()

        For Each d As DirectoryInfo In dirs
            File.Copy(TextBox1.Text, Path.Combine(d.FullName, Path.GetFileName(TextBox1.Text)), True)
        Next

    End Sub

End Class

I've tried the following using just a file from TextBox1 but I really need to pass the whole folder from TextBox2 and parse each shortcut. I also get a "Declaration expected" error on he 2nd line with this.
Code:

Dim file As String  = (don't know how to pass the folder location from TextBox2)
        Dim shortCut As IWshRuntimeLibrary.IWshShortcut
        shortCut = CType((New IWshRuntimeLibrary.WshShell).CreateShortcut(file), IWshRuntimeLibrary.IWshShortcut)

This was my original snippet I began with and have been troubled with the "set" lines.

Code:

'process the shortcuts
ModifyLinks CheckFolder

Sub ModifyLinks (foldername)

  dim file        'for stepping through the files collection        '
  dim folder      'for stepping through the subfolders collection  '
  dim fullname    'fully qualified link file name                  '
  dim link        'object connected to the link file                '

set wso = CreateObject("Wscript.Shell")
set fso = CreateObject("Scripting.FileSystemObject")

  'process all the files in the folder
  For each file in fso.GetFolder(foldername).Files

        'Find full path of shortcut
        fullname = fso.GetAbsolutePathName(file)

        'Find full path of target within shortcut
        set link = wso.CreateShortcut(fullname)
        targetpath = LCase(link.targetpath)

Any help and direction is appreciated.

Viewing all articles
Browse latest Browse all 27061

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>