Solved

Help with a batch file on Windows 7

Posted on 2014-02-26
15
496 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Problem to open text file 11 147
Why use this lambda? 12 98
Mobile apps - web/native or hybrid? 1 122
Shared files and folders migration 2 67
This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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