Batch file to copy folder to several PCs (SOLVED)
Moderator: DosItHelp
Batch file to copy folder to several PCs (SOLVED)
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
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.
Re: Batch file to copy folder to several PCs
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:
The %%a will hold the numbers from 100 to 195 and when running the command it will change the pc name with %%a
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
Re: Batch file to copy folder to several PCs
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
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
Re: Batch file to copy folder to several PCs
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
This time the %%a will hold the pc names
The list should be in this form:
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
Re: Batch file to copy folder to several PCs
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?
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?
Re: Batch file to copy folder to several PCs
Any path or file name with spaces needs "Quotes" around the whole path and file name.
Re: Batch file to copy folder to several PCs
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 (
For /F "tokens=*" %%a in ("\\mainserver\support\ICT Desktop Systems\Software\Trial\PC_list.txt") Do (
Re: Batch file to copy folder to several PCs
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
Re: Batch file to copy folder to several PCs
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
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
Re: Batch file to copy folder to several PCs
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
Re: Batch file to copy folder to several PCs
Squashman wrote:I am brain dead this morningCode: 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"
Re: Batch file to copy folder to several PCs
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.
Re: Batch file to copy folder to several PCs
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?
Re: Batch file to copy folder to several PCs
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?
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?
Re: Batch file to copy folder to several PCs
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