VB Script to change folder permission on the fly in Vista?

I am doing huge volume of OCRing.  My OCR program insists on saving untitled folders in my User\Mike\AppData\Local\Temp folder.  I discovered this when it hit 10K folders and the software would no longer run.  I have a VBS that deletes the Untiled folders and contents, but get "permission denied" errors unless I first uncheck "read only" in the Properties dialog, and keep the OCR from running while the script deletes the folders.  Because the SW keeps making these (maybe 8 to ten an hour) I cannot use some command line / startup command.  What I need is a method to incorportate into my deleting program that, just before it tries to delete a folder, it resets the permission away from Read Only.

I tried all of the UAC, global permissions, etc, tricks, and the only thing that works is to reset the folder and file attributes.

OS = Vista32 Biz
Mike CaldwellConsultant to IP industryAsked:
Who is Participating?
Todd GerbertIT ConsultantCommented:
The command "attrib -s -h -r * /S /D" will recursively remove the System, Hidden and Read-only attributes from every file/folder.

Mike CaldwellConsultant to IP industryAuthor Commented:
Can it be limited to the \Temp folder?  Can it be done with a .BAT file?
Mike CaldwellConsultant to IP industryAuthor Commented:
Wow; works like a champion! Thank you.
Todd GerbertIT ConsultantCommented:
Yes, and yes.  Put this in a .bat, it will remove ALL contents of temp directory.

cd users\mike\appdata\local\temp
attrib -s -h -r * /S /D
for /D %%i in (*) do rmdir /s /q %%i
del /F /S /Q *
Mike - Here's your old code with the attribute change/permission mods built in. I've never pasted code so hopefully it shows up legible. I'll repaste otherwise.
'******  	DELETE UNTITLED FOLDERS ********************
Const strSourceFolderName = "C:\Users\Mike\AppData\Local\Temp\"
Set objFSO = CreateObject("Scripting.fileSystemObject")
Set objFolder = objFSO.GetFolder(strSourceFolderName)
Set WshShell = WScript.CreateObject("WScript.Shell")
	For each objSubFolder In objFolder.Subfolders			
		FN = objSubFolder.Name
			if left(FN,8) = "Untitled" Then
				intReturn = WshShell.Run("Icacls C:\Users\Mike\AppData\Local\Temp\ /T /grant everyone:(F,RA)", 1, TRUE)
				if intReturn = 0 Then
					For Each objFile in objSubFolder.Files
                		If objFile.Attributes = objFile.Attributes AND 1 Then
						objFile.Attributes = objFile.Attributes = 0 
						End If
				End If
			End If

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.