Batch file to copy folder to several PCs (SOLVED)

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
97browng
Posts: 10
Joined: 08 Aug 2012 04:31

Batch file to copy folder to several PCs (SOLVED)

#1 Post by 97browng » 08 Aug 2012 04:42

Hi All,

This is my first post so please be gentle. I am very new to batch files so please have some patience with me. I have done a little bit of VB but am trying to learn a bit of batch for my job.

Basically I want to create a batch file that copiers 3 files from a server to several PCs (about 95 in total) via the C$. I have managed the below and can copy the files to 1 machine;

COPY "\\mainserver\support\Test" "\\pc100\C$\Trial" /y
@ECHO OFF
ECHO.
Echo !!!If the batch file has worked the above should show 3 files copied.
ECHO Correct as of (08/08/2012)!!!
ECHO.
pause

What I would like to do is run this on all 95 PCs in one go. So I assume all I have to change is destination (lets assume the devices are PC100-PC195). I assume the way to do this would be to have a text file with the list of PCs and then change the PC asset to a variable that it pulls through. Is there an easy way to do this and how do I start?

Also I would like some sort way of confirming it has been successful on each device. Is there a way I can possible create another text file (or edit the one with the list of assets) and have the batch put the asset down and then a S for success or a F for fail next to it?

Thanks
Gary
Last edited by 97browng on 09 Aug 2012 05:05, edited 1 time in total.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#2 Post by abc0502 » 08 Aug 2012 05:14

You have the name is PC+number so the number is the one that is changing
You can use the for loop to count from 100 to 195 and the result of that loop will be used as the pc number

Try this:

Code: Select all

@echo off
cls
For /L %%a in (100,1,195) Do (
   COPY "\\mainserver\support\Test" "\\pc%%a\C$\Trial" /y
)
pause

The %%a will hold the numbers from 100 to 195 and when running the command it will change the pc name with %%a

97browng
Posts: 10
Joined: 08 Aug 2012 04:31

Re: Batch file to copy folder to several PCs

#3 Post by 97browng » 08 Aug 2012 05:34

Thanks for that I will give it a go. The assets are actually different and range from A20700 to a20795. Obviously I can change your code to match this, but as PCs fail and we need the replace them the assets will change.

So we have have A20700-A20751 (with A20752 and A20753 removed due to failure) then A20754-A20795 and then several new PCs which could have any asset (A20911, A30566, A51103). That's why I was thinking of using a text file so I can keep it up to date with the correct data.

We actually have a group on AD for all these machines so I was going to find a way of pulling the asset information from AD into a text file as in theory all the machines in this group will need the files changed.

Thanks
Gary

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#4 Post by abc0502 » 08 Aug 2012 05:43

OK, here it is, replace the PC_list.txt file with your pc list but but the list in the same folder with the batch, or write the full directory to the list file

Code: Select all

@echo Off
cls
For /F "tokens=*" %%a in (PC_list.txt) Do (
      COPY "\\mainserver\support\Test" "\\%%a\C$\Trial" /y
)
pause

This time the %%a will hold the pc names
The list should be in this form:
PC1
PC2
PC3

97browng
Posts: 10
Joined: 08 Aug 2012 04:31

Re: Batch file to copy folder to several PCs

#5 Post by 97browng » 08 Aug 2012 06:21

Thanks for that. I have sort of got it working however there seems to be a problem when I try and put the full path in.

If I run it like this it works fine;
For /F "tokens=*" %%a in (C:\PC_list.txt) Do (

However if I try this it does not work;
For /F "tokens=*" %%a in (\\mainserver\support\ICT Desktop Systems\Software\Trial) Do (

It comes up with the system cannot find the file \\mainserver\support\ICT.

I assume this is because there is a space in the file name, but when I write it in "" so it reads the below it finds the files and then comes up with 'The network path was not found. 0 Files copied'.

For /F "tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial") Do (

It will need to go on the server and I cannot change the server name so I am a bit stuck now.

Any ideas?

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file to copy folder to several PCs

#6 Post by Squashman » 08 Aug 2012 06:42

Any path or file name with spaces needs "Quotes" around the whole path and file name.

97browng
Posts: 10
Joined: 08 Aug 2012 04:31

Re: Batch file to copy folder to several PCs

#7 Post by 97browng » 08 Aug 2012 06:45

Thanks squashman but when I write it as the below it does not work;

For /F "tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt") Do (

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file to copy folder to several PCs

#8 Post by Squashman » 08 Aug 2012 06:47

Code: Select all

@echo Off
cls

pushd "\\mainserver\support\ICT Desktop Systems\Software\Trial"
For /F "tokens=*" %%a in (PC_list.txt) Do (
      COPY "\\mainserver\support\Test" "\\%%a\C$\Trial" /y
)
popd
pause

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#9 Post by abc0502 » 08 Aug 2012 06:51

yes that is right the space make that error, and putting the location between "" will treat any thing between as a string not a location

it is easy to fix that by reading the list file using type command

Code: Select all

@echo off
cls

For /F "tokens=*" %%a in ('type "\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt"') Do (
      COPY "\\mainserver\support\Test" "\\%%a\C$\Trial" /y
)
pause

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file to copy folder to several PCs

#10 Post by Squashman » 08 Aug 2012 06:57

I am brain dead this morning

Code: Select all

For /F "usebackq tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt") Do echo %%a

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#11 Post by abc0502 » 08 Aug 2012 07:01

Squashman wrote:I am brain dead this morning

Code: Select all

For /F "usebackq tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt") Do echo %%a


will, that is new for me :)
i never understood the "usebackq"

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file to copy folder to several PCs

#12 Post by Squashman » 08 Aug 2012 07:14

abc0502 wrote:will, that is new for me :)
i never understood the "usebackq"

As soon as you said string in your previous post the synapse fired in my brain.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#13 Post by abc0502 » 08 Aug 2012 07:17

I just read the use of the usebackq, so it only work to delete the quotes when using it in a situation like this one, to convert a string to a file set?

97browng
Posts: 10
Joined: 08 Aug 2012 04:31

Re: Batch file to copy folder to several PCs

#14 Post by 97browng » 08 Aug 2012 07:18

Thanks guys,

It is all working now. My final question is how can I find out which ones it worked on or more importantly which ones it did not work on?

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Batch file to copy folder to several PCs

#15 Post by abc0502 » 08 Aug 2012 07:26

I don't know if that will work or not but you can check if the distenation exist or not, if not it log the PC name to a file

Code: Select all

@echo off
cls
For /F "usebackq tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt") Do (
     IF Exist "\\%%a\C$\Trial" ( COPY "\\mainserver\support\Test" "\\%%a\C$\Trial" /y
     ) Else (
     Echo %%a>>"C:\Failed.log" )
)
pause

Post Reply