Windows 7 Forums
Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.

Windows 7: Need help understanding batch commands/parameters

01 Mar 2015   #1

Windows 7 64-bit
Need help understanding batch commands/parameters


In this batch program (deletes temp files):

@echo off
cd %temp%
for /d %%D in (*) do rd /s /q "%%D"
del /f /q *

What does the "%%D" component signify and how is it used? Or rather, how does the "for" command work for that matter? I've looked at other explanations online and I can't seem to find a clear distinction between %%D and %D, for example.

I appreciate any input. Thanks.

My System SpecsSystem Spec
01 Mar 2015   #2

Windows 10, Windows 8.1 Pro, Windows 7 Professional, OS X El Capitan

In a batch for loop, this "%%D" signifies a dynamic variable that will expand to each item of a list of data that For produces. In this example, %%D expands to each directory found within the %temp% folder.

The for /d in (*) part of the command says to grab a list of all directories in the current working directory (which is %temp% in this case), and the command part of the loop: rd /s /q "%%D" is executed a finite number of times, where %%D expands to each item of data with each iteration of the loop.

The letter proceeding the percents is completely arbitrary. It does not have to be capital, or even a letter at all. The following are examples of valid dynamic variable names; %%A, %%h, %%6, %%0, %%#, %%_, %%$. Though, it's always best practice to use a capital letter; it's rare you'll come across anything else.

The writer of this batch script has appropriately chosen the letter D for its dynamic variable, as each iteration of the loop should expand %%D to a name of a directory.

"%D" does not exist within batch files. A single percent is used when you want to run the For command within a Command Prompt. So if you want to run for /d %%D in (*) do rd /s /q "%%D" in a Command Prompt, it would become for /d %D in (*) do rd /s /q "%D". It's just something you have to remember.

You may enter help for into a Command line to read more about the For loop. Or you can learn how to effectively use batch For Loops here.
My System SpecsSystem Spec

 Need help understanding batch commands/parameters

Thread Tools

Similar help and support threads
Thread Forum
Batch Files - Create a Menu to Execute Commands
How to Use a Batch File to Create a Command Prompt Menu to Execute Commands This tutorial shows the process one goes through when creating a batch file that opens a command prompt menu allowing you to launch commands, programs and more. I will only be executing programs in this tutorial,...
How do I create a batch file to run commands on auto?
the thread that i made previously only got 1 answer that really didnt have anything to do with my problem so thought it would be ok if another one was made, and not trying to spam, here are the commands for the batch file netstop sppvc cd...
General Discussion
How to run a batch file at logoff & some batch scripting help please
Hiya, I'd just like to run a simple batch file at logoff to backup some settings. I plan to use Robocopy, which is what i use to more thorough backups. Two questions: 1. How make it run by clicking Start > Shutdown? (i realise i could make a shutdown batch file and run it instead, but i'd...
Backup and Restore
Invalid Parameters
Hmmm, ran CMD.exe as administrator, copied and pasted "powercfg -lastwake" after C:\Users\bert> and got an immediate "Invalid Parameters -- try "/?" for help. Didn't even get a chance to hit the enter key after lastwake. Tried it a few more times. Nothing. Typing /? gets me '/?' is not...
Performance & Maintenance
Incorrect parameters, please try again later
I just bought a TOSHIBA L-500 (it was my birthday :P ) And i install the updates, but when i want to go to Youtube i get this error '' Incorrect parameters, please try again later'' whats the problem My bit system is x64 (sorry if in wrong section :'( )
Browsers & Mail

Our Sites

Site Links

About Us

Find Us

Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd

All times are GMT -5. The time now is 12:37.
Twitter Facebook Google+