How to Create Folders and Subfolders in %AppData% - vbscript

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.

Related

Moving all files except specific file to folder vbscript

I'm trying to move all my files to another folder using vbscript but somehow I can't seem to get it right. I've executed my code but the filename that I don't want also moves to the folder that I've created. Can you help me with this?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel
Files")
If objFolder.Name <> "TestResults.xlsx" Then
objFSO.Movefile "C:\Users\Users\Desktop\Other Files\Excel Files\*",
"C:\Users\Users\Desktop\Sample Folder"
End If
I've executed my code but the filename that I don't want also moves to the folder that I've created.
Its because you are moving all the files because you used *
There are few issues with your code like If objFolder.Name <> "TestResults.xlsx" even though your object refers to folders only.
Then you moved all the files - you gotta traverse the folder and filter out the files which are not to be moved
Try below code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel Files").Files
For Each objFile In objFolder
If objFile.Name <> "TestResults.xlsx" Then
objFSO.MoveFile objFile.Path, "C:\Users\Users\Desktop\Sample Folder\"
End If
Next

How to delete temp files created in %temp% folder using 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)

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Changing the current directory of a FileSystemObject

When using a FileSystemObject you can reference the directory the script was run from by using the path ".". Is it possible to change what the FileSystemObject thinks is the current directory so you can use the "." path syntax for other directories?
Example:
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.GetFolder(".") ' Returns the current directory
Set f2 = fso.GetFolder(".\backup") ' Returns the "backup" directory under the current directory
As a simplified example, is there a method to call on fso so that the fso.GetFolder(".") call returns the backup directory instead?
You can change the current working folder for your script using the WshShell.CurrentDirectory property:
Dim oShell : Set oShell = CreateObject("WScript.Shell")
oShell.CurrentDirectory = ".\backup"
And here's a Hey, Scripting Guy! article on the subject: How Can I Change the Working Folder of a Script?
Not in general.
But why not find the current folder and store it:
Set fso = CreateObject("Scripting.FileSystemObject")
currentPath = fso.GetAbsolutePathName(".")

creating generic file in generic folder using QTP

I created a generic folder by taking the name from DataTable:
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
strdata = Datatable.Value("Column name", "Sheet")
fso.CreateFolder "C:\Documents and Settings\schoudar\Desktop\" & strdata
Till this I am successful but now I want to save a file in this folder and the folder name changes when I do no. of iterations. So I tried with CaptureBitmap file:
Browser("Browser").CaptureBitmap "C:\Documents and Settings\Desktop\ & strdata\filename.bmp"
But it is not working so please help me out.
What error are you getting?
Just a guess, fix the quotes and string appends:
Browser("Browser").CaptureBitmap "C:\Documents and Settings\Desktop\" & strdata & "\filename.bmp"

Resources