[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 389
  • Last Modified:

Batch file help adding to servers to txt file

I have a script that will run a couple commands, but I want it to read all of the servers in the text file to run the commands below...


 copy /y test.cmd "\\%\c$\test.cmd"
 copy /y test.pst "\\%\c$\outlook\pst"
 start psexec \\% c:\test.cmd
0
mystikal1000
Asked:
mystikal1000
  • 3
  • 3
  • 2
  • +1
3 Solutions
 
Christopher Raymond MendozaCommented:
I hope this helps:

http://ss64.com/nt/for_f.html
0
 
Steve KnightIT ConsultancyCommented:
To elaborate on that something like this should do you.  The bit in the for loop between the ( and ) is ran once for each line in the text file, taking the first"token" off each line - if you have more than one item on each line other than just the server name we can adjust to pickup the right bit.

Depending upon what you have there already you might want to do some checks of whether files are copied, whether directory exists and create it if not etc.

Steve

@echo off
set servers=c:\temp\servers.txt
set log=c:\temp\logfile.txt

for /f "tokens=1" %%s in ('type "%servers%') do (
 echo Working on %%s
 copy /y test.cmd "\\%%s\c$\test.cmd"
 copy /y test.pst "\\%%s\c$\outlook\pst"
 start psexec \\%%s c:\test.cmd
)
0
 
QlemoC++ DeveloperCommented:
FOR /F is the correct hint, but there is more to do:
@echo off
for /F "tokens=*" %%C in (c:\Temp\Servers.txt) do (
  copy /y test.cmd "\\%%C\c$\test.cmd"
  copy /y test.pst "\\%%C\c$\outlook\pst"
  psexec \\%%C -d c:\test.cmd
)

Open in new window

I have used the "detach" option of PsExec instead of spawning it with start, because it will not open new windows on your desktop that way.
0
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
mystikal1000Author Commented:
Dragon-IT - when I run the file I am just getting the output and its not going further...
set servers=c:\temp\servers.txt
set log=c:\temp\logfile.txt

Qlemo - when I try to run this i don't get anything from echo on, it just vanishes.

I would like to get this fixed as soon as you can help me.  I appreciate it!
0
 
Steve KnightIT ConsultancyCommented:
Ok so you have a file "C:\temp\servers.txt" or whatever which has all your computer names in, one per line, viewable in notepad, say, OK?

If you try this way does it show you the names?

@echo off
set servers=c:\temp\servers.txt

for /f "tokens=1" %%s in ('type "%servers%') do (
 echo Working on %%s
)
pause 

Open in new window


What did you call the batch file, not copy.cmd, for.cmd or anything else similar that would cause it to run itself again?
0
 
mystikal1000Author Commented:
i called it a different name.  When I ran your batch file I get this error...

C:\Temp>test.cmd
The syntax of the command is incorrect.
C:\Temp>
0
 
mystikal1000Author Commented:
Yes I am just testing one server in C:\temp\servers.txt folder, looks like its just not reading the file.
0
 
Steve KnightIT ConsultancyCommented:
Sorry I see I have missed a " in the copy/paste.  It should be like this.... or use Qlemo suggestion.  I used 'type "filename"' in cae you use a server filename with spaces in, as then you need quotes around it, i.e. "C:\my dir\filename.txt" and that causes the for command to work differently.

Steve


@echo off
set servers=c:\temp\servers.txt

for /f "tokens=1" %%s in ('type "%servers%"') do (
 echo Working on %%s
 copy /y test.cmd "\\%%s\c$\test.cmd"
 copy /y test.pst "\\%%s\c$\outlook\pst"
 start psexec \\%%s c:\test.cmd
) 

Open in new window

0
 
QlemoC++ DeveloperCommented:
BTW: For tests, never run a batch file by clicking it - you won't see errors. Start a cmd prompt, and execute the batch from there.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now