creating generic file in generic folder using QTP - vbscript

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"

Related

How do I set Dynamic File Paths in VBScript Specifically for FilesystemObject

I have spent several hours trying to get this to work. For some reason, my file paths register just fine in command prompt, but if I try to reference the file path using a FileSystemObject it doesn't work and tells me the file can't be found.
Dim g_shell
Set g_shell = CreateObject("WScript.Shell")
strCurDir = g_shell.CurrentDirectory
strValue = someName 'This is actually passed from a function argument
Set objFSO = CreateObject("Scripting.FileSystemObject")
configPath = strCurDir & "\maintLogs\" & strValue & "\MaintGuy_" & strValue & ".config"
Set objStream = objFSO.OpenTextFile(configPath) ' This throws a file not found error
g_shell.Run "cmd /c" & configPath & "& pause" 'This opens the file with no problem
'Let's Try something else:
Set objFSO = CreateObject("Scripting.FileSystemObject")
aPath = objFSO.BuildPath(g_shell.CurrentDirectory, "maintLogs")
bPath = objFSO.BuildPath(aPath, strValue)
cPath = objFSO.BuildPath(bPath, "MaintGuy_" & strValue & ".config")
Set objStream = objFSO.OpenTextFile(cPath) ' still doesn't work
g_shell.Run "cmd /c " & cPath & "& pause" ' This works (opens the file)
So my question is, why does this give me a file not found, when it works in command prompt and is an existing file. What am I missing?
In the example I am trying to read a text file, but I also could not get these file paths to work when I was trying to create a folder, move files into folders, and other file manipulation techniques. I accomplished it through launching the command prompt but could not get them to work using Scripting.FileSystemObject service.
Operating System: Windows 10
Scripting Language: VisualBasic Script
Application: SecureCRT
I figured it out. It turns out, because I was moving a file into the location I was trying to read from, it was trying to read the file BEFORE the file was there. I added a sleep function to the code, and it worked!

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 do I make a vbs script to move a certain file to the desktop if I click on it?

There is a folder in which I have a file, and a shortcut to that file.
I need a VBscript in the same folder which will move the shortcut to the desktop.
If there is any more information you require to help me, I will give it to you.
I tested it and it works for me...
Dim ObjFso
Dim SourceLocation
Dim DestinationLocation
Dim FileName
SourceLocation = "C:\REST-OF-PATH"
DestinationLocation = "%userprofile%\Desktop\"
FileName = "FILENAME"
Set ObjFso = CreateObject("Scripting.FileSystemObject")
ObjFso.MoveFile SourceLocation & "" & FileName, DestinationLocation & ""
Just copy all this...
Then replace REST-OF-PATHwith the rest of the path
And Replace FILENAME with the name of the file

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.

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

Resources