2

How can I check if a folder exists if the user does not have read permissions?

I am writing a script to automate mapping of drives for users. The user has read permissions to their folder but not to the parent folder. So my current if statement is going straight to else even though the folder does exist.

My current code looks like this

if exist "\\domain\parent\%drive%" (
   net use i: /delete
   net use i: \\domain\parent\%drive% /P:YES
) else if exist "\\domain\parent\%username%" (
   net use i: /delete
   net use i: \\domain\parent\%username% /P:YES
) else (
   echo error: folder not found
   echo error: unable to map I drive
   pause
   exit
)

So the user has read permissions to their folder name, structured either %drive% or %username%, but they do not have read permissions to the parent folder.

I am able to map the drive manually but I'm wondering if it's possible to use if exist to check if folders exist even if the user does not have read permissions to the parent?

UPDATE: This code works if the user has read permissions to the parent and sub folders but is there some way to check if the file exists even if the user does not have read permissions to the parent folder?

hotlinks0
  • 101
  • 4
  • 12
  • Take a look at: https://stackoverflow.com/questions/21033801/checking-if-a-folder-exists-using-a-bat-file – Zakir May 29 '17 at 17:19
  • @zakir thanks but that question only deals with if the file/folder exists. It does not touch on permissions at all – hotlinks0 May 29 '17 at 17:30

1 Answers1

1

This could help you:

setlocal enableDelayedExpansion

for /f "delims=" %p in ('attrib ntuser.ini') do (
    set process=!%p:~0,8!
    echo !process! | findstr "H"
)

This command does:

  • Read attribute of file.ext
  • Find if the permission result contains H, if so it means that the file is Hidden, you can change the H to whatever you like, just check attrib /?

This is the correct if statement.

if exist filename/foldername (
    command
) else (
    if blah == blah (
        command2
    ) else (
        command3
  )
)

You should combine multiple if statements to emulate if, else if, else statement

  • 1
    1. Your `attrib`/`findstr` approach also returns a file containing `H` in its path, even it is not Hidden. 2. There is no problem in using `else if`; it is nothing but another `if` command used as what you call "else command", without the surrounding parentheses, which are actually not necessary, but useful for readability (you do need them for the "(if) command" though unless there is no `else`). – aschipfl May 30 '17 at 12:08
  • @aschipfl 3 months late, but I fixed the issue you mentioned. –  Aug 16 '17 at 10:53