'Iterate trough list of directories using batch script
I want to simplify my code, using a FOR to use directory addresses in windows and use the same operation:
@echo off
set path=C:\Program Files\Autodesk
cd /d "%path%"
SET RULENAME=AUTODESK
FOR /r %%G in ("*.exe") Do (@echo %%G
c:\windows\system32\netsh advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G" action="block" enable="yes")
set path=C:\Program Files (x86)\Autodesk
cd /d "%path%"
SET RULENAME=AUTODESK
FOR /r %%G in ("*.exe") Do (@echo %%G
c:\windows\system32\netsh advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G" action="block" enable="yes")
set path=C:\Program Files\Common Files\Autodesk Shared
cd /d "%path%"
....
I've tried separating the addresses with quotes, without quotes, with and without commas but it didn't work:
@echo off
for /F "tokens=*" %%A in ("C:\Program Files\Autodesk", "C:\Program Files (x86)\Autodesk") do (
set path=C:\Program Files\Autodesk
cd /d "%path%"
SET RULENAME=AUTODESK
FOR /r %%G in ("*.exe") Do (@echo %%G
c:\windows\system32\netsh advfirewall firewall add rule name="%RULENAME%-%%~nxG" dir=in program="%%G" action="block" enable="yes")
Solution 1:[1]
Your code have several small errors:
You confuse the purpose of
forandfor /Fcommands. Plain FOR command serve to process several individual items, but FOR /F is used to split lines in parts. In your example you want to use the first method.Both path and RULENAME variables are assigned into a FOR loop, so they must be expanded via Delayed Expansion !varname!. A simple method to fix this is just using their values: "path" must have the value of the FOR replaceable parameter, and RULENAME is a constant.
You forgot a closing right paren. This can be easily detected if you use proper alignment, as in the working code below:
.
@echo off
for %%A in ("C:\Program Files\Autodesk" "C:\Program Files (x86)\Autodesk") do (
cd /d %%A
FOR /r %%G in (*.exe) Do (
@echo %%G
c:\windows\system32\netsh advfirewall firewall add rule name="AUTODESK-%%~nxG" dir=in program="%%G" action="block" enable="yes"
)
)
PS - You should never modify a variable called path. This is used by cmd.exe to search for executable files.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
