简体   繁体   中英

Batch Script - Loop through text file and set each line as variable and run script then repeat for next line

I have this script I've attempted to create -

It's not looping as I need it to. Seems to only be grabbing the last line of the file?

I have a text file with several computer names, one per line... I need to set line 1 as variable, echo that line and then run the one line code and then set line 2 as variable, echo that line and then run the code and on through the rest and display "Complete" when it's finished with all of the lines...

Any assistance is appreciated. I have searched Stack Overflow and found similar questions with answers but none are working as I intent them to work...

@Echo off
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "tokens=*" %%f in (computernames.txt) do (
  set compname=%%f
    echo !compname!
)
echo %compname%
REG ADD "\\%compname%\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v "fDenyTSConnections" /t REG_DWORD /d 0 /f

pause

Your script does grab every line in the file, but you're only echoing the result when the loop finishes (so only the last line).

First, echo %%f inside the loop to see all the computer names. Then, you have to move your registry code inside the loop as well. You can just use %%f .

@Echo off
for /f "tokens=*" %%f in (computernames.txt) do (
  echo %%f
REG ADD "\\%%f\HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v "fDenyTSConnections" /t REG_DWORD /d 0 /f
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM