Best way to check if a directory is available in a BAT script?

How to check if a directory can be writable by an executable user from a script package?

Here is what I have tried so far:

> cd "%PROGRAMFILES%" > echo. > foo Access is denied. > echo %ERRORLEVEL% 0 

So how about ...

 > copy NUL > foo Access is denied. > echo %ERRORLEVEL% 0 

Is not it? Then how about ...

 > copy foo bar Access is denied. 0 file(s) copied. > echo %ERRORLEVEL% 1 

This works, but it breaks if the file does not exist.

I read something about internal commands that do not install ERRORLEVEL, but copy obviously does this in the latter case.

+6
source share
5 answers

You can write copy %0 foo to copy the batch file itself.
It will always exist.

Remember to delete the file after this and make sure that you do not overwrite the existing file with this name.

There should be a better way to do this, but I don't know anything.

EDIT : even better, try mkdir foo .
If the batch file is offline (or if it is very large), it can be faster.

+3
source

Definitely running a team against him to find out if her refusal is an easy way to do this. You can also use CACLS to determine exactly which permissions or not. Here is a sample.

In the CACLS /? CMD type

CACLS "filename" will give you access to the current permissions in the file. R = Read, W = Write, C = Change (same as write?), F = Full access.

EDIT: You can also use the directory name.

To check, you must:

 FOR /F "USEBACKQ tokens=2 delims=:" %%F IN (`CACLS "filename" ^| FIND "%username%"`) DO ( IF "%%F"=="W" (SET value=true && GOTO:NEXT) IF "%%F"=="F" (SET value=true && GOTO:NEXT) IF "%%F"=="C" (SET value=true && GOTO:NEXT) SET value=false ) ECHO This user does not have permissions to write to file. GOTO:EOF :NEXT ECHO This user is able to write to file. 
+3
source

I found that running copy in a batch file repeated an error in STDERR, but %ERRORLEVEL% remained untouched (still 0). therefore, a workaround was to combine the command with conditional execution of set .

 copy /Y NUL "%FOLDER%\.writable" > NUL 2>&1 && set WRITEOK=1 IF DEFINED WRITEOK ( rem ---- we have write access ---- ... ) else ( rem ---- we don't ---- ... ) 

This is tested on XP and 7 and seems to work reliably.

+1
source
 set testdir=%programfiles% set myguid={A4E30755-FE04-4ab7-BD7F-E006E37B7BF7}.tmp set waccess=0 echo.> "%testdir%\%myguid%"&&(set waccess=1&del "%testdir%\%myguid%") echo write access=%waccess% 
0
source

The extension for Mechaflash's answer and solves the problem of overwriting the file, generating a unique file name for the "test" file.

 @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET "a=%~1" SET "b=" SET "g=0" :a SET "c= '1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm, .~!@ #$%%^&()_+QWERTYUIOP{}ASDFGHJKLZXCVBNM" SET /A "d=0, e=1" :b IF "!c!" NEQ "" ( IF "!c:~%d%,1!" NEQ "" ( IF EXIST "!a!\!b!!c:~%d%,1!" ( SET "c=!c:~0,%d%!!c:~%e%!" ) ELSE ( SET /A "d=!d!+1, e=!e!+1" ) GOTO :b ) ) IF "!c!" EQU "" ( SET "c= '1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm, .~!@ #$%%^&()_+QWERTYUIOP{}ASDFGHJKLZXCVBNM" :c IF "!c!" NEQ "" ( IF "!c:~%d%,1!" NEQ "" ( SET /A "d=!d!+1" GOTO :c ) ) SET /A "d=!d!-1" SET /A "f=%RANDOM%*!d!/32768" SET "b=!b!!c:~%f%,1!" GOTO :a ) ELSE ( SET /A "d=!d!-1" SET /A "f=%RANDOM%*!d!/32768" SET "b=!b!!c:~%f%,1!" ) ((ECHO EXIT>"!a!\!b!" && SET "g=1") & IF EXIST "!a!\!b!" DEL /F "!a!\!b!") >NUL 2>&1 ENDLOCAL & (SET "a=%g%") IF "%a%" EQU "1" ECHO TRUE 

( %~1 is the input directory)
EDIT: If you want a safer option

 @ECHO OFF SETLOCAL ENABLEDELAYEDEXPANSION SET "a=%~1" SET "b=" SET "g=0" :a SET "c= '1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm, .~!@ #$%%^&()_+QWERTYUIOP{}ASDFGHJKLZXCVBNM" SET /A "d=0, e=1" :b IF "!c!" NEQ "" ( IF "!c:~%d%,1!" NEQ "" ( IF EXIST "!a!\!b!!c:~%d%,1!" ( SET "c=!c:~0,%d%!!c:~%e%!" ) ELSE ( SET /A "d=!d!+1, e=!e!+1" ) GOTO :b ) ) IF "!c!" EQU "" ( SET "c= '1234567890-=qwertyuiop[]asdfghjkl;'zxcvbnm, .~!@ #$%%^&()_+QWERTYUIOP{}ASDFGHJKLZXCVBNM" :c IF "!c!" NEQ "" ( IF "!c:~%d%,1!" NEQ "" ( SET /A "d=!d!+1" GOTO :c ) ) SET /A "d=!d!-1" SET /A "f=%RANDOM%*!d!/32768" SET "b=!b!!c:~%f%,1!" GOTO :a ) ELSE ( SET /A "d=!d!-1" SET /A "f=%RANDOM%*!d!/32768" SET "b=!b!!c:~%f%,1!" ) IF EXIST "!a!\!b!" ( SET "b=!b:~0,-1!" GOTO :a ) ELSE ( ((ECHO EXIT>"!a!\!b!" && SET "g=1") & IF EXIST "!a!\!b!" DEL /F "!a!\!b!") >NUL 2>&1 ) ENDLOCAL & (SET "a=%g%") IF "%a%" EQU "1" ECHO TRUE 
0
source

Source: https://habr.com/ru/post/896421/


All Articles