How to write a function to combine folder path and file name? - vbscript

I would like to pass the full path of a text file to one of the function.
i am placing the my script, and text file at same location
by using the below command i found the folder path where my script is
p = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
p came as C:\test
my file name is xyz.txt
i want to pass the the argument to the function as C:\test\xyz.txt
how can i combine path and file name
i tried below code
path = p & "xyz.txt"
can any one help me how can join the path and file name.

You can use string concatenation to build a path. The correct way to do it, however, is to use the FileSystemObject's BuildPath() method, because this will do the right thing with the backslashes under all circumstances.
Set FSO = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
textFilePath = FSO.BuildPath(scriptPath, "xyz.txt")
MsgBox textFilePath

Try like this code :
Option Explicit
Msgbox GetFilePath("xyz.txt")
'******************************************************
Function GetFilePath(FileName)
Dim fso,scriptPath
Set fso = CreateObject("Scripting.FileSystemObject")
scriptPath = FSO.GetParentFolderName(WScript.ScriptFullName)
GetFilePath = FSO.BuildPath(scriptPath,FileName)
End Function
'******************************************************

Related

Find the owners of all the folders within a given path

I am trying to find the owners of all the folders in a given path. I have the following code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFolder In objFSO.GetFolder("C:\Windows").SubFolders
strpath = objFolder.Path
WScript.Echo strpath
Next
My end goal is to put the path and and owner of all the folders from the given path into a text file.
Could someone help me find the owner of a folder and be able to place this owner name into a variable. I could then use this to improve my existing code.
As JoSerra mentioned in the comments you can retrieve the owner of a file or folder via the WMI class Win32_LogicalFileSecuritySetting. The sample code from the Script Center is mostly accurate. I would, however, recommend using double quotes instead of single quotes around the path.
Single quotes (unlike double quotes) are valid characters in a path. If you invoke the statement wmi.Get("Win32_LogicalFileSecuritySetting.Path='" & path & "'") with a path containing a single quote, the call will fail with an "invalid object path" error. Thus it's better to use double quotes and escape backslashes in the path.
path = "C:\some\folder 'with' quotes"
Function Esc(str)
Esc = Replace(str, "\", "\\")
End Function
Set wmi = GetObject("winmgmts:")
Set fs = wmi.Get("Win32_LogicalFileSecuritySetting=""" & Esc(path) & """")
rc = fs.GetSecurityDescriptor(sd)
If rc = 0 Then
WScript.Echo "Owner: " & sd.Owner.Domain & "\" & sd.Owner.Name
Else
WScript.Echo "Couldn't retrieve security descriptor."
End If

VBScript to copy file/s beginning with XXX or YYY or ZZZ from directory A to directory B

I have next to zero knowledge on vbs scripting but I have managed to cobble a few together to copy files from one directory to another and delete files in a directory but I've not been able to find anything specifically what I'm now after.
I'm looking to write a vbs script to do the following - copy file/s beginning with XXX or YYY or ZZZ from directory A to directory B.
I've had a look around and cannot quite find what I'm looking for, they all seem far too complex for what I need and involve the latest date or parsing a string within the files etc.
I'm quite sure this is simple but as stated at the top I really do not know what I'm doing so any help would be greatly appreciated.
The following is what I have for copying all files from one directory to another with a progress bar so a amendment to this would be great.
Const FOF_CREATEPROGRESSDLG = &H0&
' copy test 1 to test 2
strTargetFolder = "C:\test2\"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(strTargetFolder)
objFolder.CopyHere "C:\test1\*.*", FOF_CREATEPROGRESSDLG
Not sure as of yet how to get this in one big progress indicator. Currently it will show progress for each individual file.
Const FOF_CREATEPROGRESSDLG = &H0&
strSourceFolder = "C:\test1\"
strTargetFolder = "C:\test2\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(strSourceFolder)
Set objFiles = objSourceFolder.Files
Set objShell = CreateObject("Shell.Application")
Set objTargetFolder = objShell.NameSpace(strTargetFolder)
For Each objSingleFile in objFiles
If (InStr(1,objSingleFile.Name,"xxx",vbTextCompare) = 1) Or _
(InStr(1,objSingleFile.Name,"yyy",vbTextCompare) = 1) Or _
(InStr(1,objSingleFile.Name,"zzz",vbTextCompare) = 1) Then
' The file name starts with one the flagged keywords
objTargetFolder.CopyHere objSingleFile.Path, FOF_CREATEPROGRESSDLG
End If
Next
Keep your strTargetFolder code which is used for the actual copy procedure used at the end of the script. Using the FileSystemObject objFSO we cycle through all the files of the directory c:\test1. Each file name is then checked to see if it starts with either of 3 different strings. The comparison is done using vbTextCompare which essentially has it running case insensitive. If a match is found then, using your original code, copy the file to the target directory with progress.
Currently this is not going to recursively navigate all subfolders for file but you could make a recursive function for that.
Use the FileSystemObject in combination with a regular expression:
src = "C:\test1"
dst = "C:\test2"
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Pattern = "^(XXX|YYY|ZZZ)"
For Each f In fso.GetFolder(src).Files
If re.Test(f.Name) Then f.Copy dst & "\"
Next

vbscript bad file name when using variable from text file

I want to create a vbscript program that reads a .ini file to get parameters and use it.
My parameter file contains several parameters:
propertyfile.ini
"C:\PROGRA~1\narrowcast\scripts\transferStatusLog.txt"
..other parameters
..more parameters
my vbscript will read the file and use it
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objPropertyFile = objFSO.OpenTextFile("C:\PROGRA~1\scripts\propertyfile.ini", 1)
Do Until objPropertyFile.AtEndOfStream
myfile = objPropertyFile.ReadLine
... other parameters for use on other fso object
Loop
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogProgram = objFSO.CreateTextFile(myfile)
Im having an error bad file name or number. Please help
You are reading in the quotes - vbscript is not liking that. You'll need to strip them off.
Here's a quick and dirty replacement. Don't forget to add a check to see if it exists - otherwise if it doesn't you'll probably get a path not found error.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objPropertyFile = objFSO.OpenTextFile("C:\PROGRA~1\scripts\propertyfile.ini", 1)
Do Until objPropertyFile.AtEndOfStream
myfile = replace (objPropertyFile.ReadLine,"""","")
Loop
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogProgram = objFSO.CreateTextFile(myfile)
What are we doing here?
We are simply telling vbscript to replace the double quote character with nothing (we had to "escape it, by double quoting, etc):
myfile = replace (objPropertyFile.ReadLine,"""","")
I hope this helps.

Replacing SubString values with the Replace function

The code below looks in the test folder for any files that have not been accessed in over 5 days, if it finds one it assigns mRoot the file path and then whats NOT WORKING is using the Replace method to look inside the mRoot string for the IP and replace it with the new one, I have it show me what mRoot looks like in a pop up just to make sure it changes(or doesn't). I can't seem to get the IP to change. Can anyone help out? I'm very new to VBS so I'm hoping this is obvious (whether it is doable or not). Thanks.
Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")
sRoot = "\\192.168.1.104\test\"
today = Date
Set aFolder = oFileSys.GetFolder(sRoot)
Set aFiles = aFolder.Files
For Each file in aFiles
FileAccessed = FormatDateTime(file.DateLastAccessed, "2")
If DateDiff("d", FileAccessed, today) > 5 Then
Set objShell = Wscript.CreateObject("Wscript.Shell")
mRoot = file
Call Replace(mRoot,"\\192.168.1.104","\\192.168.1.105")
objShell.Popup mRoot,, "My Popup Dialogue box"
'oFileSys.MoveFile file, mRoot
End If
Next
Try mRoot = Replace(mRoot,"\\192.168.1.104","\\192.168.1.105")

Copy and Rename File VBScript

I need to move a file with a name based off of a date to another folder.
The file structure is:
Source: \\network_location\folder\Filename_09-11-2012.txt
Destination: C:\Dump\Filename.txt
The source file is always 1 day behind. I am looking to rename the file while copying it.
The code I am trying to use is:
Sub Copy_And_Rename()
Name "\\network_location\folder\Filename_"+Month(Now())+"-"+Day(Now()-1)+"-"+Year(Now())+".txt" As "C:\Dump\Filename.txt"
End Sub
You can copy and rename a file with the FileSystemObject like this:
Set objFSO = CreateObject("Scripting.FileSystemObject")
' First parameter: original location\file
' Second parameter: new location\file
objFSO.CopyFile "C:\Test\folder1\name1.txt", "C:\Test\folder2\name2.txt"
Code to copy and rename file
sourceFilePath = "C:\filePath\source.xlsx"
destinationFilePath = "C:\filePath\destination.xlsx"
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.CopyFile sourceFilePath, destinationFilePath

Resources