I won't to set objFolder path, but I can't get current username
objShell.NameSpace(C:\users\%CurrentUser%\AppData)
Code:
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace("C:\users\%USERNAME%\AppData\Roaming\Microsoft\Windows")
Or can I replace objShell.NameSpace("C:\somepath") different method?
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace("C:\users\%USERNAME%\AppData\Roaming\Microsoft\Windows")
If you are going to use Shell.Namespace you might want to read the documentation.
It says:
vDir [in]
Type: Variant
The folder for which to create the Folder object. This can be a string
that specifies the path of the folder or one of the
ShellSpecialFolderConstants values. Note that the constant names found
in ShellSpecialFolderConstants are available in Visual Basic, but not
in VBScript or JScript. In those cases, the numeric values must be
used in their place.
when you follow the link for ShellSpecialFolderConstants
You'll find
ssfSTARTMENU 0x0b (11). File system directory that contains Start menu
items. A typical path is C:\Users\username\AppData\Roaming\Microsoft\Windows\Start Menu.
Which is I one level deeper than what you are looking for. So from that folder you can just go up one level and you have the folder you need.
Something like this should work:
option explicit
const ssfSTARTMENU = 11
sub main
msgbox getWindowsFolder().Self.Path
end sub
function getWindowsFolder()
dim shell
Set shell = CreateObject("Shell.Application")
dim startMenuFolder
set startMenuFolder = shell.Namespace(ssfSTARTMENU)
set getWindowsFolder = startMenuFolder.ParentFolder
end function
main
Related
I'm coding a new VBS script, which should create a TXT file in the Documents directory. While with other inputs I used %USERNAME% for the User Name of the PC, the command "CreateTextFile" seems to want the real directory, without including any variables like %USERNAME%.
I'm kinda of new to this so I can't figure it out.
That's what I tried:
Dim objFS, objFile
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.CreateTextFile("C:\Users\%USERNAME%\Documents\Demo.txt", true)
objFile.WriteLine("some sample text")
It should understand %USERNAME% as the UserName of the PC, but doesn't.
What I get as result is the program saying he can't find the directory C:\Users\%USERNAME%\Documents.
To get the desktop one uses
SpecialFolders Property
Returns a SpecialFolders object (a collection of special folders).
object.SpecialFolders(objWshSpecialFolders)
Arguments
object WshShell object.
objWshSpecialFolders The name of the special folder. (eg Desktop)
Environment strings are not expanded. You can pass an entire string containing environmental variables. EG "%userprofile%\Desktop". This code lists all variables available https://pastebin.com/rrEyVxFd.
ExpandEnvironmentStrings Method
Returns an environment variable's expanded value.
object.ExpandEnvironmentStrings(strString)
Arguments
object WshShell object.
strString String value indicating the name of the environment variable
you want to expand.
There are plenty longer drawn-out, which are appropriate but these are a couple examples - much easier:
Option Explicit
Msgbox CreateObject("WScript.Network").UserName
' or into a variable
Dim oShell : set oShell = CreateObject("WScript.Network")
Dim UserName : UserName = oShell.UserName
Msgbox UserName
Set oShell = Nothing
Hello I've tried a lot of researching but cant find what I need and haven't been able to successfully piece this together myself.
Each of my users have a XML file within their profile that I would like to edit. The file contains a reference to their computer name and clientname, which are out of date each time they login to a new terminal. I need to replace these with the current computername and clientname. The bit I cannot figure out how to do is how to search the XML for the computername when I only know the first few characters, then replace it.
my XML will have any entry something like this
"InstalledPrinter name="\WHBCVDI0109\LabelPrinter650 (from IGEL-00E0C533943E)"
I need to search the file and replace the WHBCVDI0109 and the IGEL-00E0C533943E with the correct entries. My script successfully gets those entries I just dont know how to find and replace them in the file.
My script looks like this:
Const ForReading = 1
Const ForWriting = 2
Set oShell = CreateObject( "WScript.Shell" )
'Get Variables
user=oShell.ExpandEnvironmentStrings("%UserName%")
appdata=oShell.ExpandEnvironmentStrings("%appdata%")
strComputerName = oshell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
'Set XML location
strfile = appdata & "\Smart Label Printer\SlpUserConfig.xml"
'Open
Set objfso = CreateObject("Scripting.FileSystemObject")
Set filetxt = objfso.OpenTextFile(strfile, ForWriting)
strTemp = "HKEY_LOCAL_MACHINE\SOFTWARE\Citrix\ICA\Session\ClientName"
WScript.Echo "client name is : " & oShell.RegRead(strTemp)
An pointers would be much appreciated.
You shouldn't use the FileSystemObject and String/RegExp operations to edit an XML file. Using the canonical tool - msxml2.domdocument - is less error prone and scales much better.
See here for an example (edit text); or here for another one (edit attribute).
If you publish (the relevant parts of) your .XML file, I'm willing to add same demo code specific for your use case here.
So im writing a script that drops a file folder then moves that file to the folder it dropped it self. Well the folder drops fine but the file wont move. Can some see whats wrong with my code? Or give me a better way to move the file. I also get no error message about trying to move the file.
Dim folder,fso,filsys,C
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Set wshshell = CreateObject("wscript.shell")
Set filesys = CreateObject("scripting.filesystemobject")
Set objfso = CreateObject("Scripting.filesystemObject")
Set c = fso.GetFile(Wscript.scriptFullname)
On Error Resume NEXT
Set objFolder = objFSO.CreateFolder("C:\55egr932ntn7mk23n124kv1053bmoss5")
If Err.Number<>0 Then
End If
WScript.Sleep 3000
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
And I have a program I use that turns the VBS into and EXE so you see the "file.exe" which really is the .VBS itself
I'm not familiar with this syntax, but the line below looks like it's expecting the folder variable to be a string.
C.Move ("C:\552ntn7mk23n124kv1053bmoss5\File.exe") (folder&"\File.exe")
Earlier in code it looks as though you're setting folder as an object.
Set folder = fso.GetSpecialFolder(1)
You might not get the error you mentioned in your comment if you convert folder to a string.
~~
Another thing to try is the following code:
Set fso = CreateObject("Scripting.filesystemObject")
Set folder = fso.GetSpecialFolder(1)
Alert (folder&"\File.exe")
(I'm not sure if it's "Alert" or "Msgbox" or something else.) That test will show you whether the file path makes sense. If you get an error on line 3 of that test, try converting folder to a string before your Alert (or Msgbox).
I have the following script which works great if I need to rename files in a folder but now I want to move and rename folders from one mapped drive to another mapped drive. Can someone help me to modify the script to do so? I am failry new to VB so pardon if I can't firgure this out but it took me a little while to figure this one out and now I am not sure how to modify this script. Thank you in advance!
The folders by default are labeled A.1234, A.5678, and so on and will always have a different number assigned to them. I will keep the numbers in the label as they are PO numbers. So my end result desired is Ack~1234, Ack~5678, and so on.
Dim fso, f, f1, fc, s Set
fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move f1.ParentFolder & "\" & replace(f1.Name, "A.", "Ack~")
Again these folders exist on the root of a mapped drive and need to move to another mapped drive with the new names. If more info is needed please do not hesitate to ask.
UPDATE
I modified the script below to give an idea of what I'm looking to do.
Dim fso, objFol
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Z:\")
Set objFolders = objFol.Folders
For each folder in objFolders
fso.Movefolder folder, "Y:\" & Replace(fso.Name, "A.", "Ack~")
Next
This give me an error stating it does not support "Folder". There will be any number of folders in the Z drive and I need to move them all to the Y drive. Sorry if I didn't explain properly in the previous post.
The move command is taking a string, for which you're providing the parent folder of the file, so the file isn't moving. I think it'll be sufficient to provide a different folder location. For example:
Dim fso, f, f1, fc, s
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder("Y:\Test")
Set fc = f.Files
For Each f1 in fc
f1.move "Z:\TargetFolder\" & replace(f1.Name, "A.", "Ack~")
Note: I've not tested this, but if there are problems then just ask
Additional Stuff after update:
The following code will allow you to move folders, and do the replacements you need to the folder name string.
Dim fso, objFol, objMoveFol, strPathBuild
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFol = fso.GetFolder("Y:\Test")
For Each objMoveFol In objFol.SubFolders
'Replace the root folder locations in the path
strPathBuild = Replace(objMoveFol, "Y:\Test", "Z:\TargetFolder\")
'Do the required other fiddle
strPathBuild = Replace(strPathBuild, "A.", "Ack~")
fso.Movefolder objMoveFol, strPathBuild
Next
Make sure you're really careful with this sort of thing, as getting is wrong can be fairly spectacular.
I want to search a file from the path.
For Example,
Path = "C:\Newfolder\"
file name = *.txt, *.fin
I want to get all the *.txt, *.fin file from the new folder.
Use the Scripting.FileSystemObject.
Call it with GetFolder("C:\Newfolder"), then loop through the files in that folder with the
.Files property and filter them on extensions using the GetExtensionName method.
For example:
Dim fso as Object: Set fso = CreateObject("Scripting.FileSystemObject")
Dim f as Object
For Each f in fso.GetFolder("<folderpath>").Files
If fso.GetExtensionName(f.Path) = "txt" Then 'or maybe it's .txt, I'm not sure
' also test for 'fin'
'... do stuff
End If
Next f
Check out the Scripting.FileSystemObject.
In your project add a reference to the "Microsoft Scripting Runtime".
Then you can do something like this:
Dim fso As Scripting.FileSystemObject
Set fso = New Scripting.FileSystemObject
Dim fld As Scripting.Folder
Set fld = fso.GetFolder("d:\temp\newfolder")
Dim i As Integer
Dim ext As String
Dim fl As Scripting.File
For Each fl In fld.files
'get extension
ext = Mid(fl.Name, Len(fl.Name) - 2)
If ext = "txt" Or ext = "fin" Then
'do something with the file
End If
Next fl
This is one of the areas that got so much better with .NET.
Just drop-in a CDirDrill class that does it all for you, in full native VB6. Another excellent solution from Karl Peterson :)
The FileSystemObject is not recommended for a couple of reasons: one, it adds a dependency, and two, it depends on scripting, which may be disabled per policy. I've had bugs because some customer has managed to muck up scrrun.dll on their PC. Eliminate dependencies unless they're really helping you a lot.
BTW this question is a duplicate of this