VB Script to Copy Files from Network Share to a new folder in the users profile

Answered VB Script to Copy Files from Network Share to a new folder in the users profile

  • Thursday, May 17, 2012 6:16 AM
     
      Has Code

    Hi All,

    I have a question that stems from my original post: http://social.technet.microsoft.com/Forums/en-US/ITCG/thread/b27b920d-b2f3-4486-a342-bf9aaf65b374/#6f9268b6-f50a-4c01-a83f-97b98269fb97

    The script in this reply work perfectly, however I require one extra step. I need to copy the icons for the shortcut to each of the users profiles under a new folder (e.g. %userprofile%\Icons). I have found some code that I was going to attempt to modify and incorporate to the original script however I cant seem to get it to work. If anyone can assist that would be great. 

    Below is the script I am trying to modify and add to the original script in the post:

    Set WshShell = WScript.CreateObject( "WScript.Shell" )
    Path = WshShell.ExpandEnvironmentStrings("%USERPROFILE%\")
    
    SourceDir = "\\DOMAIN.com.au\netlogon\Shortcuts\"
    FolderName="Icons"
    DestDir = Path & "\" & FolderName
    
    Set oFso = CreateObject("Scripting.FileSystemObject")
    If NOT oFso.FolderExists(Path & "\" & FolderName) Then
    	' Create New Folder
    	set NewFolder=oFso.CreateFolder(FolderName)
    End If
    
    'Copy Files
    Set Source = oFso.GetFolder(SourceDir)
    Set FileList = Source.Files
    For Each File in FileList
    	oFso.CopyFile SourceDir & "\" & File.Name, DestDir & "\" & File.Name      
    Next
    
    Set oFso = Nothing
    Set WshShell = Nothing
    

    The original script from the post is:

    Option Explicit
    
    On Error Resume Next
    
    Dim objShell 
    Dim objDesktop 
    Dim objLink 
    Dim strAppPath 
    Dim strWorkDir 
    Dim strIconPath
    Dim strArgument
    
    strWorkDir ="C:\windows" 
    strAppPath = "C:\Program Files\Google\Chrome\Application\chrome.exe"
    strIconPath = "\\COMPANY.com.au\NETLOGON\Shortcuts\Intranet.ico"
    strArgument="-app=http://intranet"
    
    Set objShell = CreateObject("WScript.Shell") 
    objDesktop = objShell.SpecialFolders("Desktop") 
    Set objLink = objShell.CreateShortcut(objDesktop & "\Intranet.lnk")
    
    
    objLink.Description = "Link to the Intranet"
    objLink.IconLocation = strIconPath 
    objLink.TargetPath = strAppPath 
    objLink.Arguments = strArgument
    objLink.WindowStyle = 3 
    objLink.WorkingDirectory = strWorkDir 
    objLink.Save


All Replies

  • Thursday, May 17, 2012 1:31 PM
     
     Proposed Has Code

    You're doubling up the backslashes in the paths:

      SourceDir  = "\\DOMAIN.com.au\netlogon\Shortcuts\"               ' <-- Already has ending backslash '

      Path       = WshShell.ExpandEnvironmentStrings("%USERPROFILE%\") ' <-- Already has ending backslash '

      FolderName = "Icons"

      DestDir    = Path & "\" & FolderName  ' == "C:\User\somuser\\Icons"  <-- Extra backslash '

      ' In the CopyFile arguments: '

      SourceDir & "\" & File.Name  ' == "\\DOMAIN.com.au\netlogon\Shortcuts\\somefile.ext"  <-- Extra backslash '

    To avoid this:

    • Use FileSystemObject.BuildPath
    • Use WScript.Echo to give yourself debugging feedback

    Also

    • You're calling CreateFolder on the short FolderName variable ("Icons") rather than the full path ("DestDir")
    • You can get the full name of a file from the Path property (rather than Name property)

    All together, here's a script that should work:

    Set oFso = CreateObject("Scripting.FileSystemObject")
    Set WshShell = WScript.CreateObject( "WScript.Shell" )
    Path = WshShell.ExpandEnvironmentStrings( "%USERPROFILE%" ) ' Don't include backslash
    
    SourceDir  = "\\DOMAIN.com.au\netlogon\Shortcuts"  ' Don't include backslash
    FolderName = "Icons"
    DestDir = oFso.BuildPath(Path, FolderName)
    WScript.Echo "DestDir = " & DestDir
    
    If Not oFso.FolderExists(DestDir) Then oFso.CreateFolder DestDir
    
    For Each File in oFso.GetFolder(SourceDir).Files
    	oFso.CopyFile File.Path, oFso.BuildPath(DestDir, File.Name)
    Next
    
    Set oFso = Nothing
    Set WshShell = Nothing


    jmh

  • Thursday, May 17, 2012 5:42 PM
     
     

    Hav e you considered just placing a copy of teh shortcut into teh All Users profilea long with the icon.  This can be done once from any commandline if you are an administrator.  Why make this into such a big project.


    ¯\_(ツ)_/¯

  • Thursday, May 17, 2012 11:19 PM
     
     

    Hav e you considered just placing a copy of teh shortcut into teh All Users profilea long with the icon.  This can be done once from any commandline if you are an administrator.  Why make this into such a big project.


    ¯\_(ツ)_/¯

    I thought about doing this but I would need to run this command from over 200 users workstations. Any suggestions?
  • Thursday, May 17, 2012 11:24 PM
     
     Answered

    Hav e you considered just placing a copy of teh shortcut into teh All Users profilea long with the icon.  This can be done once from any commandline if you are an administrator.  Why make this into such a big project.


    ¯\_(ツ)_/¯

    I thought about doing this but I would need to run this command from over 200 users workstations. Any suggestions?

    Run it remotely.  Just copy the shortcut to the allyusers folders from any workstaion.

    copy shortcut.lnk "\\pc01\c$\Documents And Settings\AllUsers\Desktop"
    copy icon.bmp "\\pc01\c$\Documents And Settings\AllUsers\Application Data\Icons"

    Place a loop on that and read in the WS names and you are done.


    ¯\_(ツ)_/¯

  • Friday, May 25, 2012 3:00 PM
    Moderator
     
     

    As there has been no activity in this thread for a few days, we assume the issue is resolved. We will mark it as "answered" to assist others in similar situations. If you disagree, please reply with further information. You can unmark the answer if you wish. If a reply helped answer your question, please mark it as the answer.


    Richard Mueller - MVP Directory Services