List sub directories of a folder in APPDATA using Windows JScript - winapi

I'm trying to edit a json file which is located inside a folder in AppData\Roaming.
The file path is AppData\Roaming\Myapp\RANDOM_CRAP\settings.json
RANDOM_CRAP is just a random folder name which is different for every machine.
In order to open this file for writing, I first tried to get it's file path, like so:
function getAppData() {
var oShell = new ActiveXObject("WScript.Shell");
var strValue = oShell.RegRead("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\User Shell Folders\\AppData");
return strValue;
}
Problem is, the value stored under that registry key is %USERPROFILE%\AppData\Roaming which doesn't seem to open with:
var folder = fso.GetFolder(getAppData());
(Throws Path not found error)
Can I get to the APPDATA path in another way?

Getting to AppData path is easy with the ExpandEnvironmentStrings Method.
Party time:
var WshShell = WScript.CreateObject("WScript.Shell");
WScript.Echo("WinDir is " + WshShell.ExpandEnvironmentStrings("%AppData%"));
Good luck.

Related

Windows Script Host Javascript Get Path of .js file

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.

Move files from a special folder to another special folder

I'm writing a script to move an Outlook signature into %APPDATA%\Microsoft\Signatures\ and it's just not going as planned.
I want to instruct the user to put the "signature" folder on their desktop, and then run a script that will move all of the items in the signature folder to the AppData folder.
I've been reading, and it looks like it's not as simple as just putting %userprofile%\Desktop\signatures\* into VBScript or PowerShell code. I don't understand why Windows Explorer knows what to do with that path, but PowerShell/VBScript doesn't know what a special folder is, but whatever the case, my code just isn't working.
Here's an example of what I'm trying to do with VBScript:
Dim desktop
Dim appdata
desktop = object.SpecialFolders("Desktop")
appdata = object.SpecialFolders("APPDATA")
With CreateObject("Scripting.FileSystemObject")
.MoveFile desktop\MET_Signature_Template\*, appdata\Microsoft\Signatures\test\
End With
I get a syntax error, but no direction on why it's wrong. I've tried a few different things I've found on here to no avail.
When in doubt, read the documentation.
Syntax
object.SpecialFolders(objWshSpecialFolders)
Arguments
object
WshShell object.
Change this:
desktop = object.SpecialFolders("Desktop")
appdata = object.SpecialFolders("APPDATA")
into this:
Set sh = CreateObject("WScript.Shell")
desktop = sh.SpecialFolders("Desktop")
appdata = sh.SpecialFolders("APPDATA")
Build source and destination paths using the BuildPath method:
Set fso = CreateObject("Scripting.FileSystemObject")
source = fso.BuildPath(desktop, "MET_Signature_Template")
destination = fso.BuildPath(appdata, "Microsoft\Signatures\test")
fso.MoveFile source & "\*", destination & "\"
In PowerShell you'd do it like this:
$source = "$env:APPDATA\MET_Signature_Template"
$destination = Join-Path [Environment]::GetFolderPath('Desktop') 'Microsoft\Signatures\test'
Copy-Item "$source\*" $destination

how to access a network folder using vbscript

I have a folder which is on a network like \\server\contents\tasks and I want to access this folder.
I am getting a "path not found" exception. What am I doing wrong here:
Dim FolderPath
FolderPath = "\\server\contents\tasks"
set FSO = CreateObject("Scripting.FileSyatemObject")
FSO.GetFolder(FolderPath)
Thanks
Edit: I found this post which answers the same thing I am trying to achieve, but the issue is I am getting an error the network share is no longer available. What I have is a local folder as a shared folder and mapped as \\servername\contents\tasks but it gives me the above error.
Edit: I was pointing at the wrong folder.
Now I have another issue trying to open a text file in the network folder. It is able to create a folder at the network path but throwing error while reading a text file in the network folder. Is there something else that needs to be done?
Set FSO = CreateObject("Scripting.FileSystemObject")
strOutputPath = strOutput1 --this is a network path
Set txsOutput = FSO.CreateTextFile(strOutputPath)
Set f = FSO.OpenTextFile(strInput1)
Open the network folder using explorer.exe and pass the location of the folder as a parameter (in this example it's sPath storing the folder path)
Example:
sPath = "\\somedrive.somecompany.ie\software"
Set oShell = CreateObject("WScript.Shell")
oShell.Run "explorer /n," & sPath, 1, False
Terms and conditions: username and password privileges already setup for acccess to the network folder.

Copy folder contents to a created .zip file: 'file not found or no read permissions'

I'm trying to create a .zip file from an existing folder using JScript and it seems that my copyHere function is not copying to the .zip folder. Instead I get a popup box titled 'Compressed (zipped) Folder Error' with the message 'file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property (32).
Here is the script I'm using:
//Get commman line arguments
var objArgs = WScript.Arguments;
var zipPath = objArgs(0);
var sourcePath = objArgs(1);
//Create empty ZIP file and open for adding
var fso = new ActiveXObject("Scripting.FileSystemObject");
var file = fso.CreateTextFile(zipPath, true);
// Create twenty-two byte "fingerprint" for .zip
file.write("PK");
file.write(String.fromCharCode(5));
file.write(String.fromCharCode(6));
file.write('\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0');
var objShell = new ActiveXObject("shell.application");
var zipFolder = new Object;
zipFolder = objShell.NameSpace(zipPath);
sourceItems = objShell.NameSpace(sourcePath).items();
if (zipFolder != null)
{
zipFolder.CopyHere(sourceItems);
WScript.Sleep(1000);
}
Now the CopyHere function works for copying the contents of the sourcePath to a normal folder but when I try to create a .zip file and copy the contents to that, nothing happens. Any ideas on why copyHere is not copying the contents of the sourcePath to the .zip?
An Example for calling this script would be:
cscript win-zip.js C:\desired\zip\file.zip C:\path\to\source\folder
And the desired outcome would be that file.zip was created and now contains the contents of the source folder. Could this be a problem with permissions? What might cause this behavior?
Side Note, using a vbScript and the same commands I can successfully create and populate a .zip, so why doesn't it work using jscript!
Set objArgs = WScript.Arguments
ZipFile = objArgs(0)
SourceFolder = objArgs(1)
' Create empty ZIP file and open for adding
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set zip = CreateObject("Shell.Application").NameSpace(ZipFile)
' Get items in source folder
Set sourceItems = CreateObject("Shell.Application").NameSpace(SourceFolder).Items
' Add all files/directories to the .zip file
zip.CopyHere(sourceItems)
WScript.Sleep 1000 'Wait for items to be copied
Any helpful comments are greatly appreciated, thanks!
I encountered the same problem (file not found or no read permissions' even though I have read/write privileges on the file according to the value of my file.attributes property).
The problem disapeared as soon as I found and suppress a 0 length file somewhere in the directory to be copied with the copyhere method.
As Raymond said, the problem was that I had an open reference to the .zip folder in the file variable that I created (which had a lock on the folder so I could not copy any contents to it). The solution is to call
file.Close();
after writing to the file, that way we can access the file to copy contents to it :)

Is it any way to test from vbscript if a folder is a symbolic link?

I have some visual basic script code in my installer and i need to test that specific folder is a symbolic link or a normal folder. Is it any way to perform such task in vbscript?
File system items that are symbolic links have the FILE_ATTRIBUTE_REPARSE_POINT (1024) attribute set. You can check for this attribute like this:
Const FA_REPARSE_POINT = &h400
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("C:\MyFolder")
If (f.Attributes And FA_REPARSE_POINT) = FA_REPARSE_POINT Then
' The folder is a symbolic link
Else
' The folder is a normal folder
End If

Resources