views:

256

answers:

2

In bash, I would use

[ -w ... ]

What's the equivalent for Windows batch files?

+1  A: 

you can do it like this using vbscript

Set objFS=CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
strFile = objArgs(0)
Set objFile = objFS.GetFile(strFile)
If Not objFile.Attributes And 1 Then
   WScript.Echo "The file is Read/Write."
Else
   WScript.Echo "The file is Read-only."
End If

save as check.vbs and on command line

c:\test> cscript //nologo check.vbs myfile
ghostdog74
ACLs are involved too. See Álvaro's answer.
Joey
+2  A: 

As far as I know, you can found out whether the file exists or not, but there's no way to know if it's writeable, apart from trying to write on it. It's not only a matter of not having the R flag; network and NTFS permissions are involved too.

If you can rewrite your code, you can capture the return code of the write operation trough the errorlevel.

Álvaro G. Vicario
+1 for mentioning permissions. It's definitely not just a matter of the read-only flag which would be trivial to check with `%~ax` or something like that.
Joey