Solved

Xcopy a folder to a server - Script file batch file.

Posted on 2004-09-28
47
1,639 Views
Last Modified: 2008-01-16
Hello there...

I am planned to backup data 'my documents' on every pc I have on my network to my pc server.

My planned is to create a script file to copy this folder c:\my documents to the userser folder on ther server.
example:

Lucie PC:
c:\My documents
  - Wordfiles
  - Excellefiles

Server
Users/
  Lucie

Before lucie leave the office she need to click on the script located on her desktop then all the folder will be copied to the server.  Anyone know how to write this script as xcopy in a BATCH file. please advice.
thank you
elie.
0
Comment
Question by:elieazzi
  • 19
  • 18
  • 3
  • +4
47 Comments
 
LVL 57

Expert Comment

by:Pete Long
Comment Utility
Do you have a domain? if so simply redirect "my documents" to the server through policy?
0
 

Author Comment

by:elieazzi
Comment Utility
How I'm able to do this... usually some users are taking the laptop at home in the evening...

so my suggestions is before they leave the office if they can launch this script where it will copy all the document an incremental to the server... do you see what i mean?

Tks.
Elie.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Hello Elie,
perhaps try something like this:

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\My Documents" /Y \\server\Users\Lucie
set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

Assuming that you want to copy the files to "\\server\Users\Lucie".
Either way, if the users name on the server, ie;  \\server\Users\ LUCIE   are the same as the users computer account, and you're on atleast Win2000, then you can use something like this:  
xcopy "c:\My Documents" /Y \\server\Users\%UserName%

If this doesn't work, and the "\\server\Users\Lucie" is correct, then try mapping the server path to a drive,, using 'net use' (type  net use /?  for more info).

Hope that helps  :)

Best of luck,
[r.D]
0
 
LVL 57

Expert Comment

by:Pete Long
Comment Utility
>>How I'm able to do this... usually some users are taking the laptop at home in the evening...


If you have a domain you can set all your domain users to have their my documents where you want it to be (i.e. on your server) as for laptops, if they are windows 2000 or windows XP just set them to syncrinise the my documents folder, then they can take the laptop home and the next time they connect to your network the latest version gets syncronised to the server - theres no need to be running scripts that your users will "ahem" forget to run :)
0
 

Author Comment

by:elieazzi
Comment Utility
Thank you but it's copy only the files... how about i want copy the subfolders of the test directory.  

Also if there is a different directory, example: c:\my documents and c:\test.
how i'm able to put the two folder.

tks.
Elie.
0
 

Author Comment

by:elieazzi
Comment Utility
Pete,

For your idea, i'm using as server windows 2003 small business server.  My users are using some of the XP and others windows 2000.

Would you please provide me step by step how i'm able to do it.

tks.
elie.
0
 

Author Comment

by:elieazzi
Comment Utility
Hello,

In this following script, there is a way i can put an if statement:

If the file located on the C:\test is the same as the server, don't copied.
If the file located on the C:\test was modified / new please copy it to the server...

Please advice. thank you.

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I \\Cafserver\users\elie\
set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Well, due to the /Y parameter, then if the file already exists, it will be overwritten without prompting you.
But if the file is rather large, and will take a while to upload, and thus you really don't want it to re-upload the file, say, and I'll try and add an IF statement. :)

gL,
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
Could you please write the script for me. tks.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
:o\ Try this....


for %%i in (c:\test\*.*) do call :PROCESS
exit /b

:PROCESS
if exist "\\Cafserver\users\elie\%1" (
  :: Do Nothing ::
) ELSE (
  :: Copy file ::
  copy %1 "\\Cafserver\users\elie\"
)


I hope it helps :)
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Woops, replace:

for %%i in (c:\test\*.*) do call :PROCESS

with:

for %%i in (c:\test\*.*) do call :PROCESS %%i
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Actually, one more correction:

cd\test
for %%i in (*.*) do call :PROCESS %%i

:)
0
 

Author Comment

by:elieazzi
Comment Utility
Dr Warezz,

Please advice if this is okay as order in the file.

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System

cd\test
for %%i in (*.*) do call :PROCESS %%i


exit /b

:PROCESS
if exist "\\Cafserver\users\elie\%1" (
  :: Do Nothing ::
) ELSE (
  :: Copy file ::
  copy %1 "\\Cafserver\users\elie\"
)



:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I /M \\Cafserver\users\elie\

set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

tks.
0
 
LVL 9

Accepted Solution

by:
DrWarezz earned 500 total points
Comment Utility
If you want to use BOTH those pieces of code (although doing near enough the same thing), then you need to make one or two little changes, like so:

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System

cd\test
for %%i in (*.*) do call :PROCESS %%i
goto :loop0

:PROCESS
if exist "\\Cafserver\users\elie\%1" (
  :: Do Nothing ::
) ELSE (
  :: Copy file ::
  copy %1 "\\Cafserver\users\elie\"
)
exit /b

:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I /M \\Cafserver\users\elie\

set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

But, like I say, they do very similar tasks, so are you sure that you want to do BOTH:

cd\test
for %%i in (*.*) do call :PROCESS %%i


exit /b

:PROCESS
if exist "\\Cafserver\users\elie\%1" (
  :: Do Nothing ::
) ELSE (
  :: Copy file ::
  copy %1 "\\Cafserver\users\elie\"
)

AND:

:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I /M \\Cafserver\users\elie\

set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b

?

If so, my full code above should do it :)
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
Tks for your feedback, i'd like to do only one the if statement...

if this okay for the if statement.


::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System

cd\test
for %%i in (*.*) do call :PROCESS %%i
goto :loop0

:PROCESS
if exist "\\Cafserver\users\elie\%1" (
  :: Do Nothing ::
) ELSE (
  :: Copy file ::
  copy %1 "\\Cafserver\users\elie\"
)
exit /b

@:loop0
@cls
@echo.
@echo Backing up Files ...
@xcopy "c:\test" /Y /s /I /M \\Cafserver\users\elie\

set err=%errorlevel%
cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
My proposition is to use /D switch with XCOPY.
It will copy a file only if it is newer than existed.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Ah, yes. /D switch :)

Elie, try this perhaps:

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I /M /D \\Cafserver\users\elie\
set err=%errorlevel%

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

Hope it helps  - and thanks alot For-Soft :)
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
I've deleted the Files on the server... Then I've run this script, no files has been copied.

So this script is a second script??

Try it...
1. Delete existant files on the server.
2. test this script
3. Results it will not copy any file mmmmmmmmmm

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
xcopy "c:\test" /Y /s /I /M /D \\Cafserver\users\elie\
set err=%errorlevel%

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
What's exactly the outcome?
If you don't get much of a specific error message, then try this code to try and narrow it down:

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\test" /Y /s /I /M /D \\Cafserver\users\elie\ >>"c:\ErrFile.txt"
set err=%errorlevel%
echo Errorlevel: %err% >>"c:\ErrFile.txt"

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

Then copy and paste the content of  c:\ErrFile.txt  to here, and we'll try and decide what's going wrong here :o\
gL,
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
1. All the documents are on the c:\test.

2. There is no documents on the u:\users\elie (EMPTY)

3. Run the script

4. this is the result in the file:
 
xcopy files
0 File(s) copied
Errorlevel: 0


I think the script should verify file, if file not there then copy file.

Thank you please advice, you're the expert... God bless you!
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
I just played a little with XCOPY command.

xcopy "c:\temp\bat" /Y /s /I /M /D \\server\c\temp\bat\

First run made a perfect replication of c:\temp\bat folder.

Every next run copied only changed files.

If no new files = 0 File(s) copied.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
So, Elie needs to remove some of the switches, right, For-Soft?
[r.D]
0
 
LVL 10

Expert Comment

by:For-Soft
Comment Utility
I suspect that:
\\Cafserver\users\elie\
and
u:\users\elie
are not the same folder.

My proposition is to run:
DIR \\Cafserver\users\elie

Are there any files in the \\Cafserver\users\elie folder?
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:elieazzi
Comment Utility
it's working ! but read my text please and advice on my last question !!

Please follow me.

1. i've create this folder c:\temp\bat.
   i've created 3 .txt files   Okay
2. I've open a new notapad save it as elie.bat
The content of elie.bat is


::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
set err=%errorlevel%
echo Errorlevel: %err% >>"c:\ErrFile.txt"

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::


3. I've close the file.
4. I've run the script from windows explorer.

5. I've open the ErrFile.txt

   xcopy files
C:\temp\bat\asdfument.txt
C:\temp\bat\dgd.txt
C:\temp\bat\vcvx.txt
3 File(s) copied
Errorlevel: 0

6. I went to the server.
  The folder temp\bat was created in the elie directory plus 3 files has been copied.

7 NOW I've did a modification on the c:\temp\bat .  I've add text to an existant file.
  and I've add a new txt document.   I want see if it will copy onle the new text document and the file has been modified.
 
I've check the folder on the server, it's copied the filed !!! VERY GOOD.
and the content of the errlevel

xcopy files
C:\temp\bat\ffg.txt
C:\temp\bat\vcvx.txt
2 File(s) copied
Errorlevel: 0

8. Now if I have 2 folder one is c:\temp\bat and the other c:\docs\ to be copied to the server how i'm able to add c:
docs to the script .  Would you Please advice.  Thank you.

echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
set err=%errorlevel%


Tks
Elie.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
If I've interpreted what you've said correctly, try this:

echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
xcopy "c:\docs" /Y /s /I /M /D \\CAFserver\users\elie\docs >>"c:\ErrFile.txt"
set err=%errorlevel%

Is that what you want?
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
I want all the xxx files to be stored in X folders.

I mean the destination should be same folder.  is this correct.

xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
xcopy "c:\docs" /Y /s /I /M /D \\\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Yeah, that looks right.
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
It didn't copy the second folder c:\test at all.

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
xcopy "c:\test" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
set err=%errorlevel%
echo Errorlevel: %err% >>"c:\ErrFile.txt"

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::


Any idea:, please advice... tks.

This is error msg in the file:
xcopy files
0 File(s) copied
0 File(s) copied
Errorlevel: 0
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
What files have you put in the "c:\test" dir? If they're the same as the "c:\temp\bat" dir, then due to the /D switch, it won't copy anything.

[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
They are a .txt files but differnent names... They're not the same as c:\temp\bat.

Please test on your site to see.

tks.
el.e
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Ermm... give me a little longer..
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
okay tks.
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
I just tested it on my c:, and all I changed was the "\\CAFserver\users\elie\temp\bat" to a c: path.
And it all worked FINE! :S

Just try this:
Create a directory on the server, called this:  \\CAFserver\users\elie\test

Then try this:

::----------------------- FileName.bat --------------------------::
@echo off
TITLE My Documents Backup System
:loop0
cls
echo.
echo Backing up Files ...
echo xcopy files > "c:\ErrFile.txt"
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
xcopy "c:\test" /Y /s /I /M /D \\CAFserver\users\elie\test >>"c:\ErrFile.txt"
set err=%errorlevel%
echo Errorlevel: %err% >>"c:\ErrFile.txt"

cls
echo.
echo Complete. With an Errorlevel of: %err%
ping -n 2 localhost >>NUL
exit /b
::----------------------- FileName.bat --------------------------::

Then tell me if it copies the files from c:\test to the \\server..\\..\test dir

gL,
[r.D]
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
The reason that I've suggested this, is because the line:
xcopy "c:\temp\bat" /Y /s /I /M /D \\CAFserver\users\elie\temp\bat >>"c:\ErrFile.txt"
Works fine.
And due to the similarity between the files in "c:\test" and "c:\temp\bat", it doesn't make sense why the batch file copies one succesffuly, and not the other.

So, perhaps it's related to the server side, and all these switch statements don't 'help' :P. So, I recon that if you give the above a go, it may well help to solve this 'mystery' :)

gL,
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
IT's working thank you very much.  :)

What is the best way to put to the user.  AS you see I want his document to be copied.  There is any issue in windows where i can put it? as automated etc.../? I know in start up menu... but there is no like a time i can run the script ///

?

Thank yo.
0
 

Author Comment

by:elieazzi
Comment Utility
Pete, I want know about the domain way you've told me how i'm able to set up the synchronise a folder...

please advice as another solution.
0
 
LVL 23

Expert Comment

by:DanCh99
Comment Utility
you can also use the Resource Kit tool robocopy.exe to run constantly.  It could be set to check each users directory for new files, and copy them over if they don't exist.  
I can dig out an example if you want.

personally, I'd keep all the data on the Servers, and replicate it back to the laptops - more reliable, and easier to back up.

also, check out shutdown/log off scripts:
http://www.jsiinc.com/SUBM/tip6100/rh6128.htm
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
>"but there is no like a time i can run the script"
Do you mean, you'd like to set a time, for all the scripts to be backed up?  If so, you could either download some freeware software, or, use at.exe to synchronise it a time, to run the batch script automatically...?

gL,
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
Good morning,

Here is the scenario:

One of my users is using his laptop at work.  Every night his taking his laptop at home.
all his documents currently are on the C:\ drives.

I'm looking for a solution to copy his documents to the server which has been done thank Dr. Warezz, the other issue once these documents has been copied to the server and this user want copy a specific file to his c:\folder to be able to work at home, then i want this file to be copied back to the server in a way synchronise i think do you see my point.

If i decide to use this script and Peter told me I can synchronise a domain folder, how i'm able to do this, or how i'm able to run a script daily... i'm confuse how i want run or use the synchronie issue...

Thank you for your help all.
El.
0
 
LVL 6

Expert Comment

by:tanelorn
Comment Utility
have you looked at using the briefcase?

to syncronize the files, all he has to do is right click and select update all.

I have some users set up this way.

here's how we do it here

on the server, in the users folder du jour..
right click, and create new briefcase

double click it and a window will pop up and click next or ok, what ever it happens to be.
rename the briefcase to what ever you want.

now, right click on the breifcase, and select copy

then right click on the users desktop and select paste shortcut.

now.  all the user has to do is copy the folders he wants to syncronize from his hard drive into the newly created shortcut on the desktop.

they edit files on the hard drive,   they right click on the breifcase shorcut, and select update all.
they will sync.
if files are edited on the server, they right click on the breifcase shorcut, and select update all.
They will sync.

kind of a manual solution, but it will work on demand...

Tanelorn
0
 

Author Comment

by:elieazzi
Comment Utility
I didn't find a briefcase in Windows XP.
0
 
LVL 6

Expert Comment

by:tanelorn
Comment Utility
I know you've accepted an answer already.  I don't want to take anything away from DrWarezz, but here's a link on how to use the briefcase in windows xp

http://support.microsoft.com/default.aspx?scid=kb;en-us;307885&sd=tech

have a look at the article..  I think the key to it is the syncronization that it has built in.


T
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
8-) Thanks for the points.
      Take a look at tanelorns' link. That should solve your second problem :) Thanks tanelorn.

Best of luck,
[r.D]
0
 

Author Comment

by:elieazzi
Comment Utility
Thank you Tanelorn for your generosity support.   You have a nice feedback.

El.
0
 
LVL 6

Expert Comment

by:tanelorn
Comment Utility
Thanks!!

;)

T
0
 

Expert Comment

by:AndrewWalsh
Comment Utility
I have been using an Xcopy script for a while to backup user's my documents to a network folder with great results.

What I would like to know is if there is a way to delete files on the network folder if they are deleted on the workstation?  Right now if I delete files in My Documents folder, they are still on the network folder after the backup script runs.  Is there another option in Xcopy that will enable the files to be deleted from the server?

I'd rather not use synchronization utilities if possible.

Thanks
0
 
LVL 9

Expert Comment

by:DrWarezz
Comment Utility
Soz, Andrew, you'll need to ask a new question for this.

[r.D]
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now