Link to home
Start Free TrialLog in
Avatar of MichaelScottPaper
MichaelScottPaperFlag for United States of America

asked on

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!
Avatar of Member_2_4694817
Member_2_4694817

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

ASKER CERTIFIED SOLUTION
Avatar of zalazar
zalazar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MichaelScottPaper

ASKER

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!
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