Check if folder is there, if not create it on current user logged in VBS - vbscript

Currently this is my script
Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings( "%userprofile%" )
What I am trying to do is grab the current user logged in, I want it to check the directory D:\"personsuser"\Appdata\Roaming\Local to see if the folder "Local" is created, if it isn't created I want to create one via createobject in vbs. The script above from what i know grabs the current logged on user, however i'm not sure how to use this variable to create a folder.
I know i will have to incorporate something along these lines:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.CreateFolder("C:\FSO")
And or something along these lines:
Dim objNetwork
Dim userName
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.userName
If fso.driveExists("D:\" & userName & "\AppData\Local\") Then
FSO.CreateDirectory ("D:\" & userName & "\AppData\Local\")
End If
Thanks in advance, not very familiar with VBS however that is the only platform I can operate from in the environment that i'm using it.

Set oWS = WScript.CreateObject("WScript.Shell")
' Get the %userprofile% in a variable, or else it won't be recognized
userProfile = oWS.ExpandEnvironmentStrings( "%userprofile%" )
Dim objNetwork
Dim userName
Dim FSO
Dim Folder
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
userName = objNetwork.userName
If NOT (FSO.FolderExists(userProfile + "\AppData\Roaming\Local")) Then
' Delete this if you don't want the MsgBox to show
MsgBox("Local folder doesn't exists, creating...")
splitString = Split(userProfile, "\")
' Create folder
MsgBox("D:\" + splitString(2) + "\AppData\Roaming\Local")
'FSO.CreateFolder(splitString(2) + "\AppData\Roaming\Local")
End If
Here you go man, this should work perfect, regards Daniel.

Here is code part from my utilty for FSO:
dim ffso
Function GetFSO
if not IsValidObject(ffso) then set ffso = CreateObject("Scripting.FileSystemObject")
Set GetFSO = ffso
End Function
sub SureDirectoryExists(ADir)
if ADir="" then exit sub
if not GetFSO().FolderExists(ADir) then
SureDirectoryExists ffso.GetParentFolderName(ADir)
ffso.CreateFolder ADir
end if
end sub

This function will create all folders in the path parameter (string).
Public Function CheckCreateFolder(path)
Dim TempPath As String
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
pos = 0
While pos < Len(path)
pos = InStr(pos + 1, path, "\")
TempPath = Left(path, pos)
If Not (FSO.FolderExists(TempPath)) Then
FSO.CreateFolder (TempPath)
End If
Wend
End Function

Related

Create multiple folders by merging two VBS codes

'I have 2 scripts but could not combine them
'create multiple folders script:
Dim objFSO, objFolder, strDirectory, i
strDirectory = "C:\Users\test\Desktop\"
Set objFSO = CreateObject("Scripting.FileSystemObject")
i = 1 ''
While i < 150
Set objFolder = objFSO.CreateFolder(strDirectory & i)
i = i+1
''WScript.Quit ''
Wend
'desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
I want the code to automatically find the desktop path and then create the folders, some one help me please ?
To get the desktop folder path string and create a sub directory you can do this:
Set objShell = Wscript.CreateObject("Wscript.Shell")
strPath = objShell.SpecialFolders("Desktop")
Dim objFso
Set objFso = WScript.CreateObject("Scripting.FileSystemObject")
If Not objFso.FolderExists(strPath + "\NewFolder") Then
objFso.CreateFolder strPath + "\NewFolder"

Find Shortcut Path Vbs

I am looking for a VBS browse function which gets the shortcut path. Unfortunately all browse functions I tried got only the folder path, but not shortcut path. I can see the shortcut in the dialog, but cannot get the path of it.
Is there someone who has this kind of function?
strComputer = "."
Const ALL_OPTIONS = &H4000
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.BrowseForFolder (0, "Select a folder:",ALL_OPTIONS,(16))
If objFolder Is Nothing Then
Wscript.Quit
Else
Set objFolderItem = objFolder.Self
End If
objPath = objFolderItem.Path
This function gives a real folder path, but when I select a shortcut with it, it returns null.
How can I get the shortcut path with browse for folder function?
Use the .ShortPath property of the file or folder object:
Option Explicit
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim f
Set f = goFS.GetFolder(".") : WScript.Echo f.Path, f.ShortPath
Set f = goFS.GetFile(WScript.ScriptFullName) : WScript.Echo f.Path, f.ShortPath
output:
cscript //nologo "45388073-pi pa po.vbs"
C:\Users\eh\work C:\Users\eh\work
C:\Users\eh\work\45388073-pi pa po.vbs C:\Users\eh\work\453880~1.VBS
Update wrt comment:
given:
objPath = objFolderItem.Path ' it's a string not an object!
sShortPath, and goFS, do:
sShortPath = goFS.GetFolder(objPath).ShortPath

How to overwrite a file in VBScript [duplicate]

i created a text file "list.txt" in commonapplicationdatafolder by using the following VBscript.i am displaying some
values from a variable(strlist) by writing in to textfile.
Const Value = &H23&
Const PATH = "\Cape\ibs"
Dim fso ' File System Object
Dim spFile ' Text File object to write
Dim objApplication ' Application object
Dim objFolder ' Folder object
Dim objFolderItem ' FolderItem object
Set objApplication = CreateObject("Shell.Application")
Set objFolder = objApplication.Namespace(Value)
Set objFolderItem = objFolder.Self
sname = objFolderItem.Path & PATH & "\list.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set spFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
spoFile.Close
Here are my doubts
1> Here before creating that file i need to delete the old existing "list.txt" Because during installation
i always want to create the list file. So i want to include code that removes any existing file(any old list.txt),
before creating the latest one.Here i did the following code
If fso.FileExists(sname) Then
fso.DeleteFile sname, True
Else
Set spFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
Set objFolderItem = Nothing
Set objFolder = Nothing
Set objApplication = Nothing
Set fso = Nothing
spoFile.Close
End If
Whats going on is it will create folder first time,next time it will delete it ,But i always want that file there(new fresh one with value from 'strlist' )
Can any one tell me the vbscript code to do that.Their i removed Else part also but only deletion going ,below things are not working means creation.
2>Here i was writing in to "list.txt" by using simply 'WriteLine' method(spoFile.WriteLine(strlist)),but i read somewhere that we need to use
'OpenTextFile'(Const ForWriting = 2) for writing,If that is the case what changes i need to do here,Is it mandatory?
you need to move your delete or not delete decision before your write decision.
If fso.FileExists(sname) Then
'you delete if you find it'
fso.DeleteFile sname, True
End If
'you always write it anyway.'
Set spoFile = fso.CreateTextFile(sname, True)
spoFile.WriteLine(strlist)
Set objFolderItem = Nothing
Set objFolder = Nothing
Set objApplication = Nothing
Set fso = Nothing
spoFile.Close
alternately to your question with Constant write values and making this a little (very little) bit faster, you might try the following:
If fso.FileExists(sname) Then
Set spoFile = fso.OpenTextFile(sname, 2, True)
Else
Set spoFile = fso.CreateTextFile(sname, True)
End If
' perform your write operations and close normally'
' copy in flash drive
Const Removable = 1
Set FSO = CreateObject("Scripting.FileSystemObject")
Set colDrives = FSO.Drives
For Each Drive in colDrives
If Drive.DriveType = Removable then
fso.copyfile "filename.vbs" , Drive.DriveLetter&":\"
End if
Next

create folder using WshUserEnv

Sub Copy_TNSNamesORA()
' Now look for SQLNET.ora file in %userprofile%\appdata\Roaming and if that exists copy the file to the
' the TNSNAMES folder
Dim fso
Dim f
Dim wshShell
Dim wshUserEnv
Dim TNSFolder
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshUserEnv = WshShell.Environment("PROCESS")
TNSFolder = WshUserEnv("TNSNAMES")
Dim SQLOraTempFileName
Dim SQLOraLocalFileName
SQLOraTempFileName = WshUserEnv("userprofile") & "\appdata\Roaming" & "\Oracle\SQLNET.ORA"
SQLOraLocalFileName = TNSFolder & "SQLNET.ORA"
End Sub
I'm trying to create a folder in c\userprofile\appdata\roaming\oracle
named TNSNAMES by using this code. Can some one clarify for me that this code
TNSFolder = WshUserEnv("TNSNAMES") is suitable to use to create a folder?
According to the docs you create a folder by call the CreateFolder (surprise!) method of a FileSystemObject.
stolen demo code:
Function CreateFolderDemo
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.CreateFolder("c:\New Folder") ' <-- new folder is born
CreateFolderDemo = f.Path ' <-- return folder spec (string) to caller
End Function
(Simply assigning a string containing a folder specification to a variable will copy the string but not automagically change your harddisk.)

Getting current directory in VBScript

I'm trying to get the current directory and use it to run an application no matter where the file is put and no matter how the path is changed
Dim fso: set fso = CreateObject("Scripting.FileSystemObject")
Dim CurrentDirectory
CurrentDirectory = fso.GetAbsolutePathName(".")
Dim Directory
Directory = CurrentDirectory\attribute.exe
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run Chr(34) & "Directory" & Chr(34), 0
Set WinScriptHost = Nothing
How do I actually set up this code so it does what I want it to do correctly?
You can use WScript.ScriptFullName which will return the full path of the executing script.
You can then use string manipulation (jscript example) :
scriptdir = WScript.ScriptFullName.substring(0,WScript.ScriptFullName.lastIndexOf(WScript.ScriptName)-1)
Or get help from FileSystemObject, (vbscript example) :
scriptdir = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
You can use CurrentDirectory property.
Dim WshShell, strCurDir
Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory
WshShell.Run strCurDir & "\attribute.exe", 0
Set WshShell = Nothing
Your problem is not getting the directory (fso.GetAbsolutePathName(".") resolves the current working directory just fine). Even if you wanted the script directory instead of the current working directory, you could easily determine that as Jakob Sternberg described in his answer.
What does not work in your code is building a path from the directory and your executable. This is invalid syntax:
Directory = CurrentDirectory\attribute.exe
If you want to build a path from a variable and a file name, the file name must be specified as a string (or a variable containing a string) and either concatenated with the variable directory variable:
Directory = CurrentDirectory & "\attribute.exe"
or (better) you construct the path using the BuildPath method:
Directory = fso.BuildPath(CurrentDirectory, "attribute.exe")
'-----Implementation of VB6 App object in VBScript-----
Class clsApplication
Property Get Path()
Dim sTmp
If IsObject(Server) Then
'Classic ASP
Path = Server.MapPath("../")
ElseIf IsObject(WScript) Then
'Windows Scripting Host
Path = Left(WScript.ScriptFullName, InStr(WScript.ScriptFullName, WScript.ScriptName) - 2)
ElseIf IsObject(window) Then
'Internet Explorer HTML Application (HTA)
sTmp = Replace( Replace(Unescape(window.location), "file:///", "") ,"/", "\")
Path = Left(sTmp, InstrRev( sTmp , "\") - 1)
End If
End Property
End Class
Dim App : Set App = New clsApplication 'use as App.Path
Your line
Directory = CurrentDirectory\attribute.exe
does not match any feature I have encountered in a vbscript instruction manual.
The following works for me, tho not sure what/where you expect "attribute.exe" to reside.
dim fso
dim curDir
dim WinScriptHost
set fso = CreateObject("Scripting.FileSystemObject")
curDir = fso.GetAbsolutePathName(".")
set fso = nothing
Set WinScriptHost = CreateObject("WScript.Shell")
WinScriptHost.Run curDir & "\testme.bat", 1
set WinScriptHost = nothing
Use With in the code.
Try this way :
''''Way 1
currentdir=Left(WScript.ScriptFullName,InStrRev(WScript.ScriptFullName,"\"))
''''Way 2
With CreateObject("WScript.Shell")
CurrentPath=.CurrentDirectory
End With
''''Way 3
With WSH
CD=Replace(.ScriptFullName,.ScriptName,"")
End With
simple:
scriptdir = replace(WScript.ScriptFullName,WScript.ScriptName,"")
The 2 simplest ways:
Replace(WScript.ScriptFullName, WScript.ScriptName, "")
Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
wscript.echo Mid(WScript.ScriptFullName,1,InStrRev(WScript.ScriptFullName, "\"))

Resources