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.



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

@echo off
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

echo User has no write permissions to logfile.log


Open in new window

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

Open in new window

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!
Steve KnightIT ConsultancyCommented:
BTW personally I always write log files if possible to the temp drive, just use:

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


echo whatever >> %log%

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

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.