• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

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 )
0
bager1802
Asked:
bager1802
  • 3
  • 2
  • 2
  • +1
1 Solution
 
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
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now