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
Solved

Batch file help adding to servers to txt file

Posted on 2013-06-13
9
357 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
  • 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 69

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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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 69

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
This article will show, step by step, how to integrate R code into a R Sweave document
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

860 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