-2

I'm trying to delete a file from vbs, but I can't because this is a long extension and it must use quotation marks:

Set oShell = CreateObject ("Wscript.Shell")
    Dim strArgs6919
    strArgs6919 = "cmd /c del %APPDATA%\Microsoft\Windows\Start Menu\Programs\Startup\SoftEther VPN Client Manager Startup"
    oShell.Run strArgs6919, 0, false

I can remove it manually without problem, but I want to do it from vbs, how I can solve this? Thanks very much and regards!

  • 1
    Your code is contradicting your intended usage. If you want to do this with Vbscript then use the [File System Object](https://ss64.com/vb/filesystemobject.html) to do the delete instead of calling out to the `DEL` command built-in to `CMD.EXE`. – Squashman Apr 19 '21 at 22:01
  • @Squashman, how can do this? – Ulises Antonio Chávez Apr 19 '21 at 22:03
  • 1
    Does this answer your question? [Vbscript to delete files with special characters in their name](https://stackoverflow.com/questions/17442100/vbscript-to-delete-files-with-special-characters-in-their-name) – Squashman Apr 19 '21 at 22:13
  • And you will probably want to read this as well. [Can I pick up environment variables in vbscript?](https://stackoverflow.com/questions/904739/can-i-pick-up-environment-variables-in-vbscript-wsh-script) – Squashman Apr 19 '21 at 22:15
  • 2
    I would suggest your issue may be simpler than that. My first guess, based upon the location, is that you should be trying to delete a shortcut, i.e. change `SoftEther VPN Client Manager Startup`, to `SoftEther VPN Client Manager Startup.lnk`. **_Things get so much easier when you don't let Windows hide extensions for known filetypes_**. Obviously if you're doing this I'd ensure that you're also doublequoting that filepath, due to its included space characters. I'll assume at this stage that you know how to handle those nested doublequotes, if not please let us know – Compo Apr 19 '21 at 22:54
  • @Compo Wouldn’t assume anything tbh. It’s clear they don’t know VBScript so not likely to realise they need to escape the quotes to literal quotes around the file path. See this question time and time again. – user692942 Apr 19 '21 at 23:23
  • Technically its a side issue though, @user692942, because if they use the methodology suggested in the commented links, instead of running cmd.exe with arguments, it could be handled differently. As indicated in my previous comment the main issue is that it appears that they've not even got the actual file name correct. – Compo Apr 19 '21 at 23:28

1 Answers1

1

Here's a quick example based upon the cumulative advice you've received within the comments:

' VB Script Document
Option Explicit
Dim objFSO, objSh, strFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSh = CreateObject("WScript.Shell")
strFile = objSh.SpecialFolders("Startup") & "\SoftEther VPN Client Manager Startup.lnk"
If objFSO.FileExists(strFile) Then objFSO.DeleteFile strFile, True
Compo
  • 36,585
  • 5
  • 27
  • 39
  • I did try this: `Set shell = CreateObject("WScript.Shell") Set fso = CreateObject("Scripting.FileSystemObject") startupFolder = shell.SpecialFolders("Startup") & "\" filename = "C:\Users\"& USERNAME &"\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\SoftEther VPN Client Manager Startup.lnk" If fso.FileExists(filename) Then fso.DeleteFile filename, True` but don't work – Ulises Antonio Chávez Apr 20 '21 at 00:27
  • 1
    I'm not helping you with different code @UlisesAntonioChávez, my answer above is all I expect you to use and try. I will only debug my own code if it fails to work as intended, and you've already announced that it does. I'm not providing a one to one tutor and support service. – Compo Apr 20 '21 at 00:30
  • Ok, but your code works correctly, thanks – Ulises Antonio Chávez Apr 20 '21 at 00:36