locked
xcopy help RRS feed

  • Question

  • hi, can someone tell me how to get the files being copied into a list box for display ??

    the code

    Dim ProcID As Integer

    ' start xcopy.

    With ProcID = Shell("xcopy C:\DOCUME~1\*.dbx/s/h c:\backup\", AppWinStyle.Hide)

    End With

    Tuesday, June 27, 2006 12:30 AM

Answers

  • I would think you mean a textbox, but I'll split the contents of a file into individual lines and display each line in a listbox.

    General Example - in this case creates a text file and then reads it in and displays it in a list box and a textbox.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            '//Create a Textbox file with 3 lines in
            Dim strcontent As String = "vb1line1" & vbCrLf & "vbline2" & vbCrLf & "vbline3"
            My.Computer.FileSystem.WriteAllText("C:\test.txt", strcontent, False)

            ProcessFileIntoIndividualLines("C:\test.txt")  '//Into listbox
            ProcessFileIntoTextbox("C:\test.txt") '//Into Textbox
        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal Filename As String)
            Dim str As String = My.Computer.FileSystem.ReadAllText(Filename)
            Dim sa As String() = str.Split(vbCrLf)

            For Each x As String In sa
                Dim lXReplace As String = x.Replace(vbLf, "").Replace(vbCr, "")
                ListBox1.Items.Add(lXReplace)
            Next
        End Sub

        Sub ProcessFileIntoTextbox(ByVal Filename As String)
            Dim str As String = My.Computer.FileSystem.ReadAllText(Filename)
            Textbox1.Text = str
        End Sub
    End Class


     

    Tuesday, June 27, 2006 1:10 AM
  • My incorrect interpretation of what was being asked.   For taking the output and writing to a listbox the following should work.  This will write each line of the output to a new item in the listbox.


    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim procInfo As New ProcessStartInfo
            With procInfo
                .FileName = "xcopy"
                .Arguments = " C:\DOCUME~1\*.dbx/s/h c:\backup\"
                .RedirectStandardOutput = True
                .UseShellExecute = False
                .CreateNoWindow = True
            End With
            Dim proc As Process = Process.Start(procInfo)
            Dim str As String = proc.StandardOutput.ReadToEnd()
            proc.WaitForExit()

            ProcessFileIntoIndividualLines(str)

        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal str As String)
            Dim sa As String() = Str.Split(vbCrLf)

            For Each x As String In sa
                Dim lXReplace As String = x.Replace(vbLf, "").Replace(vbCr, "")
                ListBox1.Items.Add(lXReplace)
            Next
        End Sub

    End Class

    Tuesday, June 27, 2006 2:51 PM
  • So final code is

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim procInfo As New ProcessStartInfo
            With procInfo
                .FileName = "xcopy"
                .Arguments = " C:\DOCUME~1\*.dbx/s/h c:\backup\"
                .RedirectStandardOutput = True
                .RedirectStandardInput = True
                .UseShellExecute = False
                .CreateNoWindow = True
            End With
            Dim proc As Process = Process.Start(procInfo)
            Dim str As String = proc.StandardOutput.ReadToEnd()
            proc.WaitForExit()

            ProcessFileIntoIndividualLines(str)

        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal str As String)
          Dim crlf() As String = {vbCrLf}
          Dim sa As String() = str.Split(crlf, StringSplitOptions.None)

          Me.ListBox1.Items.Clear
          For Each x As String In sa
            Me.ListBox1.Items.Add(x)
          Next
        End Sub


    End Class

    Wednesday, June 28, 2006 11:46 PM

All replies

  • I would think you mean a textbox, but I'll split the contents of a file into individual lines and display each line in a listbox.

    General Example - in this case creates a text file and then reads it in and displays it in a list box and a textbox.

    Public Class Form1

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

            '//Create a Textbox file with 3 lines in
            Dim strcontent As String = "vb1line1" & vbCrLf & "vbline2" & vbCrLf & "vbline3"
            My.Computer.FileSystem.WriteAllText("C:\test.txt", strcontent, False)

            ProcessFileIntoIndividualLines("C:\test.txt")  '//Into listbox
            ProcessFileIntoTextbox("C:\test.txt") '//Into Textbox
        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal Filename As String)
            Dim str As String = My.Computer.FileSystem.ReadAllText(Filename)
            Dim sa As String() = str.Split(vbCrLf)

            For Each x As String In sa
                Dim lXReplace As String = x.Replace(vbLf, "").Replace(vbCr, "")
                ListBox1.Items.Add(lXReplace)
            Next
        End Sub

        Sub ProcessFileIntoTextbox(ByVal Filename As String)
            Dim str As String = My.Computer.FileSystem.ReadAllText(Filename)
            Textbox1.Text = str
        End Sub
    End Class


     

    Tuesday, June 27, 2006 1:10 AM
  • Here's an example of how to do it.  Create a form with a button and an oversized multiline textbox.  Then:

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim procInfo As New ProcessStartInfo
        With procInfo
          .FileName = "cmd.exe"
          .Arguments = "/c dir c:\windows"
          .RedirectStandardOutput = True
          .UseShellExecute = False
          .CreateNoWindow = True
        End With
        Dim proc As Process = Process.Start(procInfo)
        Me.TextBox1.Text = proc.StandardOutput.ReadToEnd()
        proc.WaitForExit()
      End Sub

    You can do it asynchronously by implementing the OutputDataReceived and Exited event handlers of the Process class.

    Tuesday, June 27, 2006 1:49 PM
    Moderator
  • My incorrect interpretation of what was being asked.   For taking the output and writing to a listbox the following should work.  This will write each line of the output to a new item in the listbox.


    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim procInfo As New ProcessStartInfo
            With procInfo
                .FileName = "xcopy"
                .Arguments = " C:\DOCUME~1\*.dbx/s/h c:\backup\"
                .RedirectStandardOutput = True
                .UseShellExecute = False
                .CreateNoWindow = True
            End With
            Dim proc As Process = Process.Start(procInfo)
            Dim str As String = proc.StandardOutput.ReadToEnd()
            proc.WaitForExit()

            ProcessFileIntoIndividualLines(str)

        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal str As String)
            Dim sa As String() = Str.Split(vbCrLf)

            For Each x As String In sa
                Dim lXReplace As String = x.Replace(vbLf, "").Replace(vbCr, "")
                ListBox1.Items.Add(lXReplace)
            Next
        End Sub

    End Class

    Tuesday, June 27, 2006 2:51 PM
  •  

    ........................................................................................................

    Spotty,

    I can not get your last example code to work. (I do add a '/y' parameter to the xcopy arguments btw.)

    The xcopy part of the code will ONLY work if I comment out

    .RedirectStandardOutput = True

    (and of course the listbox part will not work if the above is commented out)

     

    Imports System

    Imports System.Text

    Imports System.IO

    Imports System.Diagnostics

    Imports System.Threading

    Imports System.ComponentModel

    Imports Microsoft.VisualBasic

     

     

    Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim procInfo As New ProcessStartInfo

    With procInfo

    ' first set of filename/arguments work

    ' second and third set (commented out) do not work

    .FileName = "cmd"

    .Arguments = "/c" & "dir" & vbCrLf

    '.FileName = "cmd"

    '.Arguments = "/c" & "xcopy C:\DOCUME~1\*.txt /s/h/y c:\backup\" & vbCrLf

     

    '.FileName = "xcopy"

    '.Arguments = "C:\DOCUME~1\*.txt /s/h/y c:\backup\"

    .RedirectStandardOutput = True

    .UseShellExecute = False

    .CreateNoWindow = True

    End With

    Dim proc As New Process

    proc.StartInfo = procInfo

    proc.Start()

    Dim str As String = proc.StandardOutput.ReadToEnd()

    proc.WaitForExit()

    proc.Close()

    ProcessFileIntoIndividualLines(str)

    End Sub

    Sub ProcessFileIntoIndividualLines(ByVal str As String)

    Dim sa As String() = str.Split(vbCrLf)

    For Each x As String In sa

    Dim lXReplace As String = x.Replace(vbLf, "").Replace(vbCr, "")

    Me.ListBox1.Items.Add(lXReplace)

    Next

    End Sub

    End Class

     

    Wednesday, June 28, 2006 12:09 AM
  • xcopy is a weird program.  I finally found the fix by adding this line:
      .RedirectStandardInput = True
    That took a few hairs...

    There's a minor problem with the Split() function, none of the overloads accepts a string.  I fixed it like this:
      Sub ProcessFileIntoIndividualLines(ByVal str As String)
        Dim crlf() As String = {vbCrLf}
        Dim sa As String() = str.Split(crlf, StringSplitOptions.None)
        For Each x As String In sa
          Me.ListBox1.Items.Add(x)
        Next
      End Sub

    • Proposed as answer by weciii Wednesday, November 23, 2011 6:42 PM
    Wednesday, June 28, 2006 7:24 PM
    Moderator
  • NoBugz,

    Thanks for the 'xcopy' fix.

     Your original 'split' has been working okay for me so far. Ummmm?

    Wednesday, June 28, 2006 7:38 PM
  • Put "Option Strict On" at the top of the program to see the problem.

    Wednesday, June 28, 2006 7:50 PM
    Moderator
  • So final code is

    Public Class Form1

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim procInfo As New ProcessStartInfo
            With procInfo
                .FileName = "xcopy"
                .Arguments = " C:\DOCUME~1\*.dbx/s/h c:\backup\"
                .RedirectStandardOutput = True
                .RedirectStandardInput = True
                .UseShellExecute = False
                .CreateNoWindow = True
            End With
            Dim proc As Process = Process.Start(procInfo)
            Dim str As String = proc.StandardOutput.ReadToEnd()
            proc.WaitForExit()

            ProcessFileIntoIndividualLines(str)

        End Sub

        Sub ProcessFileIntoIndividualLines(ByVal str As String)
          Dim crlf() As String = {vbCrLf}
          Dim sa As String() = str.Split(crlf, StringSplitOptions.None)

          Me.ListBox1.Items.Clear
          For Each x As String In sa
            Me.ListBox1.Items.Add(x)
          Next
        End Sub


    End Class

    Wednesday, June 28, 2006 11:46 PM
  • Thanks Spotty,

    Your 'spotty' code is now 'spotless!

    (Forum owners, please don't shoot me for the bad pun )

    Wednesday, June 28, 2006 11:55 PM
  • thank you!!!!!!!!!!!!!!!!!
    Saturday, July 1, 2006 3:30 AM
  •  nobugz wrote:
    xcopy is a weird program.  I finally found the fix by adding this line:
      .RedirectStandardInput = True
    That took a few hairs...

    Thanks nobugz!  I was pulling my hair out and this post just kept me from going bald.  How did you ever figure that one out?!?

    Tuesday, October 3, 2006 10:52 PM