| 1. |
Solve : CMD Window Closing Before Batch Finishes Operations? |
|
Answer» I have a batch file that is doing a lot of work inside a folder. It is creating files, deleting folders, renaming files and folders, moving files and folders, ETC... It currently performs about 50 different operations. The problem I am having is that the command window closes, as if it were finished, but the operations it has set in motion CONTINUE to happen in WINDOWS Explorer for the next 5 to 10 seconds. I'm never sure when it is done. F:\-- Test Directory\New Folder>wait mkdir CM1Quote from: erobby on April 13, 2016, 01:27:31 PM use wait before the commandThere is no WAIT command. Maybe you meant to say use the START command with the WAIT option.New Problem Alright, so "START /WAIT test.bat" opens a new command window with the command processor run with the /K switch to cmd.exe. But this window is just a new command window. If I have to start test.bat by typing its name in again, the point of an automated process is lost. Questions
I was testing this with a smaller bat file, just trying to make sure I knew what I was doing before I ran the big batch file that exhibits the problematic behavior. Even using the WAIT command, the window exits like normal and Windows Explorer continues to show activity for 5 to 10 seconds afterwards. If I don't use the EXIT command at the end of the batch file, the window stays open forever, which I don't want. I just want it to stick around until all the operations the batch has set in motion have completed. Write operations to a drive are typically cached. Windows determines when to perform the write operations most effectively on it's own. For example, internally, command prompts copy command likely uses the appropriate functions to read and write files. The important consideration is that those functions will typically return to the caller before the operation has completed- that is, if you write some data to disk, the function may return before that data has been written to the disk- instead it will be written the next time Windows decides to flush it's write cache for the drive. You might be able to workaround this by using the /V switch on the copy command, although this will only apply to the copy commandQuote from: BC_Programmer on April 13, 2016, 02:51:36 PM Write operations to a drive are typically cached. Windows determines when to perform the write operations most effectively on it's own. For example, internally, command prompts copy command likely uses the appropriate functions to read and write files. The important consideration is that those functions will typically return to the caller before the operation has completed- that is, if you write some data to disk, the function may return before that data has been written to the disk- instead it will be written the next time Windows decides to flush it's write cache for the drive.This makes perfect sense. In this case then, a TIMEOUT will suffice. Thank you Quote from: Mulsiphix on April 13, 2016, 02:23:29 PM and Windows Explorer continues to show activity for 5 to 10 seconds afterwards Some points I can clarify: 1) Adding the "" after the start command as a matter of course will stop it biting you when the command is double quoted. start "c:\File Utilities\give me money.exe" WOULD fail without a leading "" and just open a cmd window without doing anything. Code: [Select]start "" 2) Adding the /B switch to the start command reuses the same console window. See start /? for extra details. 3) Using the call command works for batch files without using start. Exactly what is happening in your code is in the same realm as knowing next weeks winning Tattslotto numbers, and batch scripts could be what you are using. 3) Windows Explorer is also multi-threaded and will do it's own merry thing in some instances. Quote from: foxidrive on April 13, 2016, 08:32:41 PM BINGO! Why people seem to think we can troubleshoot their code without showing us their code is being any logical reason in my small world.Quote from: foxidrive on April 13, 2016, 08:32:41 PM 1) Adding the "" after the start command as a matter of course will stop it biting you when the command is double quoted.I figured this one out after I posted. I swear, some of the things that slip by . Quote from: foxidrive on April 13, 2016, 08:32:41 PM 2) Adding the /B switch to the start command reuses the same console window.I didn't know about either of these. Thank you Quote from: Squashman on April 13, 2016, 08:51:17 PM BINGO!Point made! I'll remember this for the future |
|