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

Posted on 2009-02-13
Last Modified: 2013-12-04
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
Question by:Mike Caldwell
    LVL 33

    Accepted Solution

    The command "attrib -s -h -r * /S /D" will recursively remove the System, Hidden and Read-only attributes from every file/folder.

    LVL 1

    Author Comment

    by:Mike Caldwell
    Can it be limited to the \Temp folder?  Can it be done with a .BAT file?
    LVL 1

    Author Comment

    by:Mike Caldwell
    Wow; works like a champion! Thank you.
    LVL 33

    Expert Comment

    by:Todd Gerbert
    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 *
    LVL 5

    Expert Comment

    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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Users of Windows 10 Professional can disable automatic reboots using the policy editor. This tool is not included in the Windows home edition. But don't worry! Follow the instructions below to install (a Win7) policy editor on your Windows 10 Home e…
    Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
    The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now