Results 1 to 12 of 12

Thread: Problem with process, file doesn't exists?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Resolved Problem with process, file doesn't exists?

    Hello to all.
    This is my first post, i believe that is easily solve

    I have a file a.txt in c:\ and want to rename it to b.txt, this should run in "batch" i have this code to do it.
    Code:
    If File.Exists("c:\a.txt") Then
                Dim p As Process
                p = New Process
    
                p.StartInfo.FileName = "ren"
                p.StartInfo.WorkingDirectory = "C:\"
                p.StartInfo.Arguments = "C:\a.txt b.txt"
                p.Start()
               
            Else
                sError = New LogsFile("File doesn't exists.", "ERROR: ")
                sError.NotifyLogs()   ' write to log file the error
            End If
    On p.start() gives the exception file can be found.

    Thanks for your time.
    Last edited by mpals; Dec 19th, 2005 at 05:10 AM.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Problem with process, file doesn't exists?

    VB Code:
    1. If System.IO.File.Exists("c:\a.txt") Then
    2.  
    3.             System.IO.File.Move("C:\a.txt", "C:\b.txt")
    4.  
    5. ...

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Problem with process, file doesn't exists?

    Thanks mendhak.
    It work for ren a.txt b.txt but (my mistake) the command could be ren or other DOS command

    I find this solution (but to much code ) create file, run shell, and then delete file.... has to have another way.

    Code:
    If File.Exists("c:\a.txt") Then
         
                Try
                    ' File.Move("c:\a.txt", "c:\b.txt")
    
                    ' Need to create comm.bat with the DOS command  EX: ren c:\a.txt b.txt
                    Shell("c:\comm.bat", AppWinStyle.Hide) 
                  
    
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
    
    
                ' Finally delete c:\comm.bat
                Try
                    File.Delete("c:\comm.bat")
                Catch ex As Exception
                    MessageBox.Show(ex.Message)
                End Try
    
            Else
                sError = New LogsFile("File doesn't exists.", "ERROR: ")
                sError.NotifyLogs()
            End If

    I try to run
    Code:
    shell("ren c:\a.txt c:\b.txt")
    but gives the same error Why don't accept the command?!?!

    I also try
    Code:
     'System.Diagnostics.Process.Start("ren", "c:\a.txt b.txt")
    and gives the same error !!!!! But here i understand why, "ren" is "read" like a file!!!

    Any other ideia?

  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Problem with process, file doesn't exists?

    It didn't work because ren is not a file, it's a dos command.

    What's wrong with the suggestion I gave? What are you trying to accomplish?

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Problem with process, file doesn't exists?

    Quote Originally Posted by mendhak
    It didn't work because ren is not a file, it's a dos command.

    What's wrong with the suggestion I gave? What are you trying to accomplish?
    Nothing wrong with your suggestion

    but the command could be ren or any other DOS command...
    i didn't say this in first post, but in second i correct it

  6. #6
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Problem with process, file doesn't exists?

    Now I see what you mean.

    Do this:

    VB Code:
    1. Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    2.         psi.RedirectStandardOutput = True
    3.         psi.RedirectStandardInput = True
    4.         psi.UseShellExecute = False
    5.         psi.CreateNoWindow = True
    6.  
    7.         Dim p As Process = Process.Start(psi)
    8.         p.StandardInput.WriteLine("ren c:\a.txt b.txt")
    9.         p.StandardInput.WriteLine("exit")

    HTH

  7. #7

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Resolved Re: Problem with process, file doesn't exists?

    Quote Originally Posted by mendhak
    Now I see what you mean.

    Do this:

    VB Code:
    1. Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    2.         psi.RedirectStandardOutput = True
    3.         psi.RedirectStandardInput = True
    4.         psi.UseShellExecute = False
    5.         psi.CreateNoWindow = True
    6.  
    7.         Dim p As Process = Process.Start(psi)
    8.         p.StandardInput.WriteLine("ren c:\a.txt b.txt")
    9.         p.StandardInput.WriteLine("exit")

    HTH

    Perfect

    Thanks very much mendhak, do exacty what i want.

    One more time. Thanks.

  8. #8
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: Problem with process, file doesn't exists?

    Great, now add [Resolved] to the thread title in the first post of this thread.

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Problem with process, file doesn't exists?

    Quote Originally Posted by mendhak
    Great, now add [Resolved] to the thread title in the first post of this thread.
    Done

  10. #10
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,173

    Re: [Resolved] Problem with process, file doesn't exists?

    For error handling,

    VB Code:
    1. Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    2.         psi.RedirectStandardOutput = True
    3.         psi.RedirectStandardInput = True
    4.         psi.RedirectStandardError = True
    5.  
    6.         psi.UseShellExecute = False
    7.         psi.CreateNoWindow = True
    8.  
    9.         Dim p As Process = Process.Start(psi)
    10.  
    11.  
    12.         p.StandardInput.WriteLine("ren c:\a.txt b.txt")
    13.         p.StandardInput.WriteLine("exit")
    14.         Dim strErr As String = p.StandardError.ReadToEnd()

    strErr will contain the error.

  11. #11

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: [Resolved] Problem with process, file doesn't exists?

    One more time, Thanks mendhak

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2005
    Posts
    33

    Re: Problem with process, file doesn't exists?

    Hello again. One more time a question about this.

    Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
    psi.RedirectStandardOutput = True
    psi.RedirectStandardInput = True
    psi.RedirectStandardError = True

    psi.UseShellExecute = False
    psi.CreateNoWindow = True

    Dim p As Process = Process.Start(psi)


    p.StandardInput.WriteLine("copy c:\a.txt b.txt")
    p.StandardInput.WriteLine("exit")
    Dim strErr As String = p.StandardError.ReadToEnd()
    The b.txt already exists... wait for confirmation Yes/No/All

    How can i give the anwser? Yes/No/All

    I try to put
    VB Code:
    1. p.StandardInput.WriteLine("y")
    before
    VB Code:
    1. p.StandardInput.WriteLine("exit")
    didn't work...
    and then after and didn't work to.

    Anyone knows how i can do this?

    Thanks.
    Before this post you . After this post you . Then please Rate it .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width