Solved Need help with timing and waiting until process ends

July 15, 2012 at 08:21:52
Specs: Windows XP
I am currently trying to make a program in Visual Basic ( not .net or scipting or anything else, just the regular one) in which the following happens:

1: A folder is created
2: Files are copied to that folder
3: Files in that folder undergo a change
4: The files in that folder are then copied to another folder
5: The folder is then deleted

I am basically trying to make a file copied to a new folder where a batch changes its name and then the files are copied to a different folder. Then the folder that was created is destroyed. The problem is that once the system starts to copy the folders, it doesn't wait until that process is done, it just goes on to the next process of deleting or renaming. I have tried looking at other forums and doing research but I can't seem to do it correctly. Here is my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
My.Computer.FileSystem.CreateDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-23")
My.Computer.FileSystem.CreateDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-3")
If CheckBox1.CheckState = 0 Then
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\SAVE", "C:\Documents and Settings\Allan\Desktop\Monograms\123", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\SAVE", "C:\Documents and Settings\Allan\Desktop\Monograms\-23", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\SAVE", "C:\Documents and Settings\Allan\Desktop\Monograms\-3", FileIO.UIOption.AllDialogs, FileIO.UICancelOption.DoNothing)
End If
If CheckBox1.CheckState = 1 Then
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\Save", "C:\Documents and Settings\Allan\Desktop\Monograms\123", True)
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\Save", "C:\Documents and Settings\Allan\Desktop\Monograms\-23", True)
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\Save", "C:\Documents and Settings\Allan\Desktop\Monograms\-3", True)
End If
System.Diagnostics.Process.Start("C:\Documents and Settings\Allan\Desktop\Monograms\Properties\Renamer1.bat")
System.Diagnostics.Process.Start("C:\Documents and Settings\Allan\Desktop\Monograms\Properties\Renamer2.bat")
System.Diagnostics.Process.Start("C:\Documents and Settings\Allan\Desktop\Monograms\Properties\Renamer3.bat")
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-23", "C:\Documents and Settings\Allan\Desktop\Monograms\23")
My.Computer.FileSystem.CopyDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-3", "C:\Documents and Settings\Allan\Desktop\Monograms\3")
My.Computer.FileSystem.DeleteDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-23", FileIO.DeleteDirectoryOption.DeleteAllContents)
My.Computer.FileSystem.DeleteDirectory("C:\Documents and Settings\Allan\Desktop\Monograms\-3", FileIO.DeleteDirectoryOption.DeleteAllContents)
End Sub

So if you study this code carefully, you can see that I want this to go one at a time. The problem with timers is that they automatically have a set time, but they don't know how long it will take to copy the files. Sometimes the file size will be huge, sometimes the file size will be small. I want the code to do this one at a time and wait until the other process has finished! Please try to adjust and customize the code to my scenerio since when people put a

*.* or other symbols I don't really know,

I don't understand how to apply it to my case...

So just for my case please try to adjust the code so I don't need to worry about certain places I need to fill in.
Sorry about the length of my problem.
Please write back ASAP since I need to get this ground covered already.
Much appreciated,

See More: Need help with timing and waiting until process ends

Report •

July 15, 2012 at 15:19:06
Are you sure you're not hanging up on running the scripts? Because you need to use Process.WaitForExit().
Dim cmds As New List(Of String) From {"cmd", "notepad", "calc"}
cmds.ForEach(Sub(c) Process.Start(c).WaitForExit())

Or as it applies to your code:

System.Diagnostics.Process.Start("C:\Documents and Settings\Allan\Desktop\Monograms\Properties\Renamer1.bat").WaitForExit()

How To Ask Questions The Smart Way

Report •

July 15, 2012 at 16:33:10
I see what you're saying! So far I'm 50% there. You see, the code" .waitforexit() "
is very good for starting a process, correct?
But I also have files being copied and I don't really see a way to add it to the copying part. Since, right after it is copied, the directory undergoes deletion. The code you gave me works half way, which is not bad, but I also need the deletion process to wait until the copying is done. (Again timers don't work over here.)

Please try to figure out how to postpone the deletion until only after the copying is done.
And please try somehow be able to put a "wait for exit" for the action of copying files.

Report •

July 15, 2012 at 22:55:03
✔ Best Answer
You might have to resort to copying the files (maybe using batch) instead of the copydirectory, to give you more control of the timing. This batch might get the last file to be copied in *.* order:
for %%a in (source\*) do set lastfile=%%~nxa
then look for that last file to appear in the target directory before proceeding:
copy source\*.* dest
if not exist dest\%last% goto :a
which wil hang the batch in a loop until all the files are present.

Report •

Related Solutions

July 16, 2012 at 15:06:43
Thank you! I didn't really understand your method, but you sparked an idea! So instead of Visual Basic copying files, I decided to have a batch do it. Then since I had a batch doing it, I was able to control it and give it a ".WaitForExit()". Thanks a lot for the help!

Report •

Ask Question