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

Posted on 2012-08-31
Last Modified: 2012-08-31
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.



Question by:MichaelScottPaper
    LVL 20

    Expert Comment

    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

    LVL 11

    Accepted Solution

    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


    Author Closing Comment

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

    Expert Comment

    by:Steve Knight
    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.


    Featured Post

    Want to promote your upcoming event?

    Is your company attending an event or exhibiting at a trade show soon? Are you speaking at a conference? Spread the word by using a promotional banner in your email signature. This will ensure your organization’s most important contacts are in the know.

    Join & Write a Comment

    This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
    A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
    Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now