Batch File If Else With Network Drive

I am trying to get a batch file that will check to see if a user has a network drive and if it does copy a file to it, if it does not I want it to copy the file to the C: drive.  i have a script but am not sure what i am doing wrong.  here is the script:

IF EXIST Q: ( Echo f | xcopy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\outlook.ost" "Q:\outlook.ost" /y /d )
ELSE ( copy "C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\outlook.ost" "C:\outlook.ost" /y )
bager1802Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
Not that it should make a difference, but WHY are you using XCOPY in one part and plain old COPY in the other?

And why are you using "C:\Documents and Settings\%username%\..." instead of "%userprofile%\..."

And MOST IMPORTANTLY... you say it's not working... Are there error messages?  What happens when you run the script manually (that's how you can best start to debug scripts).
0
t0t0Commented:
Would the following code not be better for you?



set source=C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook
set file=outlook.ost

if exist Q: (
   xcopy "%source%\%file%" Q:\  /y /d
) else (
   copy /y "%source%\%file%" C:\
)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t0t0Commented:
Or even this?


set source=C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook

if exist Q: (
   xcopy "%source%\outlook.ost" Q:\  /y /d
) else (
   copy /y "%source%\outlook.ost" C:\
)
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

bager1802Author Commented:
I am using xcopy for the network location because it would run but not copy any files if i just used copy.  and username was the variable that i knew off the top of my head and it worked so i saw no reason not ot use it.
as for the errors, when there is a network location it works perfectly but when there is no network location it runs and closes quickly but does not copy any files to the local drive, and there are no errors that I can see.
0
bager1802Author Commented:
Thank You, that was great and an amazingly quick response!!!
0
Bill PrewCommented:
I don't see anything obvious wrong with your code, what error(s) are you getting?

I would suggest a slightly cleaner version that would look something like this:

set SourceFile=C:\Documents and Settings\%username%\Local Settings\Application Data\Microsoft\Outlook\outlook.ost
if exist Q: (set DestLoc=Q:\) else (set DestLoc=C:\)
copy /y "%SourceFile%" "%DestLoc%"

Also, do consider LEEW comment above about how you want to reference the file to be copied.

~bp
0
Lee W, MVPTechnology and Business Process AdvisorCommented:
All environment variables can be reviewed by typing SET in a command prompt.

> [%username%] i saw no reason not ot use it.

Here's a reason - Your script won't work on Vista or Windows 7.  And if you ever alter Windows Setup and use a different folder in Windows XP, it won't work either.

%userprofile% on the other hand WILL Work because it INCLUDES the correct path right through the user name.  Make the change now or have a headache troubleshooting or rewriting in the future - your call.

If you were consistent using xcopy, you probably wouldn't have had the issue.
0
t0t0Commented:
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.