Windows Script Host Javascript Get Path of .js file - windows

I have been using QTTabBar for a while and am using .js scripts with it. The scripts are run using Windows Script Host, but I find myself having to specify hardcoded directories in the .js file instead of relative paths. This is not ideal.
In the .js file, is it possible to get the containing folder of the .js file (no matter what directory it is originally run from)? I just need to avoid specifying absolute paths somehow. For example, part of my .js file might look like this:
var qs = new ActiveXObject( "QTTabBarLib.Scripting" );
var fso = new ActiveXObject("Scripting.FileSystemObject");
var txtFile = fso.OpenTextFile("C:\\Installation\\Scripts\\QTTabBar\\dirs.txt", 1, false, 0);
var fText = txtFile.ReadAll();
I can't just put "dirs.txt" in the OpenTextFile function because when the .js script is run in QTTabBar, the working directory (I think) starts in system32 rather than at the .js file location. So I somehow need to get the path of the .js file itself and combine it with the relative name to create the absolute path. But I'm not sure if this is possible or how to do it.

You can get the path of current JScript file with
js_file_path = WScript.ScriptFullName;
and the absolute path to the text file is
path = WScript.ScriptFullName.split("\\").slice(0, -1).join("\\") + "\\dirs.txt";
No includes or imports are needed if the script is run in Windows Script Host.

Related

Delete all files except those with a specific extension in VBS [duplicate]

I'm making a project out of creating a script to use at work to automate one of our processes.
I'd like the script to check an input for username to search the specified user profile path for any files of .doc,.docx,.pdf,.pst ect. and copy them as is to a created folder on a network drive location.
My main question is what is the command or chain of commands to check folders and sub folders starting at the specified userpath, for JUST files with those extensions and I guess copy them but without getting to a situation where it just copies the same file over and over and over again. Sorry if that's confusing.
This answer provides sample code for recursively traversing a folder tree. A list of extensions could be handled by creating a dictionary:
Set extensions = CreateObject("Scripting.Dictionary")
extensions.CompareMode = vbTextCompare 'case-insensitive
extensions.Add "doc", True
extensions.Add "docx", True
extensions.Add "pdf", True
extensions.Add "pst", True
...
and then checking the extension of the processed files like this:
For Each f In fldr.Files
If extensions.Exists(objFso.GetExtensionName(f.Name)) Then
f.Copy targetFolder & "\"
End If
Next
The trailing backslash is required when the destination is a folder, otherwise you'd have to specify the full target path including the target filename.
I think I have understood most of the requirements, and this can be more easily achieved by using a .BAT file approach within windows. This batch (.Bat) file can run commands such as copy / delete etc.
So create a file called test.bat, and inside the file add the below script:
::XCOPY source [destination]
XCOPY "C:\Temp\*.doc" "C:\Temp\another"
What does this do? Well it uses an XCOPY Command to copy any files within the C:\Temp direcory which have a .doc extension. The files will be copied over to a folder called C:\Temp\another.
The XCOPY takes two primary arguments: source and destination. Source is where the file currently lives, and destination is where you want to copy the files to. More info of all of the options available can be found on:
http://support.microsoft.com/kb/240268
In order to run the file, just double click it, or schedule it to run whenever required.
Let me know if this meets your requirement, I didn't fully understand the bit about an input for a username?

To check whether a file has been downloaded or not via protractor

Need help to check whether a file has been successfully downloaded or not in a specified folder.
The file name dynamically changes every time a new file is downloaded but the initial part of the file "Pivot_Report" always stays the same
The actual file gets downloaded in the mentioned folder but protractor is not able to find it with just the initial part of the full name
This is the code I'm using (filenamePath is '/Users/Shubh/Documents/')
browser.driver.wait(function() {
        var fileName = filenamePath+"*.csv"
        var filesArray = glob.sync(fileName)
        if (typeof filesArray !== 'undefined' && filesArray.length > 0){
          return filesArray
        }
      }, 10000).then(function(filesArray) {
        var fileWithPath = filesArray[0]
        var temp = fileWithPath.indexOf("Pivot_Report")
        expect(fileWithPath.indexOf("Pivot_Report") >= 0).toBe(true,'Pivot Download is not succesfull')
        if(fs1.existsSync(fileWithPath)){
          fs1.unlinkSync(fileWithPath)
        }
      })
Getting the timeout error
It happens, because your path is incorrect.
I think easiest way is to use path.resolve() :
var path = require("path");
var filenamePath = path.resolve("Users/Shubh/Documents");
and then you will have (notice that you missed a / before *.csv)
var fileName = filenamePath+"/*.csv"

File Path When Including Nested Files in a Script

I have a script file which opens a text file located in the same directory. Let's call it SubScript.
SubScript.vbs
Function DoSomething(foo)
...
Dim Key
With CreateObject("Scripting.FileSystemObject")
Key = .OpenTextFile("key.txt", 1).ReadAll
End With
...
End Function
No problem here when the script is run on its own. However, I want to use the script above in another script file, "MainScript". The SubScript is located in a subfolder in the MainScript directory.
MainScript.vbs
With CreateObject("Scripting.FileSystemObject")
ExecuteGlobal .OpenTextFile(".\SubDir\SubScript.vbs", 1).ReadAll
End With
When I try and use the DoSomething function in the SubScript, I get a file not found error. I see what is happening, the subscript is trying to find the text file in the MainScript directory, where it doesn't exist.
Is there a way, without using an absolute file path, to make sure the SubScript loads the text file from the SubDir?
A relative path is resolved wrt the current directory of the process. Sometimes you can use the script's folder to get more flexibility. But in your case (.ExecuteGlobal), the SubScript's current directory is the current directory of the MainScript.
You should pass a path to DoSomething(), unless you can live with hardcoding ".\SubDir\key.txt".

VBS Script to locate all files of certain extensions and copy them to a specific destination

I'm making a project out of creating a script to use at work to automate one of our processes.
I'd like the script to check an input for username to search the specified user profile path for any files of .doc,.docx,.pdf,.pst ect. and copy them as is to a created folder on a network drive location.
My main question is what is the command or chain of commands to check folders and sub folders starting at the specified userpath, for JUST files with those extensions and I guess copy them but without getting to a situation where it just copies the same file over and over and over again. Sorry if that's confusing.
This answer provides sample code for recursively traversing a folder tree. A list of extensions could be handled by creating a dictionary:
Set extensions = CreateObject("Scripting.Dictionary")
extensions.CompareMode = vbTextCompare 'case-insensitive
extensions.Add "doc", True
extensions.Add "docx", True
extensions.Add "pdf", True
extensions.Add "pst", True
...
and then checking the extension of the processed files like this:
For Each f In fldr.Files
If extensions.Exists(objFso.GetExtensionName(f.Name)) Then
f.Copy targetFolder & "\"
End If
Next
The trailing backslash is required when the destination is a folder, otherwise you'd have to specify the full target path including the target filename.
I think I have understood most of the requirements, and this can be more easily achieved by using a .BAT file approach within windows. This batch (.Bat) file can run commands such as copy / delete etc.
So create a file called test.bat, and inside the file add the below script:
::XCOPY source [destination]
XCOPY "C:\Temp\*.doc" "C:\Temp\another"
What does this do? Well it uses an XCOPY Command to copy any files within the C:\Temp direcory which have a .doc extension. The files will be copied over to a folder called C:\Temp\another.
The XCOPY takes two primary arguments: source and destination. Source is where the file currently lives, and destination is where you want to copy the files to. More info of all of the options available can be found on:
http://support.microsoft.com/kb/240268
In order to run the file, just double click it, or schedule it to run whenever required.
Let me know if this meets your requirement, I didn't fully understand the bit about an input for a username?

How do I specify a relative local file path in DOS? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I dynamically specify a file in DOS?
I am using c#.NET 2.0 to execute DOS commands to ftp a file. All works except for 1 thing, in the cmd file I call, it runs a PUT statement. Right now the put statement has a hardcoded local file path. I need to specify a dynamic path. I've tried
put %~dp0\myfile.DTL myfile.dtl
but it says it can't find the file.
Right now the .NET code calls a BAT file which only exist to call the CMD file. Interestingly, the BAT file DOES successfully use a relative path in its call to the CMD file:
ftp.exe -s:%~dp0\oit.cmd
However, I can't get that relative path to wrok in the cmd file:
open <my host>
<user name>
<password>
put <hardcoded path that needs to be relative path>localfilename remotefilename
I'll bever know where it will exist so I just need to grab whatever local directorey the file is in.
Relative is "." (dot).
Can you post the exact situation? What directory are you in, and where is the file?
Be careful with the "~" char ... it has a special meaning for DOS files (8.3 notation)
Use System.IO.Directory.GetCurrentDirectory() to get the current (working) folder.
Alternatively, if you want the path relative to your .EXE file, you can use System.Reflection.Assembly.GetEntryAssembly().CodeBase to get full path to your .EXE file, then use System.IO.Path.GetDirectoryName() to get the directory name of that path.
After that, you can use System.IO.Path.Combine to get absolute path out of relative:
string absPath = Path.Combine( #"c:\working\folder", #"sub\folder\file.ext" );
// absPath == "c:\working\folder\sub\folder\file.ext
// Works with double-dot too:
string absPath2 = Path.Combine( #"c:\working\folder", #"..\up\file.ext" );
// absPath == "c:\working\up\file.ext

Resources