user2701229
user2701229

Reputation: 3

Create a list from a batch file

I Build SCCM Servers for work. We have a set of source files that need copied to multiple servers for each build. I am trying to create a script that will prompt the user for the number of role servers involved in the build, then run a loop and ask for the names of "X" number of servers, then enters the names of those servers in a text file like this:

                   Server1
                   Server2
                   Server3    etc..

then I plan to use that list of server names to do an xcopy to those servers. I want to get all of the input done on the front end so that the user can start it and leave it because it can take several hours to copy these files.

Here is my code so far, and it just closes after asking for the first server name. (also I left the first entry out of the do loop so that I can use it to clear the file of any old input)

 @echo off
Set /p ServerCount = "How Many Role Servers? : " %=%
Set /p ServerName = "Enter Server Name : " %=%
 Echo %ServerName% >Test.txt    
Do i = 2 to %servercount% by 1
 Set /p ServerName = "Enter Server Name : " %=%
 Echo %ServerName% >> Test.txt
 Echo %i
enddo

Upvotes: 0

Views: 103

Answers (1)

SachaDee
SachaDee

Reputation: 9545

The Do loop don't exist in BAT you have to use a for loop :

@echo off
Set /p ServerCount="How Many Role Servers? : 

setlocal EnableDelayedExpansion
for /l %%a in (1,1,%serverCount%) do (
Set /p ServerName=Enter Server Name [%%a]: 
Echo !ServerName!>>Test.txt
)

Upvotes: 1

Related Questions