शुक्रवार, 14 मई 2010

FOR loops

Batch files - FOR loops


Basic syntax:

 Batch files - FOR loops
FOR %A IN (list) DO command [ parameters ]
list is a list of any elements, separated by either spaces, comma's or semicolons.
command can be any internal or external command, batch file or even - in OS/2 and NT - a list of commands
parameters contains the command line parameters for command.
In this example, command will be executed once for every element in list, using parameters if specified.
A special type of parameter (or even command) is %A, which will be substituded by each element from list consecutively.

Full syntax

MS-DOS and PC-DOS
(incl. Win95's MS-DOS 7.*)  
as specified under basic syntax, plus VFAT/FAT32 long file name handling with LFNFOR in MS-DOS 7.*
OS/2 Warp as specified under basic syntax, though unlike in DOS, you may use redirection and conditional execution in the command string specified after DO
Windows NT 4/2000/XP as specified under basic syntax, plus many new options

Notes

1. %A vs. %%A
%A is for use on command lines only.
In all examples and syntax lines shown %A should be substituted with %%A when used in batch files.
2. %a vs. %A
The A in %A may be replaced by any character, either upper case or lower case, except numbers. Note, however, that variables ar case sensitive, so be consistent:
FOR %A IN (1 2 3) DO ECHO %a
will not work.
FOR %? IN (1 2 3) DO ECHO %?
on the other hand, will.
3. Nesting FOR commands
In DOS (COMMAND.COM), nesting FOR commands is not possible.
However, by using a second command processor you may still be able to nest them:
FOR %A IN (1 2 3) DO COMMAND /C FOR %B IN (A B C) DO ECHO %A%B
Note that you cannot replace COMMAND /C with CALL

In NT (CMD.EXE) nesting is possible.
Note that to nest FOR loops, each loop requires its own variable; i.e.
FOR %A IN (1 2) DO FOR %A IN (A B) DO ECHO %A
will lead to undesired results:
A
B
A
B


FOR %A IN (1 2) DO FOR %B IN (A B) DO ECHO %A%B
will work as planned, and display:
1A
1B
2A
2B
4. Commands in list
list may not only contain a list of parameters, it may even contain a list of commands that can be executed consecitively with the same parameters. a.k.a. command may also be %A:
DIR > tempfile.txt
FOR %A IN (TYPE DEL) DO %A tempfile.txt

This FOR loop will first type the temporary file and then delete it.
5. list delimiters
The "elements" in list can be delimited (separated) by spaces, tabs, commas or semicolons.
As of MS-DOS 7, doublequoted strings are treated as a single element, wether they contain delimiters or not.

In NT, it is possible to define your own delimiters with FOR /F.
6. Forward slashes in list
If list starts with a forward slash, COMMAND.COM (except MS-DOS 7) will split the string in list into its first character following the forward slash and the remainder of the string.
This is demonstrated in the interactive FOR examples
7. Conditional manipulation of variables in command
The following does not work:
FOR %%A IN (1 2 3) DO IF "%VAR%"=="" SET VAR=%%A
sets VAR to 3, not 1
The %VAR% in the comparison (IF "%VAR%"=="") is interpreted immediately, and thus empty, and then the FOR loop is started.
From the FOR loop's point of view, the command issued was:
FOR %%A IN (1 2 3) DO IF ""=="" SET VAR=%%A
The following construction can be used as a workaround:
FOR %%A IN (1 2 3) DO CALL TEST2.BAT %%A
If TEST2.BAT looks like this:
IF "%VAR%"=="" SET VAR=%1
then VAR is set to 1
In NT a CALL to a subroutine could be used instead.
8. Jump using GOTO in command
FOR %%A IN (1 2 3) DO GOTO=%%A
:1
ECHO 1
GOTO End
:2
ECHO 2
GOTO End
:3
ECHO 3
:End

will display different results for different command interpreters:
  • CMD.EXE (NT and OS/2) will jump to label 1
  • COMMAND.COM (DOS) will jump to label 1, then 2, then 3, without executing any of the following commands, and then execute the ECHO 3 and following commands

Examples

Interactive examples Shows the different ways in which different OSs handle "lists".
WHICH UNIX-like WHICH utility for Windows NT (2 batch files), OS/2 (Rexx, also searches for DLL's) and a severely limited version for DOS (batch file)
Errorlevel Show error level of previous command using FOR and redirection
RestHome Restore all homedir shares on an NT Server
SCList lists the properties of all shortcuts in an NT machine's Start Menu
DiskUse Version 5 lists the disk usage for every subdirectory (Windows 2000)
DiskUse Version 4.01 the same for NT 4

How To setup a DHCP for PXE protocol?

PXE Setup without RIS on Windows 2000 ServerHow To setup a DHCP for PXE protocol
It is possible to setup a PXE server on Widows 2000 server without installing RIS. Do all of this on whatever machine is serving DHCP.

  FAQ:

Does PXE need DHCP?
PXE does not come with a dedicated boot protocol. ... The DHCP server can send out the DHCP offer containing the clients IP address and the PXE server can send out the DHCP offer containing the option 60. The PXE client will wait for all needed information up to 60 seconds.
  

How do I enable DHCP service?
Right-click on Local Area Connection and select Properties. Highlight the Internet Protocol (TCP/IP) option and click the Properties button. If you want to enable DHCP, make sure Obtain an IP address automatically is selected, as well as Obtain DNS server address automatically.


How do you force a PXE boot?
How to Boot Into PXE
Power on or restart your computer. Press the hotkey to enter the BIOS. ...
Navigate to the network card settings with the keyboard. ...
Toggle the "LAN BOOT ROM" option to "Enabled." ...
Navigate to the boot settings with they keyboard. ...
Press "F10" to save the settings and exit the BIOS.

How do I boot from network?
Make sure networking booting is enabled. This setting can be named "Boot from the network" or "Boot from PXE". Then, start the computer and press the required key to bring up the Boot Menu. On most computers, you need to press F12 or F8, depending on the BIOS version.Mar 14, 2013


What is PXE and how it works?
PXE - Pre-Boot Execution Environment
It allows a workstation to boot from a server on a network prior to booting the operating system on the local hard drive. A PXE-enabled workstation connects its NIC to the LAN via a jumper, which keeps the workstation connected to the network even when the power is off.


What ports does PXE use?
Listening ports used during client network boot (PXE/TFTP protocols):
DHCP : port 67 UDP.
PXE BINL : port 4011 UDP.
TFTP : port 69 UDP.

What is the DHCP server?
A DHCP Server is a network server that automatically provides and assigns IP addresses, default gateways and other network parameters to client devices. It relies on the standard protocol known as Dynamic Host Configuration Protocol or DHCP to respond to broadcast queries by clients.


How does PXE boot work with DHCP?
When the client initiates a PXE boot (by traditionally pressing F12) however the process is changed slightly: The client sends out a DHCP broadcast and states that it needs to PXE boot. The DHCP server picks up this broadcast and replies with a suggested IP address to use



How do I change my DHCP settings?
To enable DHCP or change other TCP/IP settings
Do one of the following: ...
Right-click the connection that you want to change, and then select Properties. ...
Select the Networking tab. ...
To specify IPv4 IP address settings, do one of the following: ...
To specify IPv6 IP address settings, do one of the following:


What is default gateway IP?
A default gateway is the node in a computer network using the internet protocol suite that serves as the forwarding host (router) to other networks when no other route specification matches the destination IP address of a packet.

How do I find my DHCP?
Displaying a DHCP Lease
Open the Command Prompt: Windows 10: Open the Start Menu, type cmd and press Enter. (+) ...
In the Command Prompt, type ipconfig /all and press Enter. Your IP Address is listed to the right of IPv4 Address while your MAC Address is listed to the right of Physical Address. (+)


  1. Open the DHCP control panel from administrative tools.
  2. Expand the server you are wanting to configure. Expand the "Scope" folder. Right click on "Scope Options" and select "Configure Options".
  3. Put a check mark in "013 Boot File Size" and enter the file size in 512 octet blocks. Example, your boot image file is 12 KB (kilobytes). Convert 12 KB to bytes (12288). This must be exact, don't just multiply 12 by 1000. Divide the file size in bytes by 512 (12288 / 512). Enter the resulting number (24) in the text dialog that is enabled when you put a check in 013.
  4. Put a check mark in "043 Vendor Specific Info" and enter the following binary value: 01 04 00 00 00 00 ff.
  5. Open a command window and type the following:
    c:\>netsh
    netsh>dhcp
    netsh dhcp>server \\server_name
    netsh dhcp server>add optiondef 60 ClassID String 0
    netsh dhcp server>set optionvalue 60 STRING PXEClient
    
  6. Put a check mark in "066 Boot Server Host Name" and enter the IP address of the server.
  7. Put a check mark in "067 Bootfile Name" and enter the name of the boot image file.
  8. (Thanks to Me for this step.)
    If you use the Linux-based boot disk, you can set the boot disk's defaults (user name, password, share) via DHCP option 233. Open a command prompt and type:

    netsh dhcp server add optiondef 233 Unattended String 0 comment="Unattended dhcp info"
    
    Go back to "Scope Options", select configure options, put a check mark next to the new entry, and fill in the boot disk options you want to use (e.g., z_user=username z_pass=sekrit z_path=\\myserver\myshare).
  9. Right click on the server name in the DHCP control panel, go to all tasks, and select "restart".
  10. Go back to scope options and verify that all the information is there. You should see the five entries on the right. They should look like this:
    013  Boot File Size             Standard        0x18
    043  Vendor Specific Info       Standard        01 04 00 00 00 00 ff
    060  ClassID                    Standard        PXEClient
    066  Boot Server Host Name      Standard        Server IP address
    067  Bootfile Name              Standard        bzImage (boot file name)
    
    If any of the options do not match these, double-click on the option and change the value. More than likely option 060 will be wrong. Change it so it looks like above.
You are now ready to set up tftp.

TFTP Configuration

  1. Copy "tftpd.exe" from "c:\winnt\system32\dllcache\tftpd.exe" to "c:\winnt\system32\tftpd.exe"
  2. Get a copy of "sc.exe" from the system32 folder on any windows xp box and place it in "c:\winnt\system32"
  3. Open a command window and type "sc create tftpd binpath= c:\winnt\system32\tftpd.exe "
  4. The service "tftpd" is now available in the service control panel. Open the control panel, find the service, and start it. (If you want it to start automatically, you can set it to do so from the control panel.)
  5. Open the registry to the following key. \\hkey_local_machine\system\currentControlSet\services\tftpd. Add a subkey "parameters" to the tftpd folder. Add a string value "Directory" to the "parameters" key. Give "Directory" the path location where your boot image file is located. (example c:\tftpd).
Much of this is adapted from Willis Doiron's nice document.
Thanks to Tyler Hepworth for the original version of this document.

fly