Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: want to remove USB drive after executing .bat  (Read 4787 times)

0 Members and 1 Guest are viewing this topic.

larvalgeek

  • Guest
want to remove USB drive after executing .bat
« on: November 26, 2006, 10:33:52 PM »
I've checked the forums using what strings I could think of in hopes of finding the question answered, but maybe I'm just not supplying the correct search strings.  I apologize if this has been answered before.

I'm writing a batch file that copies a specific folder off of my thumbdrive to the desktop, excecutes a .exe contained in that folder, and then deletes the folder on the desktop when the .exe is done running.  

I'm actually using three .bat files, one to do the copying, one to run the file then restart the computer, and finally, one which is located in the startup folder that is basically a cleanup .bat that checks to see if the folder is present, if so, delete it, then delete itself, otherwise, just delete itself.

I'm fairly new to .bat programing, but I have taken a few courses a year or so ago in Visual Basic.  However, I specifically need to use .bat files for this particular project for work.

The machines I will be running this on will be Windows XP SP2, of many different brands of hardware, but they will all be new computers.

The problem I'm running into is that I can get everything to run smoothly, *if* I leave my thumbdrive in the computer.  The goal is to get the files copied, the second batch file called, and then I can remove my thumbdrive.  Currently, when I do, the second batch file stops running before executing the shutdown command.  The .exe that I am running takes a while to run, so it is imperative that the batch file waits until this .exe is finished before executing the shutdown command.

This is the core of the code I've written so far, I won't trouble you with all the lines of gibberish.

First .bat
Code: [Select]
SET Desktop="C:\documents and settings\all users\Desktop"
SET GSSTARTUP="C:\Documents and Settings\All Users\Start Menu\Programs\Startup"


XCOPY customizer %Desktop%\Customizer\ /H /E /Y
COPY .\customizer\cleanup.bat %GSSTARTUP%
START "Running Customizer" /SEPARATE "C:\Documents and Settings\All Users\Desktop\Customizer\runcust.bat"

And the second batch file
Code: [Select]
SET Desktop="C:\documents and settings\all users\Desktop"
SET GSSTARTUP="C:\Documents and Settings\All Users\Start Menu\Programs\Startup"

%Desktop%\customizer\customizer.exe
GOTO :shutdown

:shutdown
SHUTDOWN -r -t 30 -f
EXIT

And the final batch file
Code: [Select]
SET Desktop="C:\documents and settings\all users\Desktop"
SET GSSTARTUP="C:\Documents and Settings\All Users\Start Menu\Programs\Startup"

GOTO :init

:init
IF EXIST %Desktop%\customizer\ GOTO :cleanup
IF NOT EXIST %Desktop%\customizer\ GOTO :end

:cleanup
ECHO Removing files.
ECHO Processing...
DEL /F /S /Q %Desktop%\customizer\*.*  
RD /S /Q %Desktop%\customizer\  
IF EXIST %Desktop%\customizer\ GOTO :cleanup
IF NOT EXIST %Desktop%\customizer\ GOTO :end

:end
DEL %GSSTARTUP%\cleanup.bat /Q


I'm fairly confident the problem is with how I'm using the start command in the first batch file to execute the second batch file...

DaveLembke



    Sage
  • Thanked: 662
  • Certifications: List
  • Computer: Specs
  • Experience: Expert
  • OS: Windows 10
Re: want to remove USB drive after executing .bat
« Reply #1 on: November 26, 2006, 10:51:54 PM »
You could get the Choice command off of an older OS and copy it to the root directory of the batch, or install a Resource Kit to gain it. Then declare to run the choice command for the delay time, so that your timing works better between the 3 batches you have and the EXE.

The command is really meant for just what it is, to make a choice, otherwise default to whatever you want, to use it as a timer.

Like this: choice /n/t:c,25/c:cc

From this reference: http://forums.windrivers.com/archive/index.php/t-15300.html

More info on choice ... and Set /p ( I've never used Set /p yet ) : http://www.computing.net/dos/wwwboard/forum/13909.html

larvalgeek

  • Guest
Re: want to remove USB drive after executing .bat
« Reply #2 on: November 26, 2006, 11:16:39 PM »
First, thanks for the reply, but I don't think that answer will solve the problem I'm having.

The problem is not a timing issue, its that the first batch file is located on the thumbdrive, and it copies a couple batch files to the local machine, and starts to execute the second one.  The idea is at this point, i should be able to remove my thumbdrive, because all the files that it needs have already been copied to the local machine, and the one batch file that is on the thumbdrive has already executed all of its commands, and passed control over to the second one.

When I remove the thumbdrive, however, the batch files ends when I close the .exe file, and does not execute the final command of "restart the computer".  If I leave the thumbdrive in, everything works flawlessly, so i believe the problem is with the final command in the first batch file utilizing START.  Either I'm using the wrong command, or not utilizing the correct arguments to truly pass control to the second batch file.

Maybe I'm not explaining things properly... let me briefly recap.

I've got a folder named "customizer" on my thumbdrive that contains a customizer.exe file.  In the root directory of my thumbdrive, I have a batch file that copies the entire contents of the customizer directory to the desktop of the local machine, then run a second batch file located inside the customizer folder.  This second batch file runs the customizer.exe file, waits for the user to close the program, and then restart the computer.  On restart, the computer executes a 3rd batch file located in the all users\start menu\startup folder that deletes the customizer folder on the desktop and then deletes itself from the startup folder.

Everything works flawlessly if I leave my thumbdrive in the computer, but currently it will fail if I remove the thumbdrive.  I only remove the drive after the .exe file has been started, because I know the files are done copying at that point.  I believe the problem is that the first batch file is not fully ending and then starting a new process for the second batch file.  This would explain why everything works flawlessly when the thumbdrive is in, but stops executing commands when it is removed, taking the first batchfile with it.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: want to remove USB dfrive after executing .bat
« Reply #3 on: November 27, 2006, 04:20:03 AM »
Quote
I believe the problem is that the first batch file is not fully ending and then starting a new process for the second batch file

Unless there is more code in the first batch file, it's safe to say the first batch file ends. As written, the started task (runcust.bat) is inheriting the environment from first.bat...presumably with the USB drive allocated. Try using the /i switch on the start command where the new environment will be the original environment passed to cmd.exe and not the current environment.

USB drives should not be removed before stopping them. Either use the "Safely Remove Hardware" icon from within Windows or you can use:

RunDll32.exe shell32.dll,Control_RunDLL hotplug.dll

from within the command processor or a batch file.

The above task is interactive, so you may think twice about adding it to your batch file.

Why the rush to remove the drive anyway? 8-)
« Last Edit: November 27, 2006, 04:35:36 AM by Sidewinder »
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

larvalgeek

  • Guest
Re: want to remove USB drive after executing .bat
« Reply #4 on: November 28, 2006, 10:36:59 PM »
I will give the /i switch a try.  I think I may have given that a shot, but then again, I could be wrong.  
I do know that the USB drive shouldn't be removed without safely removing it first, but 1, its a work thumbdrive, and everything on it is also saved on several other locations (excluding my custom batch files), and the entire thing is only about 5-600 mb, not a big deal to format and copy the data back over, and 2, I have never had a problem with just pulling it out, as long as I close any files that are open (which is part of why I am so concerned about this batch file).  

As to why I'm in a rush to remove the drive, I have only two thumb drives, and have to run this batch file and all the other files on 5-10 computers at a time, and each need to be done in about 20-30 min, tops.  Anything I can do to automate this process is good for me :)  Also, the less user intervention, the better... my fellow technicians (and i, sometimes, i'll admit) are forgetful, especially when we are really, really busy, and so we sometimes forget to remove some critical settings, such as the proxy server, or forget to delete this folder off the desktop, so I want to make it as foolproof as possible.

If /i doesn't work, I guess I give up.  Its not a huge deal, I was just hoping to help out my team a bit.