Get the App
SLTechnology News&Howtos  ›  Servers  › 

For cycle of Windows Bat

Shulou Source: shulou.com Published: 2022-06-02 06:25:21 10月24日 Update

For cycle of Windows Bat 1. Basic usage of For cycle. 1.1 format

In the cmd window:

FOR% variable IN (set) DO command [command-parameters]

In the Bat file:

FOR% variable IN (set) DO command [command-parameters]

Note: in the cmd window, the form variable I after for must be referenced by a single percent sign, that is,% I, while in a batch file, the reference form variable I must use a double percent sign, that is,% I.

What are the basic elements of a For statement:

1. For, in and do are the keywords of the for statement, and none of them are indispensable.

2.% I is a reference to a formal variable in a for statement, which must appear even if it does not participate in the execution of the statement after do.

3. After in, the parentheses before do cannot be omitted

4. Set represents a string or variable, and command represents a string, variable or command statement

1.2 View Windows help documentation

Use for /? You can view

1.3 practical examples

1.3.1 list all txt files under the current path.

For I in (? .txt) do echo'I'

1.3.2 text parsing shows for / f usage.

1) assign the value of the command line to the variable.

For / f% I in ('wmic ComputerSystem get Manufacturer ^ | find / v "Manu" ^ | findstr.') Do (set type=%i)

2) use "delims=" to specify the delimiter, and / f defaults to a space or Table key as the delimiter.

For / f "delims=,"% I in ('wmic ComputerSystem get Manufacturer ^ | find / v "Manu" ^ | findstr.') Do (set type=%i)

Ps: the structure of the IF statement

For / f% I in ('wmic ComputerSystem get Manufacturer ^ | find / v "Manu" ^ | findstr.') Do (set type=%%i) if% type%== "HP" (reg add "HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Terminal Server" / v fDenyTSConnections / t REG_DWORD / d 0 / f): open the remote desktop service reg add "HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Terminal Server\ Wds\ rdpwd\ Tds\ tcp" / v PortNumber / t REG_DWORD / d 0x0000b239 / freg add "HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Terminal Server\ WinStations\ RDP-Tcp "/ v PortNumber / t REG_DWORD / d 0x0000b239 / f) else if% type%==" Dell "(reg add" HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Terminal Server "/ v fDenyTSConnections / t REG_DWORD / d 0 / f: open the remote desktop service reg add" HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Terminal Server\ Wds\ rdpwd\ Tds\ tcp "/ v PortNumber / t REG_DWORD / d 0x0000b275 / freg add" HKEY_LOCAL_MACHINE \ SYSTEM\ CurrentControlSet\ Control\ Terminal Server\ WinStations\ RDP-Tcp "/ v PortNumber / t REG_DWORD / d 0x0000b275 / f) else (echo your server type does not match the specified port number)

Tags: Statements variables forms files services loops delimiters commands characters strings desktop utility indispensable three keywords semicolons slogans parentheses text Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Huawei Shulou Technology MySQL OPPO Reno