Solved

Help with a batch file on Windows 7

Posted on 2014-02-26
15
494 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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