[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2012-08-31
Medium Priority
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

ID: 38355876
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

LVL 12

Accepted Solution

zalazar earned 400 total points
ID: 38355905
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

ID: 38355946
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
ID: 38356040
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Suggested Courses

872 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