none
VB.NET\robocopy UNC with spaces RRS feed

  • Question

  • I am creating a tool to backup my work files to a folder on my user drive named backup.  My user director looks like this:  
    \\server.dm1.dm2.dm3.gov\data folder

    I have tried using variable and the full path directly in the command.  I get the following error...  
    ERROR : Invalid Parameter #3 : "folder\Backup\"

    I have tried the commands in the following ways...


    robocopy " & source2 & " " & Dest1 & " /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XD .cisco .nuget " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget /XA:SH /MT:30"
    
    robocopy C:\Users\username\\server.dm1.dm2.dm3.gov\data folder\Backup  /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"
    
    robocopy C:\Users\username "\\server.dm1.dm2.dm3.gov\data folder\Backup" /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"
    
    Dim Dest1 As String = ("\\server.dm1.dm2.dm3.gov\data folder\Backup\Backup\")
    
    robocopy C:\Users\username " & Dest1 & " /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"
    
    Robocopy C:\Users\username \\server.dm1.dm2.dm3.gov\data folder\Backup /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"
            
    
    
    Dim p As New System.Diagnostics.Process
    Dim stInfo As New ProcessStartInfo("cmd.exe")
    stInfo.Arguments = "/k robocopy C:\Users\username \\server.dm1.dm2.dm3.gov\data folder\Backup /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"        stInfo.UseShellExecute = True
            

    stInfo.UseShellExecute = True
    p.StartInfo = stInfo p.StartInfo.WindowStyle = ProcessWindowStyle.Normal p.Start()


    These don't work and get the above error on both commands using the UNC path.  Everything I try except on 2 local directories with no spaces fails.  Can someone help me?

     



    • Edited by Chet.Horton Thursday, April 2, 2020 10:45 PM
    Thursday, April 2, 2020 10:40 PM

Answers

  • None of the suggestions work with the process arguments so I just decided to go with the drive letter and directory and skip all the junction points in my profile and not worry about about any of the issues that may arise by not using the UNC path.  I have permissions on everything I am backing up and this isn't going to be used as a migrating tool where everything will need to match perfectly when restored/moved to the new server it won't matter.
    • Marked as answer by Chet.Horton Tuesday, April 7, 2020 12:10 PM
    Tuesday, April 7, 2020 12:10 PM

All replies

  • Hi

    Suggestion: navigate to the destination using File Explorer. Shift-Right Click on the destination folder name and select 'copy as path' - then paste that into your code and try that.


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, April 2, 2020 11:29 PM corrected click to RIGHT CLICK
    Thursday, April 2, 2020 11:22 PM
  • I will give it a try.  I went to the destination and got the path from explorer bar at the top.  I also pasted into the start menu like we use to be able to do start + run and it resolved the path and opened it.  I didn't do what you suggested but I will try because I haven't done that yet.
    Friday, April 3, 2020 12:15 AM
  • It didn't work because that option isn't available on my system.  Maybe it is hidden by GPO or doesn't exist in Windows 10.  I don't think it would have made a difference anyway because I could paste the path into the start menu and it would resolve then open the folder on my user directory.

    I have it working but it isn't optimal.  I have it using the drive letter instead of the UNC path and I read an article stating to always use UNC paths when copying to servers.  https://adamtheautomator.com/robocopy-the-ultimate/.
    Friday, April 3, 2020 12:55 PM
  • Hi Chet,

    With Startinfo it is almost always the same thing that gives the problem. 

    The arguments, and especially the quotes in that. 

    Your code is not simple to change without the ability to test. But have a look at this page (and set it to VB in top of that) which gives an extensive explanation.

    https://docs.microsoft.com/en-us/dotnet/api/system.diagnostics.processstartinfo.arguments?view=netframework-4.8


    Success
    Cor


    Friday, April 3, 2020 3:34 PM
  • does this look for the UNC path with spaces?

    \\server.dm1.dm2.dm3.gov\\"\"\"data folder\"\"\"\\Backup

    Friday, April 3, 2020 4:14 PM
  • Hello,

    See if the 75 point reply might work for you.

    https://stackoverflow.com/questions/12027987/how-to-copy-directories-with-spaces-in-the-name


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, April 3, 2020 7:16 PM
    Moderator
  • I wrote a front-end for robocopy a while ago and found it best to enclose all paths n double quotes, even if they did not contain spaces - here's a test I just did which ran with no issues - 

    Command Line:
    robocopy "D:\Arc" "\\DL4100\Public\Backup Folder With Spaces     Lots of Spaces" /R:0 /W:0 /NS /NC /NDL /XJ /E /MOT:5 /MT:8 /XD "*$RECYCLE.BIN*" "*System Volume Information*"

    -------------------------------------------------------------------------------
       ROBOCOPY     ::     Robust File Copy for Windows
    -------------------------------------------------------------------------------

      Started : 4 April, 2020 7:57:41 AM
       Source : D:\Arc\
         Dest : \\DL4100\Public\Backup Folder With Spaces     Lots of Spaces\

        Files : *.*

     Exc Dirs : *$RECYCLE.BIN*
                *System Volume Information*

      Options : *.* /NS /NC /NDL /S /E /DCOPY:DA /COPY:DAT /XJ /MOT:5 /MT:8 /R:0 /W:0

    ------------------------------------------------------------------------------
    100%                            D:\Arc\bin\7z.dll
    100%                            D:\Arc\bin\7zG.exe
    462 other filenames ommitted
    ------------------------------------------------------------------------------

                   Total    Copied   Skipped  Mismatch    FAILED    Extras
        Dirs :       165       165         1         0         0         0
       Files :       464       464         0         0         0         0
       Bytes :   29.23 m   29.23 m         0         0         0         0
       Times :   0:00:35   0:00:04                       0:00:00   0:00:00


       Speed :             7464353 Bytes/sec.
       Speed :             427.113 MegaBytes/min.
       Ended : 4 April, 2020 7:57:46 AM
    Saturday, April 4, 2020 3:08 PM
  • was that from a batch file or command prompt?  I am doing through vb.net with System.Diagnostics.Process and these arguments:  
    Arguments = "/k robocopy C:\Users\username \\server.dm1.dm2.dm3.gov\data folder\Backup /MIR /R:5 /W:15 /XJ /XJD /XF *.dat *.db *.zip *.ini *.inf *.pol *.log *.properties *.blf *.regtrans-ms /XA:SH /MT:30 /XD " & source1 & "\AppData " & music & " " & video & " " & trace & " " & srch & " " & sg & " " & OneD & " " & edgeBack & " " & intelProf & " " & Down & " " & Contacts & " " & ThreeD & " .cisco .nuget"

    I can do the same as you did from the command prompt and it works fine.  The issue is getting the arguments to recognize this path:  

    \\server.dm1.dm2.dm3.gov\data folder\Backup

    Which I have tried with quotation marks before and after exactly like you did in your example with no success.  I also tried using a variable for the UNC path and one for just the data folder with no success.

    Sunday, April 5, 2020 12:00 AM
  • I copied your path and pasted into my app and it still doesn't recognized the path.  Here is snapshot..  you can't see the last quote for the arguments because it is off the screen but I did as much as I could and you can see what it recognizes and what it doesn't.
    Sunday, April 5, 2020 1:44 AM
  • Here is the path I used:  \\server.dm1.dm2.dm3.gov\data folder\Backup

    This is the error I get ...  ERROR : Invalid Parameter #3 "Folder\Backup"

    Sunday, April 5, 2020 1:55 AM
  • It should be "\\server.dm1.dm2.dm3.gov\data folder\Backup"

    The space between "data" and "folder" makes that into two parameters:
    \\server.dm1.dm2.dm3.gov\data
    and
    folder\Backup

    As I said, enclose it all in quotes

    Sunday, April 5, 2020 8:09 PM
  • That doesn't work.  I tried that...  as I said I did.  I have tried..

    "\\server.dm1.dm2.dm3.gov\data folder\Backup"

    Dim datadir as string

    "\\server.dm1.dm2.dm3.gov\" & datadir & "\Backup"

    and the whole path as a variable.

    Enclosing in quotes DOES NOT WORK in any way I have tried with the process arguments above.  If I were executing straight from the command prompt then yes it works but the argument does not work.

    Monday, April 6, 2020 3:33 AM
  • None of the suggestions work with the process arguments so I just decided to go with the drive letter and directory and skip all the junction points in my profile and not worry about about any of the issues that may arise by not using the UNC path.  I have permissions on everything I am backing up and this isn't going to be used as a migrating tool where everything will need to match perfectly when restored/moved to the new server it won't matter.
    • Marked as answer by Chet.Horton Tuesday, April 7, 2020 12:10 PM
    Tuesday, April 7, 2020 12:10 PM
  • I guess you have resolved it but


    Dim datadir as string = "\\server.dm1.dm2.dm3.gov\" & datadir & "\Backup"

    should be

    Dim datadir as string = chr(34) & "\\server.dm1.dm2.dm3.gov\" & datadir & "\Backup" & chr(34)

    Tuesday, April 7, 2020 5:38 PM
  • Sorry for the late response.  Thanks.  I will play around with your suggestion.  I have been playing around with different ways to learn as much as I can.  I tried this and it was a lot faster.

    Private Async Sub pbStart_Click(sender As Object, e As EventArgs) Handles pbStart.Click
            Fold = tbFolder.Text
            'This runs on the UI thread
            Console.WriteLine("Starting...")
    
            'This runs on a Thread Pool thread
            Dim result As Integer = Await Task.Run(Function()
                                                       workResult =
                                                       EmptyFolder(Fold)
                                                       Return workResult
                                                       Console.WriteLine("Thread Finished")
                                                   End Function)
    
    
            'This runs again on the UI thread
            Console.WriteLine("UI Finished")
    
        End Sub
    
    
        Function EmptyFolder(ByVal pathName As String) As Boolean
            Dim errors As Boolean = False
            Dim dir As DirectoryInfo = New DirectoryInfo(pathName)
    
            For Each fi As FileInfo In dir.EnumerateFiles()
    
                Try
                    fi.IsReadOnly = False
                    fi.Delete()
    
                    While fi.Exists
                        System.Threading.Thread.Sleep(10)
                        fi.Refresh()
                    End While
    
                Catch e As IOException
                    Debug.WriteLine(e.Message)
                    errors = True
                End Try
            Next
    
            For Each di As DirectoryInfo In dir.EnumerateDirectories()
    
                Try
                    EmptyFolder(di.FullName)
                    di.Delete()
    
                    While di.Exists
                        System.Threading.Thread.Sleep(10)
                        di.Refresh()
                    End While
    
                Catch e As IOException
                    Debug.WriteLine(e.Message)
                    errors = True
                End Try
            Next
    
            Return Not errors
        End Function
    I will try your suggestion to go through the UNC instead of the drive letter and see if that makes a difference with System.Diagnostics.Process.

    Sunday, April 19, 2020 6:21 PM