Solved

Help with a batch file on Windows 7

Posted on 2014-02-26
15
490 Views
Last Modified: 2014-02-27
I have a batch file, which I want to use for automating some backups, and although it seems to follow correct syntax, it just hangs (freezes).  Here is the code:
@echo off
REM variables
set drive=K:\Archive\laptop 
set backupcmd=xcopy /d /e /h /i /r /y

echo ### Backing up myData...
%backupcmd% "C:\myData" "%drive%\myData"

echo Backup Complete!
@pause

Open in new window

I do have more code that I want to put into the file but it will essentially be following the same structure as this (just a bunch of xcopy commands).  I'm not sure why this isn't working...no error message is displayed; it just echos back "Backing up myData..." in the DOS window and then sits like that, not responding.
0
Comment
Question by:David L. Hansen
  • 7
  • 5
  • 3
15 Comments
 
LVL 8

Assisted Solution

by:Jeff Perry
Jeff Perry earned 200 total points
ID: 39889949
Just to be sure. Are the drives all local to the machine running the script?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39889951
Well there is no logical reason off hand.  I would suggest remove the @echo off command so you can see what it is doing,  Maybe you have inadvertently called the batch file xcopy.cmd or xcopy.bat or there is another one in the same dir, or your path that is?

Presumably it carries on if you don't xcopy.

Steve
0
 
LVL 8

Expert Comment

by:Jeff Perry
ID: 39890008
I have tested your script locally on my pc only making changes for source and destination drive paths with no issues.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890016
For testing, I've called the batch file "BackupMyFiles.bat" and I'm running it directly from my desktop (no scheduler yet, I'm just executing it manually).

My K:\ drive is a network drive.

Perhaps it is a permissions issue (although my profile does have admin privileges)?
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39890028
Start a command prompt.  Type the commands in manually, or run the batch file without the @Echo off at the top.  Does it stop at the xcopy line still?

Put in your batch file:

xcopy /?
pause

Does that cause it to choke still?

Steve
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890068
From the command prompt I can successfully run xcopy, but only from a local drive to a local drive. When it comes to copying to any network drive it freezes.  Surprisingly, I can't even view them from the command prompt (eg. 'cd K:\' gives me nothing...it just stays at the current directory).
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 300 total points
ID: 39890090
Very odd.  Presumably Ok from within explorer, never seen that before?!  What about if you do either:

@echo off
net use y: \\server\share
y:
dir

or

@echo off
pushd \\server\share
dir
popd

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39890095
cd K:\ btw does not change the drive, you either need:

K:
cd \

or

cd /d K:\

or

pushd k:\
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890142
@echo off
net use y: \\server\share
y:
dir

Open in new window

worked just fine (as did the change-directory with the /d switch).
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890144
Think I need to always just use the full UNC??  I'll test that right now.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890154
That was it.  I just have to use the full UNC and it works :)   Not sure why...
0
 
LVL 8

Expert Comment

by:Jeff Perry
ID: 39890173
the cd command only changes the directory you have to switch to that drive by issuing the drive.

For instance in you cd K:\ it set the directory for K: to root if you then issue K: you will be placed in the root of K:

if you were to put

cd K:\Archive\laptop
set drive=K:

that should work.

Here is how my test script ended up.

@echo off
REM variables
cd Z:\Archive\laptop\iperf
set drive=Z:
set backupcmd=xcopy /d /e /h /i /r /y

echo ### Backing up myData...
%backupcmd% "C:\iperf" "%drive%"

echo Backup Complete!
@pause 

Open in new window


I had to put the entire archive path in my cd statement to get it to work.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 39890362
especially if you schedule this the drive mapping will not exist anyway unless you run it scheduled as a user AND that user is logged in AND the drive is mapped.

If you use net use y: .... you might also want to remove the drive first with

net use y. /delete

and also consider the pushd.  this sets the drive and path for you, and will map a temporary drive to the path if a unc, e.g.

pushd \\server\share
xcopy c:\mydir \backupdit
popd

popd puts the drive mapping and current dir back where it was.

Steve
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39890448
Thanks for the tips dragon.
0
 
LVL 15

Author Comment

by:David L. Hansen
ID: 39892209
It is working just as it should. :)
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

778 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