Test for write access when echoing output to a text file.

Upon launch of a application batch script, the script tries to output information to a text file on a network share.

I have just found out that some users have write access and some users do not.  

I want to be able to test for the person ability to write to that text file.

It appears that the echo command does not return an ERRORLEVEL status.

In unix, the IF command has tests for this type of thing.

Whats an simple and elegant way to do it in batch?


C:\>echo stuff >> no_write_perms_to_this.log
Access is denied.

C:\>echo %ERRORLEVEL%
0

C:\>

Thanks!
MichaelScottPaperApplication SupportAsked:
Who is Participating?
 
zalazarConnect With a Mentor Commented:
The code below can accomplish this and does work via the && functionality.

@echo off
setlocal
set _writeaccess=0
echo stuff >> logfile.log && set _writeaccess=1
if %_writeaccess% equ 0 goto _noaccess
echo User has write permissions to logfile.log
goto _end

:_noaccess
echo User has no write permissions to logfile.log

:_end

Open in new window

0
 
thehagmanCommented:
Echo is an "internal" command (so are type and many others) and therefore has no return code.
I suggest to use an external command like copy

copy no_write_perms_to_this.log + some_empty_file no_write_perms_to_this.log
ecoh %ERRORLEVEL%

Open in new window

0
 
MichaelScottPaperApplication SupportAuthor Commented:
thehagman, I did test this solution and it did work!

zalazar, I tested your solution and it did work as well!  I do think I am going to use zalazar's solution!

Thanks both of you!
0
 
Steve KnightIT ConsultancyCommented:
BTW personally I always write log files if possible to the temp drive, just use:

set log="%temp%\logfile.log"

and

echo whatever >> %log%

You can still launch in notepad / send by email or whatever is appropriate at the end of the script.

Steve
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.