Problem with process, file does'nt exists?

mpals

Member
Joined
Nov 15, 2005
Messages
13
Programming Experience
Beginner
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.

VB.NET:
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.:confused:

Thanks for your time.:rolleyes:
 
i would use System.IO.File.Move instead
VB.NET:
File.Move("C:\a.txt", "C:\b.txt", True)
this will effectively rename a.txt to b.txt

also you can use System.IO.File.Exist to check if a.txt is even there or not too
 
Thanks JuggaloBrotha.
It work for ren a.txt b.txt but (my mistake :eek:) the command could be ren or other DOS command
ehh.gif


I find this solution (but to much code :) ) create file, run shell, and then delete file.... has to have another way.
If File.Exists("c:\a.txt") Then

VB.NET:
             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
VB.NET:
 shell("ren c:\a.txt c:\b.txt")
but gives the same error
sick.gif
sick.gif
Why don't accept the command?!?!

I also try
VB.NET:
 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? :)
user_online.gif
 
Some how i find the answer... :rolleyes:

VB.NET:
Dim psi As ProcessStartInfo = New ProcessStartInfo("cmd.exe")
        psi.RedirectStandardOutput = True
        psi.RedirectStandardInput = True
        psi.UseShellExecute = False
        psi.CreateNoWindow = True

        Dim p As Process = Process.Start(psi)
        p.StandardInput.WriteLine("ren c:\a.txt b.txt")
        p.StandardInput.WriteLine("exit")
It's just necessary format the string of DOS command
ren c:\a.txt b.txt
 
i'm glad to see that you've worked it out :)

but does this have to be using dos commands? if you know what it's supposed to do then you could make everything easier and just use the functions in vb here
 
Back
Top