I'm running a routine delete all files within a folder, then delete the folder. It works, but always seems to leave one file behind. Assume some kind of file lock, but ideas on how to clear the lock would be appreciated.
If I manually delete the 'rogue' file, and rerun the routine the folder is deleted - so all of my folder permissions are correct.
thanks
fso.DeleteFile objFolder & "\*.pdf", true
fso.DeleteFolder objFolder, true
I've located the issue - it was down to a PDF object further up my page. Once I'd set this to 'Nothing' the fso delete command worked a treat.
thanks
Related
I am making a simple VBS application which has a bit where it creates a file in the startup directory. When I did it a while back, it worked, but I've since updated my code and need it to do other things too.
This is the code that creates the file:
Dim fso, openFile
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
It gives some sort of error saying it does not know where to go, I don't know if there's a problem with the file path I've supplied, because C:\ProgramData\Microsoft\Windows\Start Menu\Programs\ is a folder, obviously. I've also just discovered that there is no startup\ folder in it, is that what's causing it?
If so, is there something I can specify that creates the directory if it does not exist?
I'm very new to VBS and I have a very limited knowledge of it, so a simple way to do this would be much appreciated.
Thanks! :)
Is the filepath correct? (can you find it mannualy?) If yes, then try to copy the path in a variable and call the variable with the path.
Dim fso, openFile
Dim filepath = "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\startup\file.bat"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile(filepath)
And also make sure, that the file don't exists before you start the programm.
Good Luck
Jonas
I have a very simple script, which only prints current directory. That's the code:
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (WshShell.CurrentDirectory)
This script is called from .exe file. It works fine until the calling executable was run directly. If I create a link to exe-file and launch it, then it runs my .vbs and it prints the directory of link, not the .exe itself! How can I fix this?
Get help from FileSystemObject, (vbscript example) :
scriptdir=CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Wscript.Echo scriptdir
OK, maybe it's somehow clumsy, but I've discovered a workable solution. The idea is simple: get full script name and a short one. Then subtract the second from the first.
set WshShell = WScript.CreateObject("WScript.Shell")
Wscript.Echo (Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName)))
I have a program in HTA and all the auxiliary files are in the same folder, a subfolder of the AppData folder. I created an uninstaller HTA which uninstalls the program by simply removing the folder with FSO DeleteFolder() method. I converted it to an executable with HtaEdit. (If you don't know this program, it doesn't matter). What the executable does is that it creates an HTA in a temporary folder (along with the auxiliary files) and runs it. The problem is that when it does the DeleteFolder() method, an error message comes up saying "denied access". I don't think that it's an administrator problem since it's in the current user's AppData folder. When I try deleting another folder that way, it works just fine. I think that there are usually problems deleting the folder containing an HTA file which is being run, but the HTA file isn't in the folder I'm trying to delete but in a temporary folder. However, it's been called by an executable in the folder I'm trying to delete.
I'm using VBScript, but it does the same thing if I use JavaScript.
You can't delete a folder from within the same folder as long as there's still something holding an open handle to the folder or something inside it. For example, the following code will usually delete the parent folder of a VBScript:
Set fso = CreateObject("Scripting.FileSystemObject")
dir = fso.GetParentFolderName(WScript.ScriptFullName)
fso.DeleteFolder(dir)
or, in case of an HTA (which doesn't have a WScript object):
Set fso = CreateObject("Scripting.FileSystemObject")
htaPath = Replace(oHTA.CommandLine, """", "") '<HTA:APPLICATION ID="oHTA" ...>
dir = fso.GetParentFolderName(htaPath)
fso.DeleteFolder(dir)
These work, because the script interpreter reads the entire script into memory when the script is launched, so no open handle to the file remains.
However, the deletion of the folder will fail with a "permission denied" error if the folder is the current working directory of the script process, because in that situation there still is an open handle to the folder. The same applies if, for instance, the folder is open in Explorer or a command prompt.
You can check for open handles with handle.exe or Process Explorer.
My external hard drive was recently affected by the recycler.exe virus when I lent it to a friend. The virus affects only external drives. It changes the folders into shortcuts to those folders via an EXE file that it creates.
I am searching for a script to change all the shortcut targets to K:\{shortcut name}. However, I don't know scripting and in the last two days that I have tried to learn scripting, I am not sure which one I should use. VBScript seems the best option, but that's just my opinion.
Problem:
For example, earlier I had a folder called 'Anime'. Now I have a shortcut linking to that folder with the following target:
%windir%\system32\cmd.exe /c "start %cd%RECYCLER\894133bf.exe &&%windir%\explorer.exe %cd%Anime
The virus also creates a folder(and file) \RECYCLER\894133bf.exe and the shortcuts are linked through that EXE file (seen above).
I would like a batch file to convert the target path to:
K:\Anime
This way the shortcut can directly link to the file. The virus and folder were removed by Norton when I got my hard drive back. However, the shortcuts remain and they don't work unless I change the target path.
Since I have over 37 folders on my hard drive which have been converted to the shortcuts, I was thinking that maybe a script would be helpful doing the following:
Extracting the filename from the shortcut
Removing the .lnk from filename
Changing the target to K:\{shortcut name}
Go to next folder and loop until last folder
I came across a script to extract a filename here:
http://blogs.technet.com/b/heyscriptingguy/archive/2006/05/30/how-can-i-extract-just-the-file-name-from-the-full-path-to-the-file.aspx
However, I do not know how to put it in a loop to do it for each folder.
Any help will be much appreciated. Thank you very much.
objFolder.Name in the For Loop is the string for each Folder it's iterating through.
Option Explicit
Dim strFolderToSearch, objFSO, objRootFolder, objFolder, colSubfolders, strOutput
strFolderToSearch = InputBox("Enter Path:")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objRootFolder = objFSO.GetFolder(strFolderToSearch)
Set colSubfolders = objRootFolder.SubFolders
For Each objFolder in colSubfolders
strOutput = strOutput & objFolder.name
strOutput = strOutput & vbCrLf
MsgBox StrOutput
Next
When uninstalling a previous installation (that I'd built using InstallShield 2009), I wanted to delete the entire folder that the program was in at the end of the uninstall. I couldn't figure out how to do that using a Custom Aaction, so using the code below, I settled for deleting the file as soon as the installation starts. This works fine if the program was already installed... but if it wasn't installed before, it throws an error 1701 because, obviously, the folder didn't exist! I have no idea how to fix this issue and I know almost no VBScript. I started to do a try-catch to just gloss over the error, but apparently that doesn't exist in VBScript.
Dim fso, Folder2Delete
Folder2Delete = "C:\Program Files\MyProgramDir"
Set fso = CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder(Folder2Delete)
So either, How can I stick a Custom Action into an uninstall in InstallShield, or how can I set the VB script to only delete a file if it exists? Or last ditch, how can I get it to not show an error when it doesn't exist... ?
Thanks a lot, this is driving me crazy!
You can try this code:
Dim fso, Folder2Delete
Folder2Delete = Session.Property("CustomActionData")
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(Folder2Delete) Then
fso.DeleteFolder(Folder2Delete)
End If
For this custom action you can then set the action data (CustomActionData property) to:
[INSTALLDIR]
This way your action will delete whatever installation path your users set.