none
delete folder in userprofile RRS feed

  • Question

  • I'm stuck.  I am trying to remove folders for an update, reinstall and the following works perfectly for a silent removal

    strFolder = "folder\location"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strFolder

    but I am having trouble with user profile

    Dim oWshS
    Set oWshS = CreateObject("WScript.Shell")
    strUsrProfile = oWshS.ExpandEnvironmentStrings("%UserProfile%")
    strPath = strUsrProfile & "C:\Users\%userprofile%\AppData\Roaming\name of software"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strFolder

    Get error in line 7

    Dim oWshS
    Set oWshS = CreateObject("WScript.Shell")
    strUsrProfile = oWshS.ExpandEnvironmentStrings("%UserProfile%")
    strPath = strUsrProfile & "C:\AppData\Roaming\name of software"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strFolder
    Get same error
    rmdir C:\user\%userprofile%\AppData\Roaming\name of software

    This works fine, but i don't want a dos popup  Can someone help with a short simple example that has no popup or user imput?

    • Moved by Bill_Stewart Monday, July 30, 2018 9:36 PM This is not "debug/fix/rewrite my script for me" forum
    Saturday, June 16, 2018 5:33 PM

All replies

  • Please look at how you are building the path.

    strPath = oWshS.ExpandEnvironmentStrings("%AppData%")  &  "\name of software"


    \_(ツ)_/



    • Edited by jrv Saturday, June 16, 2018 7:18 PM
    Saturday, June 16, 2018 7:18 PM
  • Please look at how you are building the path.

    strPath = oWshS.ExpandEnvironmentStrings("%AppData%")  &  "\name of software"


    \_(ツ)_/



    I have been testing, and testing and testing.  I believe the problem involves roaming.

    Location of file to delete.

    C:\Users\%userprofile%\AppData\Roaming\the Software to delete

    I have tried your suggestion

    Dim oWshS
    Set oWshS = CreateObject("WScript.Shell")
    strUsrProfile = oWshS.ExpandEnvironmentStrings("%UserProfile%")
    strPath = strUsrProfile & "C:\AppData\Roaming\name of software"
    
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strFolder
    
    'changed to
    
    Dim oWshS
    Set oWshS = CreateObject("WScript.Shell")
    strUsrProfile = oWshS.ExpandEnvironmentStrings("%UserProfile%")
    strPath = oWshS.ExpandEnvironmentStrings("%AppData%")  &  "\name of software"
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strPath

    And get,

    Have tried

    Dim oWshS
    Set oWshS = CreateObject("WScript.Shell")
    strUsrProfile = oWshS.ExpandEnvironmentStrings("%UserProfile%")
    strPath = oWshS.ExpandEnvironmentStrings("%AppData%")  &  "\name of software"
    set objFSO = createobject("Scripting.FileSystemObject")
    objFSO.DeleteFolder strPath, ture

    And get same result.

    Tried

    If Not WScript.Arguments.Named.Exists("elevate") Then
      CreateObject("Shell.Application").ShellExecute WScript.FullName _
        , """" & WScript.ScriptFullName & """ /elevate", "", "runas", 1
      WScript.Quit
    End If
    
    'Or this
    
    Set WshShell = WScript.CreateObject("WScript.Shell")
    If WScript.Arguments.Length = 0 Then
      Set ObjShell = CreateObject("Shell.Application")
      ObjShell.ShellExecute "wscript.exe" _
        , """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
      WScript.Quit
    End if
    
    'Or this
    
    If WScript.Arguments.length = 0 Then
       Set objShell = CreateObject("Shell.Application")
       'Pass a bogus argument, say [ uac]
       objShell.ShellExecute "wscript.exe", Chr(34) & _
          WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
    Else
       'Add your code here
    
    End If

    And any of them give me the permission denied popup, but wont stop.  I have to restart my pc and have about 200 popups by time it finally restarts.

    Have done this to get wscript.exe to run as admin at all times.

    Windows Registry Editor Version 5.00
    [HKEY_Current_User\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers]
    "c:\\windows\\system32\\wscript.exe"="RUNASADMIN"

    Which I could turn into

    REG ADD HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\AppCompatFlags\Layers /v c:\\windows\\system32\\wscript.exe /t  REG_SZ /d RUNASADMIN

    and do a reg del at the end of install, but even with regedit set to run as admin, I still get permission denied.

    Even this gives me failure.

    Option Explicit
       Dim oFSO, strAppData, objShell
       Set objShell = CreateObject("WScript.Shell")
       Set oFSO = CreateObject("Scripting.FileSystemObject")
       objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
       strAppData = objShell.ExpandEnvironmentStrings("%roaming%") & "\software to delete\"
    
       'here is the modified line
       oFSO.DeleteFile strAppData & "software to delete", True
    
       Set objShell = Nothing
       Set oFSO = Nothing
    
    'Or this
    
    Option Explicit
       Dim oFSO, strAppData, objShell
       Set objShell = CreateObject("WScript.Shell")
       Set oFSO = CreateObject("Scripting.FileSystemObject")
       objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
       strAppData = objShell.ExpandEnvironmentStrings("%appdata%") & "\software to delete\"
    
       'here is the modified line
       oFSO.DeleteFile strAppData & "software to delete", True
    
       Set objShell = Nothing
       Set oFSO = Nothing

    Gives me file not found.  I think this one is going to fall under the cmd.exe run as admin regedit and run the command as a dos file with @echo off.

    Tuesday, June 19, 2018 1:30 AM