access is denied

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

access is denied

#1 Post by Adrianvdh » 03 Aug 2013 16:41

Hello everyone...

The title may look like a n00bs question, but it isn't, I think.
I don't have any problems with that, I am working on a batch project, for the the last year and this recently accrued to me. My batch file has to write a log file to the directory that the batch file is in, and my program has trouble shooting of course. So I need my program to check is access is denied when writing that log file.

Now I have a small test batch file...

Code: Select all

echo Started program!>>batfile.txt
call :checklogUAC

:checklogUAC
if "%errorlevel%"=="0" exit /b
if "%errorlevel%"=="1" set "writeError=Writing log to file"
goto Errorwritefiles


Now the errorlevel check is not working for some reason. Currently I have made the log file read only so it will get the access is denied error, but the errorlevel check won't work.

Any ideas where I went wrong?

Thanks for your time :)

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#2 Post by aGerman » 03 Aug 2013 17:15

Hi Adrianvdh

It's obvious:

Code: Select all

call :checklogUAC

Was CALL successful? Yes! What's the consequence? Errorlevel is 0! :wink:

Code: Select all

call :checklogUAC %errorlevel%

:checklogUAC
if "%1"=="0" (exit /b) else set "writeError=Writing log to file"
goto Errorwritefiles


Regards
aGerman

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: access is denied

#3 Post by Adrianvdh » 03 Aug 2013 17:34

Hi aGerman...

My program is still reporting an error, with or without read-only enabled on the log file...What do I do?

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#4 Post by aGerman » 03 Aug 2013 17:54

Er I just realized that the "access is denied" error doesn't set the errorlevel. Also the message wasn't written to stdErr directly. I have to think about a workaround ...

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#5 Post by aGerman » 03 Aug 2013 18:09

Got it :)

Code: Select all

(echo Started program!>>batfile.txt) 2>&1 |findstr . && (
  set "writeError=Writing log to file"
  call :Errorwritefilescall
)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: access is denied

#6 Post by penpen » 03 Aug 2013 19:33

If you don't want to write something to file, while checking you could use something like this:

Code: Select all

for /F %%a in ('^(^(set "X=" ^< nul^)^>^>batFile.txt^)2^>^&1') do echo error on accessing batFile.txt

penpen

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: access is denied

#7 Post by Adrianvdh » 04 Aug 2013 02:36

Sorry, none of them seem to work. Could you please try t write a function for me like in my first post that call the function on ever echo to the log, then check if the file is OK :)

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#8 Post by aGerman » 04 Aug 2013 04:54

Actually it should be sufficient
- to check only once since the risk is low that you can't write to the file if you were already able to write the first time
- to check for any character like I did; doesn't matter what error message you get back, what content it has or what language your mother tongue is
- to call :Errorwritefiles directly if an error was detected; it doesn't make any sense to call a subroutine (like :checklogUAC) that returns immediately if errorlevel was 0 without doing anything else - it will waste your time in that case :wink:

Regards
aGerman

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: access is denied

#9 Post by Adrianvdh » 04 Aug 2013 05:23

OK, How would I use foxidrive code then, I haven't a clue?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: access is denied

#10 Post by foxidrive » 04 Aug 2013 05:30

It didn't work and I deleted it. I wasn't testing it properly.

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#11 Post by aGerman » 04 Aug 2013 05:48

Maybe I should write my proposal in full batch context.

Code: Select all

@echo off &setlocal

(echo Started program!>>batfile.txt) 2>&1 |findstr . >nul && (
  set "writeError=Writing log to file"
  call :Errorwritefilescall
)

pause
goto :eof


:Errorwritefilescall
echo %writeError%
goto :eof

- If you have access it writes to batfile.txt and displays only the pause message.
- If you don't have access it calls :Errorwritefilescall where it displays your own message (since I don't know what your code in :Errorwritefilescall actually is), returns to the point where it was called and displays the pause message.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: access is denied

#12 Post by penpen » 04 Aug 2013 06:46

And if you really want to do this like in your opening post,
although i see it like aGerman, that one writing test per file is sufficient:

Code: Select all

echo Started program!>>batfile.txt
call :checklogUAC

goto :eof

:checklogUAC
set "writeError="
for /F %%a in ('^(^(set "X=" ^< nul^)^>^>batFile.txt^)2^>^&1') do set "writeError=Writing log to file"
if defined writeError goto :Errorwritefiles
exit /b
Note: This code assumes that the lock is not set/released between the echo and the call lines, which should be improbable but possible.
You can use this like aGermans code on the echo directly: Just replace the set "X=" <nul part with the echo.

If you want to use either code on all echos i recommend you to write a macro $echoToFile and use it instead of the echo:

Code: Select all

%$echoToFile% "batfile.txt" Started program!

penpen

Edit added the : to the label assumed, that this label exists somewhere as an inner procedure label.
Last edited by penpen on 04 Aug 2013 08:15, edited 1 time in total.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: access is denied

#13 Post by Adrianvdh » 04 Aug 2013 07:55

OK, the code you gave me has made something gone wrong, "echo." is not a valid command in cmd anymore?


'echo.' is not recognized as an internal or external command, operable program or batch file.

But your code works :)

aGerman
Expert
Posts: 4743
Joined: 22 Jan 2010 18:01
Location: Germany

Re: access is denied

#14 Post by aGerman » 04 Aug 2013 08:02

There is no "echo." - Neither in my code nor in penpens code. Try "echo(" instead.
No idea what you're doing :? Since we don't know how you're using our snippets I fear we can't help out.

Adrianvdh
Posts: 177
Joined: 16 May 2013 13:00

Re: access is denied

#15 Post by Adrianvdh » 04 Aug 2013 08:06

penpen wrote:And if you really want to do this like in your opening post,
although i see it like aGerman, that one writing test per file is sufficient:

Code: Select all

echo Started program!>>batfile.txt
call :checklogUAC

goto :eof

:checklogUAC
set "writeError="
for /F %%a in ('^(^(set "X=" ^< nul^)^>^>batFile.txt^)2^>^&1') do set "writeError=Writing log to file"
if defined writeError goto Errorwritefiles
exit /b
Note: This code assumes that the lock is not set/released between the echo and the call lines, which should be improbable but possible.
You can use this like aGermans code on the echo directly: Just replace the set "X=" <nul part with the echo.

If you want to use either code on all echos i recommend you to write a macro $echoToFile and use it instead of the echo:

Code: Select all

%$echoToFile% "batfile.txt" Started program!

penpen


I tested my batch file in Windows 8 in VirtualBox and the echo. works, but why suddenly would it do this in Windows 7, main OS. Let me restart my computer and see if I still have the problem :)

Post Reply