Batch File Loop

Posted on 2013-08-30
Medium Priority
Last Modified: 2013-08-30
Hello All

I want to create a batch file that repeats a command four times.  I have some freeware that ejects USB connected drives based on their drive letter, therefore I want to run this command:

removedrive.exe e: -E -W:1000  (where e: is the drive letter)

then I want to run:

removedrive.exe f: -E -W:1000  (where f: is the drive letter)

etc, up to h: and then exit.

I assume some sort of loop would be required?  Any help much appreciated.

Question by:vision_on
  • 2
  • 2
LVL 86

Expert Comment

ID: 39451933
That fits into one command line: for %a in (E: F: G: H:) do (removedrive.exe %a -E -W:1000)
In a batch file, use double percent signs: for %%a in (E: F: G: H:) do (removedrive.exe %%a -E -W:1000)

Author Comment

ID: 39451963
Thanks oBdA

You will have to spell it out for me I am not very good at this stuff, so in a batch file, the syntax should be:

For %%a In (E: F: G: H:) Do (removedrive.exe %%a -E -W:1000)

Do I need the brackets round the drive letters and command?

LVL 86

Accepted Solution

oBdA earned 2000 total points
ID: 39451974
You will need the brackets around the drive letters.
Syntactically, the brackets around the command aren't required in this case.
If you want to spread out the command over several lines, though, they're required, for example like this:
@echo off
For %%a In (E: F: G: H:) Do (
	echo Removing drive %%a
	removedrive.exe %%a -E -W:1000

Open in new window

If you want to look into scripting (batch and other languages), check out http://www.robvanderwoude.com

Author Comment

ID: 39451986
Excellent thanks very much for your help.


Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The video will let you know the exact process to import OST/PST files to the cloud based Office 365 mailboxes. Using Kernel Import PST to Office 365 tool, one can quickly import numerous OST/PST files to Office 365. Besides this, the tool also comes…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question