How to delete temp files created in %temp% folder using vbscript - vbscript

I am trying to delete the temporary files created in C:\Users\xxxxxx\AppData\Local\Temp using vbscript.

The best way would be to leverage robocopy... but if it must be done in vbscript... here's a simple method.
The run method below will execute hidden from the user interface.
Set fso = CreateObject("Scripting.FileSystemObject")
Set oshell = CreateObject("WScript.Shell")
EmptyFolder=oshell.ExpandEnvironmentStrings("%userprofile%") & "\Empty"
if NOT (fso.folderexists(EmptyFolder)) Then fso.CreateFolder(EmptyFolder)
oShell.run "robocopy ""%userprofile%\Empty"" ""%tmp%"" /purge", 0, true
if (fso.folderexists(EmptyFolder)) Then fso.DeleteFolder(EmptyFolder)

Related

Declare VBScript inside of VBScript (Running multiple .VBS at a time)

I need to run multiple .vbs at a time. This is the only way I was able to find online:
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
objShell.Run "1.vbs"
objShell.Run "2.vbs"
objShell.Run "3.vbs"
Set objShell = Nothing
To do that I'd have create 1.vbs, 2.vbs and 3.vbs separately. Is there a way to input them all as part of one .vbs file? Something like
Dim objShell
Set objShell = Wscript.CreateObject("WScript.Shell")
Dim vbs1 as vbscript
Dim vbs2 as vbscript
Dim vbs3 as vbscript
Set vbs1 =
"Set objExcel = CreateObject("Excel.Application")
objExcel.Application.Run "'C:\Users\test1.xlsm'!Module1.refresh"
objExcel.DisplayAlerts = False
objExcel.Application.Quit
Set objExcel = Nothing"
Set vbs2 = ' whatever code
Set vbs3 = ' whatever code
objShell.Run vbs1
objShell.Run vbs2
objShell.Run vbs3
Set objShell = Nothing
The purpose of this:
I have ~50 excel reports with connections to SQL that need to be updated every day.
To do that I've created a macro and added it to each of them. The macro refreshes connections/queries > refreshes pivot tables > removes the connections/queries > saves as a macro-free workbook in a specified location.
I wrote .vbs scripts for each report. It just runs that macro in every Excel workbook.
Instead of running every .vbs separately, I've created one main .vbs that references all prior created .vbs to run them at the same time.
My question is if there's a way to do that without creating 50 separate .vbs files.

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

How to Create Folders and Subfolders in %AppData%

First of all thank you very much for all the help I’ve found over 3 Vbscript that has save my life during this last six months.
I’m new to Vbscripting and I’m currently working on getting a Vbscript that create folders and copy a file at the same time overwrite that folder and file if they already exist
Folder and subfolders to be created (Avaya) C:\Users\My Username\AppData\Roaming\Avaya\ Avaya\one-X Agent\2.5\
File from (Myfile.txt) C:\Myfile.txt to C:\Users\My Username\AppData\Roaming\Avaya\one-X Agent\2.5\
I get “Path not found” error, but If I leave the path till (Avaya) it creates Avaya Folder but not it’s subfolders C:\Users\My Username\AppData\Roaming\Avaya\
Here’s what I have and thank you in advance
Dim fso, vFolder
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
set objWShell = wScript.createObject("WScript.Shell")
usrName = objWShell.expandEnvironmentStrings("%USERNAME%")
Set fso = CreateObject("Scripting.FileSystemObject")
Set vFolder = fso.CreateFolder("C:\Users\" & usrName & "\AppData\Roaming\Avaya\one-X Agent\2.5\")
CreateFolderDemo = vFolder.Path
The problem is that CreateFolder does not create intermediate folders. The FSO doesn't have a method that does that. It might be easier to use mkdir like this:
Option Explicit
Dim shl
Set shl = CreateObject("WScript.Shell")
Call shl.Run("%COMSPEC% /c mkdir ""%APPDATA%\Avaya\one-X Agent\2.5""",0,true)
Some errors:
You declare fso, but you use objFso
You use %USERNAME% but you should consider %APPDATA% instead
You should use OPTION EXPLICIT to detect typos and undefined variables
You should make your code easier to read by dimming one variable at a time
CreateFolder doesn't create the entire tree, so you need to use FolderExists
For example:
Option Explicit
Dim objWShell
Set objWShell = WScript.CreateObject("WScript.Shell")
Dim appData
appData = objWShell.expandEnvironmentStrings("%APPDATA%")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(appData + "\Avaya") Then
objFso.CreateFolder appData + "\Avaya"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent"
End If
If Not objFso.FolderExists(appData + "\Avaya\one-X Agent\2.5") Then
objFso.CreateFolder appData + "\Avaya\one-X Agent\2.5"
End If
Lastly, it's not clear why your solution needs to be in VBScript. It appears that your requirements are creating folders and copying files, which means, batch files would probably be a far more simpler.

How to execute a vbscript from another vbscript?

For example, if I have two vbscript files: A.vbs and B.vbs. I would like the vbscript in B.vbs to execute A.vbs, such peudo-code would look like the following:
'B.vbs
execute("A.vbs")
Just as simple as this line, but I couldn't really find a valid syntax to accomplish such task. Hope someone could help me out, thanks.
Dim oShell
Set oShell = Wscript.CreateObject("WScript.Shell")
oShell.Run "name_of_vbs_file_here.vbs"
Set oShell = Nothing
The following will execute a.vbs as it were a part of the calling script itself
include "a.vbs"
sub include(script)
dim fso, file
set fso = createObject ("Scripting.Filesystemobject")
if fso.FileExists (script) then
set file = fso.OpenTextFile (script)
executeGlobal file.ReadAll ()
file.Close
set file = nothing
end if
set fso = nothing
end sub
Dim Shell
Set Shell = CreateObject ("WScript.Shell")
Shell.Run "a.vbs"
You can also spice it up a little by adding things like "SendKeys" or other Shells.
createobject("wscript.shell").run"a.vbs"
or if your files aren't in the same folder
createobject("wscript.shell").run"""C:\Users:\User:\YourFolder\a.vbs"""

creation of file using vbscript

i did the following vbscript code to write a text file called "level.txt" in C:\Documents and Settings\All Users\Application Data\secon\generator
Const CommonAppData = &H23&
Const PATH = "\secon\generator"
Dim fso, MyFile
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(CommonAppData)
Set objFolderItem = objFolder.Self
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile(objFolderItem.Path & PATH & "\level.txt", True)
MyFile.Close
this is working fine in XP,but windows2008 server machine its not creating any body has idea how to do this.How
can i implement "SHGetFolderPath" for "CSIDL"
On Windows Server 2008, the location of AppData is normally c:\ProgramData. Try creating the c:\ProgramData\Secon\Generator folder first, and your script should run.
Also, try running the script using an administrator account, or changing the permissions of the generator folder to allow the user running the script to write to the directory if you still have problems

Resources