How to pass flag option in CopyHere [duplicate] - windows

I made a script, which is supposed to copy a bunch of fonts to the Windows font folder. When I run it, I receive the output of the file names I would like copied, but nothing copies. It works when I remove the For loop, and specify file names. Any help appreciated.
Const FONTS = &H14&
sFolder = "c:\FontInstalls\"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FONTS)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each oFile In objFSO.GetFolder(sFolder).Files
If UCase(objFSO.GetExtensionName(oFile.Name)) = "TTF" Then
objFolder.CopyHere(oFile)
wscript.echo(oFile)
End if
Next

Fonts need to be installed not copied. Copy the shell's objects rather than underlying files. The shell installs fonts copied into the fonts folder.
Here's the objects you need adding files to a zip.
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

Related

Moving all files except specific file to folder vbscript

I'm trying to move all my files to another folder using vbscript but somehow I can't seem to get it right. I've executed my code but the filename that I don't want also moves to the folder that I've created. Can you help me with this?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel
Files")
If objFolder.Name <> "TestResults.xlsx" Then
objFSO.Movefile "C:\Users\Users\Desktop\Other Files\Excel Files\*",
"C:\Users\Users\Desktop\Sample Folder"
End If
I've executed my code but the filename that I don't want also moves to the folder that I've created.
Its because you are moving all the files because you used *
There are few issues with your code like If objFolder.Name <> "TestResults.xlsx" even though your object refers to folders only.
Then you moved all the files - you gotta traverse the folder and filter out the files which are not to be moved
Try below code
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:\Users\Users\Desktop\Other Files\Excel Files").Files
For Each objFile In objFolder
If objFile.Name <> "TestResults.xlsx" Then
objFSO.MoveFile objFile.Path, "C:\Users\Users\Desktop\Sample Folder\"
End If
Next

Delete file within APPDATA folder

I have many PCs that currently have a Personal macro workbook installed. More specifically, they all have a shortcut to a Personal macro workbook on a network drive.
To install that, I went to each PC and ran this VBScript:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
oFSO.CopyFile "H:\Folder\Folder\Folder\PERSONAL 1.xlam - Shortcut.lnk", strAppData, True
Set objShell = Nothing
Set oFSO = Nothing
Now though, I want to remove that shortcut to PERSONAL 1.xlam from the XLSTART folder and copy over a shortcut to a different macro workbook.
This might be really easy, but I'm new to VBS and I haven't found a way to delete a file without having the the exact path. And since the path is going to be unique to each PC, I can't do that here.
You just need to modify one line from the above script. Try the following:
Option Explicit
Dim oFSO, strAppData, objShell
Set objShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
objShell.CurrentDirectory = oFSO.GetParentFolderName(WScript.ScriptFullName)
strAppData = objShell.ExpandEnvironmentStrings("%APPDATA%") & "\Microsoft\Excel\XLSTART\"
'here is the modified line
oFSO.DeleteFile strAppData & "PERSONAL 1.xlam - Shortcut.lnk", True
Set objShell = Nothing
Set oFSO = Nothing

VBS - Getting program files folder path?

I am tring to get program files folder in vbs. Tried this without luck;
SET wsc = CreateObject("WScript.Shell")
SET fso = WScript.CreateObject("Scripting.FileSystemObject")
targetpath = wsc.SpecialFolders("ProgramFiles") & "\Google\Chrome\Application\chrome.exe"
It just get the C:\ dir. What is the correct way to do it ?
This TechNet article shows the list of SpecialFolders. Program Files is not among them. This is a limitation of the Windows Script Host. In the same way that the following shows a blank popup
SET wsc = CreateObject("WScript.Shell")
msgbox wsc.SpecialFolders("Awesome")
So instead you have at least 2 options.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(&H26&)
Set objFolderItem = objFolder.Self
msgbox objFolderItem.Path
&H26& - Program Files
&H2A& - Program Files (x86)
The other option that I would offer is to use Environment variables like JosefZ suggests.
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles%") & "..."
targetpath = wsc.ExpandEnvironmentStrings("%ProgramFiles(x86)%") & "..."

CopyHere not working as expected when copying fonts

I made a script, which is supposed to copy a bunch of fonts to the Windows font folder. When I run it, I receive the output of the file names I would like copied, but nothing copies. It works when I remove the For loop, and specify file names. Any help appreciated.
Const FONTS = &H14&
sFolder = "c:\FontInstalls\"
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(FONTS)
Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each oFile In objFSO.GetFolder(sFolder).Files
If UCase(objFSO.GetExtensionName(oFile.Name)) = "TTF" Then
objFolder.CopyHere(oFile)
wscript.echo(oFile)
End if
Next
Fonts need to be installed not copied. Copy the shell's objects rather than underlying files. The shell installs fonts copied into the fonts folder.
Here's the objects you need adding files to a zip.
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(Ag(1))
Set DestFldr=objShell.NameSpace(Ag(0))
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Msgbox "Finished"

windows shell - how to detect the current location where script file is placed?

I am trying the windows shell file which will be inserted in the folder where it will analyze folders content.
Now i would like to know how can i detect which is the current path ? i.e. location where the vbs file is placed using FileSystemObject?
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo objFS.GetParentFolderName(WScript.ScriptFullName)
You can get that from WScript.ScriptFullName. Just remove the filename from the end (the bit after the last backslash). I normally use JScript for scripts, but IIRC VBScript has an InStrRev function that will help you find the last backslash. Or: Create a File object for the WScript.ScriptFullName path and then use its ParentFolder property. Something like (untested):
Dim objFSO
Dim objFile
Dim objFolder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(WScript.ScriptFullName)
Set objFolder = objFile.ParentFolder
To get the full path only without the extension I use Replace(WScript.ScriptFullName, WScript.ScriptName, "") to just result in a filepath

Resources