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

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

Related

vbscript create text file in server directory

I have vbscript application that run in a server and I tried run that application in my local computer. The problem is I want to create a text file in server directory but I always end up with an error something like "disk is not ready". When I check, it is because in my local computer, there is only 1 partition Drive C: and for some reason the application try to make the textfile in my local directory. And if change the path to drive C: it works and the file is existed in my local directory. So what am I doing wrong? I want the text file is created in server directory.
Here's part of my code :
Dim fso, MyFile
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("D:\tesfolder\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
Set MyFile = Nothing
Set fso = Nothing
please help me
Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso.CreateTextFile("\\127.0.0.1\C$\SomeText.txt", True)
MyFile.WriteLine("This is a test.")
MyFile.Close
The above needs Admin because it uses the Admin only share (present on all computers) c$ and writes to the root of C: drive.
The format is
\\ServerName(orIP)\ShareName\Folder\File.ext
An admin would
\\Computer\D$\tesfolder\SomeText.txt
For a user substitute their share name for D$.

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

Not able to get absolute path from virtual machine if I put in Function from a Mapped Network Drive

Scenario:
I am running my VBScript in 'Test complete' which is a Automation Testing Tool from a Virtual Machine.
My scripts are residing in the Folder E:\TC from VM\TC (in physical Machine).
I have mapped my VM to this shared folder, so from VM my directory structure is: Z:\E\TC from VM\TC.
If I put the code here - then it displays the path correctly - but if I put this code in a function and call the function it displays a different path. WHY, and how I can rectify it?
Dim fso, ShowAbsolutePath, objFile, GetParentDir
Dim strFile : strFile = "..\..\Variables.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
ShowAbsolutePath = fso.GetAbsolutePathName(strFile)
Log.Message("The File is:"&ShowAbsolutePath)
GetParentDir = fso.GetParentFolderName(ShowAbsolutePath)
Log.Message("The parent directory of the File is:"&GetParentDir)
Set objFile = fso.GetFile(ShowAbsolutePath)
Output :
The File is:Z:\E\TC from VM\TC\Variables.txt
The parent directory of the File is:Z:\E\TC from VM\TC
Output (if I put this code in function and then call the function):
The File is:Z:\E\TC from VM\TC\MMSDemo\MMSDemo
The parent directory of the File is:Z:\E\TC from VM\TC\MMSDemo
Why the difference?
I am just passing to the function the variable strFile where strFile = "..\..\Variables.txt".
.GetAbsolutePathName() combines the current directory with the relative path given as parameter without regard to existence of files or folders:
>> WSCript.Echo goFS.GetAbsolutePathName("..\nosuchdir\nosuchfile.txt")
>>
E:\trials\SoTrials\answers\27749208\nosuchdir\nosuchfile.txt
So the given parameter must be part of the result. If you get
The File is:Z:\E\TC from VM\TC\MMSDemo\MMSDemo
The parent directory of the File is:Z:\E\TC from VM\TC\MMSDemo
then .GetAbsolutePathName() was not called with "....\Variables.txt", but with an empty value/string. Do you use "Option Explicit"?

List sub directories of a folder in APPDATA using Windows JScript

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.

Client-Side VBScript application, Incorrect Current Working Directory

I'm not understanding this behavior. Maybe someone can explain to me why my current working directory is not what I expect.
On my desktop, I have a folder called STKGui:
C:\Documents and Settings\Lauren\Desktop\STKGui
Located in that directory are the following files: gui.html, style.css, save.html, load.html Within STKGui there are also the following directories: Images, Scripts, and SaveData. Scripts contains various .vbs files, including gui.vbs.
I start with gui.html. I click a button which takes me to load.html. load.html uses scripts from Scripts\gui.vbs. One of the functions loads a database, and to do so I provide the location of the database: C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb Of course I want to use a relative file path instead of a fixed path. My initial attempt to load the database failed; it was trying to load from C:\Documents and Settings\Lauren\Desktop\SaveData\SaveData.accdb. So to troubleshoot I printed out the current working directory; much to my chagrin it was C:\Documents and Settings\Lauren\Desktop
I don't understand why my desktop is my current working directory. Shouldn't it be where the file is running from? I figured it would be either C:\Documents and Settings\Lauren\Desktop\STKGui (the location of load.html) OR C:\Documents and Settings\Lauren\Desktop\STKGui\Scripts (the location of gui.vbs which contains the function that's trying to load the database/printing debug messages of the current working directory).
Can someone explain why the current working directory is what it is, or better yet tell me how to get what I really want, which is the location of the files executing? (I don't care if it's the main STKGui folder or the scripts folder--as long as it's within the application's directory structure I can work with it!)
EDIT (7/14/10 4:02 pm EDT):
Various attempts at printing the current working directory or grabbing files based on what I -thought- was the relative path from my executing script have resulted in my desktop's path instead of the path of the executed script. I stumbled across this link: http://leereid.wordpress.com/2008/03/19/vbscript-current-directory-or-folder/ but none of the solutions are working for me, as I get run-time errors regarding the Wscript object. So while I don't know if any of the solutions on the aforementioned link will produce different results, if someone can help me get at least one of them working so I can find out that may be a step in the right direction.
One of the solutions, reproduced below:
Set oShell = CreateObject("WScript.Shell")
Set ofso = CreateObject("Scripting.FileSystemObject")
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
produces the following error:
Object required: 'Wscript' line: 659 char: 1
with line 659 being:
oShell.CurrentDirectory = ofso.GetParentFolderName(Wscript.ScriptFullName)
For Server-Side:
You should be using Server.MapPath() to get your "working directory". For instance, if you want to get the path to your database file in C:\Documents and Settings\Lauren\Desktop\STKGui\SaveData\SaveData.accdb, your app root being C:\Documents and Settings\Lauren\Desktop\STKGui, you would use Server.MapPath("SaveData\SaveData.accdb").
For Client-Side:
Upon closer examination and digging up some memories, I realized that MapPath is only available from the Server class. Instead, you need to create a file system object like this:
''get fs object
Set objFSO = CreateObject("Scripting.FileSystemObject")
''get actual file using path relative to calling vbs file
Set objFile = objFSO.GetFile("SaveData\SaveData.accdb")
''get path to the database
set sPathToDatabase = objFSO.GetAbsolutePathName(objFile)
In case it helps, here is a great resource for working with the file system in vbScript: http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/files/
This solution was NOT ideal, but what I ended up doing was parsing the url in my browser to get the directory.
guiPath = Mid(location.PathName, 2, len(location.PathName))
Set regExp = New RegExp
regExp.IgnoreCase = False
regExp.Global = True
regExp.Pattern = ".*/"
Set matchCollection = regExp.Execute(guiPath)
Set match = matchCollection(0)
guiPath = match.value
regExp.Pattern = "%20"
guiPath = regExp.Replace(guiPath, " ")
systemsDBPath = guiPath & "SaveData\SaveData.accdb"
Like I said, less than ideal. May not even work once I'm working with the application this will be running in. But I couldn't find a better way.

Resources