Solved

Batch file help adding to servers to txt file

Posted on 2013-06-13
9
359 Views
Last Modified: 2013-06-14
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
Comment
Question by:mystikal1000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 4

Assisted Solution

by:Christopher Raymond Mendoza
Christopher Raymond Mendoza earned 50 total points
ID: 39246683
I hope this helps:

http://ss64.com/nt/for_f.html
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39246919
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
 
LVL 70

Expert Comment

by:Qlemo
ID: 39246922
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
LVL 1

Author Comment

by:mystikal1000
ID: 39247436
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
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39247459
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
 
LVL 1

Author Comment

by:mystikal1000
ID: 39247482
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
 
LVL 1

Author Comment

by:mystikal1000
ID: 39247493
Yes I am just testing one server in C:\temp\servers.txt folder, looks like its just not reading the file.
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 425 total points
ID: 39247506
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
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 25 total points
ID: 39247613
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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question