• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1678
  • Last Modified:

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

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
elieazzi
Asked:
elieazzi
  • 19
  • 18
  • 3
  • +4
1 Solution
 
Pete LongTechnical ConsultantCommented:
Do you have a domain? if so simply redirect "my documents" to the server through policy?
0
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Pete LongTechnical ConsultantCommented:
>>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
 
elieazziAuthor Commented:
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
 
elieazziAuthor Commented:
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
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
Could you please write the script for me. tks.
0
 
DrWarezzCommented:
: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
 
DrWarezzCommented:
Woops, replace:

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

with:

for %%i in (c:\test\*.*) do call :PROCESS %%i
0
 
DrWarezzCommented:
Actually, one more correction:

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

:)
0
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
For-SoftCommented:
My proposition is to use /D switch with XCOPY.
It will copy a file only if it is newer than existed.
0
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
For-SoftCommented:
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
 
DrWarezzCommented:
So, Elie needs to remove some of the switches, right, For-Soft?
[r.D]
0
 
For-SoftCommented:
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
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
Yeah, that looks right.
[r.D]
0
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
DrWarezzCommented:
Ermm... give me a little longer..
[r.D]
0
 
elieazziAuthor Commented:
okay tks.
0
 
DrWarezzCommented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
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
 
elieazziAuthor Commented:
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
 
Danny ChildIT ManagerCommented:
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
 
DrWarezzCommented:
>"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
 
elieazziAuthor Commented:
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
 
tanelornCommented:
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
 
elieazziAuthor Commented:
I didn't find a briefcase in Windows XP.
0
 
tanelornCommented:
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
 
DrWarezzCommented:
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
 
elieazziAuthor Commented:
Thank you Tanelorn for your generosity support.   You have a nice feedback.

El.
0
 
tanelornCommented:
Thanks!!

;)

T
0
 
AndrewWalshCommented:
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
 
DrWarezzCommented:
Soz, Andrew, you'll need to ask a new question for this.

[r.D]
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 19
  • 18
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now