Solved

Help with a batch file on Windows 7

Posted on 2014-02-26
15
484 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
Comment Utility
Just to be sure. Are the drives all local to the machine running the script?
0
 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
I have tested your script locally on my pc only making changes for source and destination drive paths with no issues.
0
 
LVL 15

Author Comment

by:David L. Hansen
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 43

Expert Comment

by:Steve Knight
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the tips dragon.
0
 
LVL 15

Author Comment

by:David L. Hansen
Comment Utility
It is working just as it should. :)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now