How check share UNC

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
darioit
Posts: 230
Joined: 02 Aug 2010 05:25

How check share UNC

#1 Post by darioit » 20 Jun 2013 02:36

Hello everyone,

I'm looking for a very simple script that check if UNC share as "\\server\abc" is available before copy some file into that.

I got error after copy file, but it would be useful to know before if a share is down or user has no permission to copy file.

Best Regards

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How check share UNC

#2 Post by Squashman » 20 Jun 2013 06:09

Well to see if the share is up and running you could just do an IF EXIST.

Code: Select all

if EXIST "\\Server\Share\Folder" echo exists


Not sure about testing for permissions. I guess I would just try and copy the batch file itself to the share and check the errorlevel.

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How check share UNC

#3 Post by darioit » 20 Jun 2013 06:20

I check this script

Code: Select all

if EXIST "\\Server\Share\Folder" echo exists


if exist errorlevel=0
if NOT exist errorlevel=0
if NOT authorized errorlevel=0

That's the point !

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: How check share UNC

#4 Post by foxidrive » 20 Jun 2013 06:35

Note the trailing slash - it defines a folder.

Code: Select all

if EXIST "\\Server\Share\Folder\" (
copy file
) else (
echo unavailable
)


Check it yourself to see what happens if you don't have access.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How check share UNC

#5 Post by Squashman » 20 Jun 2013 06:48

darioit wrote:
if exist errorlevel=0
if NOT exist errorlevel=0
if NOT authorized errorlevel=0

None of those are properly formatted commands.

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How check share UNC

#6 Post by Squashman » 20 Jun 2013 06:52

Foxidrive makes a good point about the trailing slash. That way it matches against a folder or share name and not a file.

It does work. I normally don't post code unless I tested it first.

Code: Select all

H:\>IF EXIST "\\Server\Share\TEMP\" echo the Folder exists
the Folder exists

darioit
Posts: 230
Joined: 02 Aug 2010 05:25

Re: How check share UNC

#7 Post by darioit » 21 Jun 2013 01:13

Squashman wrote:
darioit wrote:
if exist errorlevel=0
if NOT exist errorlevel=0
if NOT authorized errorlevel=0

None of those are properly formatted commands.



sorry this is not a command but the result of command
This is the right script:

Code: Select all

if exist "\\Server\Share\Folder" echo exists
echo %errorlevel%


in all case tested %errorlevel% is always 0

regards

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: How check share UNC

#8 Post by Squashman » 21 Jun 2013 06:46

darioit wrote:
Squashman wrote:
darioit wrote:
if exist errorlevel=0
if NOT exist errorlevel=0
if NOT authorized errorlevel=0

None of those are properly formatted commands.



sorry this is not a command but the result of command
This is the right script:

Code: Select all

if exist "\\Server\Share\Folder" echo exists
echo %errorlevel%


in all case tested %errorlevel% is always 0

regards

Of course the ERRORLEVEL will be zero because the IF command completed successfully. It didn't error out.

Code: Select all

H:\> if 1==1 echo equal
equal

H:\>echo %errorlevel%
0

H:\> if 1==0 echo equal

H:\>echo %errorlevel%
0


Either use the IF ELSE code Foxidrive gave you to execute another command when the condition is true or set a flag with another variable.

Code: Select all

if exist "\\Server\Share\Folder" set SHAREFLAG=Y

Post Reply