?
Solved

Batch file help adding to servers to txt file

Posted on 2013-06-13
9
Medium Priority
?
372 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 200 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 1700 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 100 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Resolving an irritating Remote Desktop connection that stops your saved credentials from being used.
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

741 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