first batch file-sending files from one PC to another

master7045

Senior member
Jul 15, 2005
729
0
76
So I just got a promotion at work and part of my new responsibilites will involve creating batch files for just about anything I can. We mainly use them to distro files out to retail stores worldwide.

Anyways, I just started editing and building batch files at home to build my skills. This is the first one I've built from scratch, but let me explain what I'm trying to do.

I have 2 computers at home and I am not very good at burning my CD's to both computers, mostly it's to my laptop, but my desktop has more HDD space so I'd like to use that as a backup. Eventually the desktop will become a media center so I want all my music on that PC. In my batch file, I want it to copy either My Music, My Pictures, or My Documents directories.

The problem I'm running into is accessing my desktop PC. It is named "office-desktop" and my laptop is called "Matt-laptop". Without further ado, here is my batch file.

btw, running Windows XP Pro SP3 on both PC's. I have installed choice.exe on both PC's as well. Again, my problem is accessing office-desktop from my laptop.

Thanks!


@echo off
:: ####################################################################
:: Created By: MATT NARDI
:: Version: 1.0
:: Created Date: 03 DEC 2009
:: Modified Date: 07 DEC 2009
:: Modified By:
:: Purpose: Select what folder to Copy contents of My Documents
####################################################################

title Copy My Documents

color 4f
CLS
echo.

:HOME
cls
echo What would you like to copy?
echo.
echo 1) Copy Music
echo 2) Copy Pictures
echo 3) Copy My Documents
echo 4) Exit


CHOICE /C:1234 /T:4,30 /N Please Make a Selection
if errorlevel 4 goto EXIT
if errorlevel 3 goto DOCUMENTS
if errorlevel 2 goto PICTURES
if errorlevel 1 goto MUSIC

:MUSIC

CHOICE /T:n,60 "Are you ready to copy music"
if errorlevel 2 goto HOME

echo Copying Music from Laptop to PC
xcopy /H /C /S /D /-Y "C:\Documents and Settings\Matt Nardi\My Documents\My Music" "\\office-desktop\C$\Documents

and Settings\Administrator\My Documents\My Music"
echo Process ran on: %computername%
pause
CLS
goto HOME


:pICTURES

CHOICE /T:n,30 "Are you ready to copy Pictures"
if errorlevel 2 goto HOME

echo Copying My Pictures from Laptop to PC
xcopy /H /C /S /D /-Y "C:\Documents and Settings\Matt Nardi\My Documents\My Pictures"

"\\office-desktop\C$\Documents and Settings\Administrator\My Documents\My Pictures"
echo Process ran on: %computername%
pause
CLS
goto HOME



:DOCUMENTS

CHOICE /T:n,60 "Are you ready to copy My Documents"
if errorlevel 2 Goto HOME

echo Copying My Documents from Laptop to PC
xcopy /H /C /S /D /-Y "C:\Documents and Settings\Matt Nardi\My Documents" "\\office-desktop\C$\Documents and

Settings\Administrator\My Documents"
echo Process ran on: %computername%
pause
CLS
goto HOME


pause
:Exit
exit
 

master7045

Senior member
Jul 15, 2005
729
0
76
So I just noticed all the :) junk in there and don't know how to disable it. Sorry for that.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
I think we can read through the smilies. So what's happening when you run this? I'm no batch file expert, but nothing about the syntax jumps out at me as incorrect. If it isn't a syntax issue your problem is likely permissions.

Post the actual results and we may be able to help.
 

Ken g6

Programming Moderator, Elite Member
Moderator
Dec 11, 1999
16,560
4,473
75
From the laptop, does:

dir "\\office-desktop\C$\Documents and Settings\Administrator\My Documents\My Music"

work? If not, try:

explorer "\\office-desktop\C$\Documents and Settings\Administrator\My Documents\My Music"

Does it prompt you for a login/password? If so, you need to add an account on the desktop to match the laptop.

If the first command works after executing the second command, but there was no login/password prompt...would it be a problem having a few extra Explorer windows up?

Edit: Come to think of it, accessing those X$ style drives may be a problem. Try adding a share for "My Documents", e.g. "mndocs".
 
Last edited:

master7045

Senior member
Jul 15, 2005
729
0
76
From the laptop, does:
If not, try:

explorer "\\office-desktop\C$\Documents and Settings\Administrator\My Documents\My Music"

Does it prompt you for a login/password? If so, you need to add an account on the desktop to match the laptop.

That's the problem. How do I add an account? I just use the admin account with no password. Do I need to create another account on the desktop called laptop? This is where I get somewhat confused. Sorry for the long post, but I was confident on the batch file, but wasn't sure how to connect to the desktop. If this should have been in a different area I apologize
 

kschaffner

Golden Member
Feb 12, 2006
1,098
0
76
since you are running xp you just need to right click the folder you want the files to transfer to and share it by opening sharing and security. Also enable other users to edit the files in there, there should be a check box for it.

might want to try changing your batch file to this for the destination target
"\\office-desktop\C: \Documents and Settings\Administrator\My Documents\My Pictures"
-------------------^ no space here
 
Last edited:

MerlinRML

Senior member
Sep 9, 2005
207
0
71
I'm not a batch file expert, but I've written a few. In my experience, I've found too many problems trying to use UNC paths while using batch files. There are just too many commands that don't support them.

You might try mapping the share as a drive, then just using your command with a drive letter.

Look into doing something like
Code:
REM Map the network drive and check for error
net use /user:username drive_letter: \\unc_path\share_name password
if not exist drive_letter: goto error
...
REM Delete the network drive when finished
net use /d drive_letter

then all your copy commands can just use drive_letter\path as an argument instead of a UNC path.

Other than that, I think you've got a very good start on your first batch file.

A few suggestions that you should look into as you continue to write more batch scripts.

1. Use more variables because it means less having to hunt and find things and it makes it easier to change things in one place if you have to update your scripts.
i.e.
Code:
set drive_letter=H:
net use %DRIVE_LETTER% \\
IF NOT EXIST %DRIVE_LETTER% GOTO ERROR

Along those same lines, you should use system environment variables for things like:
Code:
xcopy /H /C /S /D /-Y "C:\Documents and Settings\Matt Nardi\My Documents\My Pictures"
documents and settings\matt nardi\ could probably be replaced with the system evnrionment variable %USERPROFILE% as long as you're assuming that only the currently logged in user can backup their own stuff.
Code:
xcopy /H /C /S /D /-Y "%USERPROFILE%\My Documents\My Pictures"

This makes the script usable for multiple users and you don't have to know exact paths.

2. Error handling, error checking
You don't do any error level checking or return checking for the xcopy command. You just run it and assume it works. I'm not sure if xcopy returns errorlevels, but you should look into that.
IF ERRORLEVEL NOT 0 GOTO ERROR
or
IF %ERRORLEVEL%=1 GOTO COPY_FAIL
If it doesn't do return codes, you could at minimum navigate to the target directory and do a count of the files there and make sure there is something there. Or even compare counts with target and source directories. Depends on how elaborate you want to get.

Looks good. Keep working on it.
 

DivideBYZero

Lifer
May 18, 2001
24,117
2
0
Check out Robocopy, too. Much better than regular copy for long distance, i.e., retries and other great features.