Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have directory with executable files like
tool-7.0.20.exe
tool-7.0.23.exe
tool-7.0.24.exe
tool-7.0.25.exe
tool-7.0.26.exe
and I'm using a for loop in a batch file to get the latest version in a variable and run it
I need to convert this for loop to a vbs script
for /f "tokens=*" %%a in ('dir /ON /B %~dps0tool*.exe') do set latest=%%a
then run the latest file variable with objShell.Exec or objShell.Run
Thanks
You can do it using Shell.Application ActiveX:
Option Explicit
Const SHCONTF_NONFOLDERS = &H40
Const SHCONTF_INCLUDEHIDDEN = &H80
Dim strCurDir, strPath, objWshShell
strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
With CreateObject("Shell.Application").Namespace(strCurDir).Items
.Filter SHCONTF_NONFOLDERS + SHCONTF_INCLUDEHIDDEN, "tool-*.exe"
strPath = .Item(.Count - 1).Path
End With
WScript.Echo strPath
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run strPath
Or by retrieving files with Scripting.FileSystemObject and filtering them with VBScript.RegExp:
Option Explicit
Dim strFiles, objMatches, strPath, objWshShell
With CreateObject("Scripting.FileSystemObject")
With .GetFolder(.GetParentFolderName(WScript.ScriptFullName))
strFiles = ""
For Each strPath In .Files
strFiles = strFiles & strPath & vbCrLf
Next
End With
End With
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.IgnoreCase = True
.Pattern = "^.+?\\tool-.*?\.exe$"
Set objMatches = .Execute(strFiles)
strPath = objMatches(objMatches.Count - 1).Value
End With
WScript.Echo strPath
Set objWshShell = CreateObject("WScript.Shell")
objWshShell.Run strPath
Or even running your cmd code (cmd instructions were slightly modified to be executed in one liner command line mode):
Option Explicit
Dim objWshShell, strCurDir, strCmd, strRes
Set objWshShell = CreateObject("WScript.Shell")
strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
strCmd = "%comspec% /v /c (for /f %a in ('dir /ON /B /S " & strCurDir & "tool-*.exe') do #set latest=%a)&echo !latest!"
strRes = objWshShell.Exec(strCmd).StdOut.ReadAll()
strRes = Replace(strRes, vbCrLf, "")
WScript.Echo strRes
objWshShell.Run strRes
If you want to get rid of the flashing console window, the above code may be modified as follows to launch second instance of the script in hidden mode, and execute cmd instructions within it:
Option Explicit
Dim objWshShell, strCurDir, strCmd, strRes, objWnd, objParent, strSignature
Set objWshShell = CreateObject("WScript.Shell")
If WScript.Arguments.Named.Exists("signature") Then WshShellExecCmd
strCurDir = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\"))
strCmd = "%comspec% /v /c (for /f %a in ('dir /ON /B /S " & strCurDir & "tool-*.exe') do #set latest=%a)&echo !latest!"
RunCScriptHidden
strRes = Replace(strRes, vbCrLf, "")
WScript.Echo strRes
objWshShell.Run strRes
Sub RunCScriptHidden()
strSignature = Left(CreateObject("Scriptlet.TypeLib").Guid, 38)
GetObject("new:{C08AFD90-F2A1-11D1-8455-00A0C91F3880}").putProperty strSignature, Me
objWshShell.Run ("""" & Replace(LCase(WScript.FullName), "wscript", "cscript") & """ //nologo """ & WScript.ScriptFullName & """ ""/signature:" & strSignature & """"), 0, True
End Sub
Sub WshShellExecCmd()
For Each objWnd In CreateObject("Shell.Application").Windows
If IsObject(objWnd.getProperty(WScript.Arguments.Named("signature"))) Then Exit For
Next
Set objParent = objWnd.getProperty(WScript.Arguments.Named("signature"))
objWnd.Quit
objParent.strRes = objWshShell.Exec(objParent.strCmd).StdOut.ReadAll()
WScript.Quit
End Sub
Related
This question already has an answer here:
How to get %username% in VBScript?
(1 answer)
Closed 3 years ago.
I have this script (courtesy of #WesternSage) that renames foo.txt to foo.bat, launches foo.bat, and when foo.bat ends, renames it back to foo.txt.
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "foo.txt", "foo.bat"
SCRIPT = "foo.bat"
Set objShell = CreateObject("WScript.Shell")
strPath = WScript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
Set objshell = createobject("wscript.shell")
objshell.Run "%COMSPEC% /c " & NewPath, 1, True
' Changes start here
'===================================================================
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Hold execution until cmd.exe process is done
Do
' Get cmd.exe processes
Set colProcessList = objWMIService.ExecQuery _
("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
WScript.Sleep 250
Loop While colProcessList.Count > 0
Fso.MoveFile "foo.bat", "foo.txt"
The problem is:
foo.txt (foo.bat) is in a path, which can change depending on the Windows version. For this I need to use environment variables to set the foo.txt path (example: %homedrive%), but this change doesn't work.
SCRIPT = "%homedrive%\test\foo.bat"
I need to call a second batch (bar.bat), when the first one ends (foo.bat). But this change does not work at the end of .vbs.
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
Fso.MoveFile "%homedrive%\test\bar.txt", "%homedrive%\test\bar.bat"
SCRIPT = "%homedrive%\test\bar.bat"
Set objShell = CreateObject("WScript.Shell")
strPath = WScript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run "%COMSPEC% /c " & NewPath, 1, True
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
' Hold execution until cmd.exe process is done
Do
' Get cmd.exe processes
Set colProcessList = objWMIService.ExecQuery _
("SELECT Name FROM Win32_Process WHERE Name LIKE 'cmd.exe'")
WScript.Sleep 250
Loop While colProcessList.Count > 0
Fso.MoveFile "%homedrive%\test\bar.bat", "%homedrive%\test\bar.txt"
This should works:
Dim Fso
Set Fso = WScript.CreateObject("Scripting.FileSystemObject")
set objshell = createobject("wscript.shell")
homedrive = objshell.ExpandEnvironmentStrings( "%HOMEDRIVE%" )
Fso.MoveFile homedrive & "\test\bar.txt", homedrive & "\test\bar.bat"
SCRIPT = homedrive & "\test\bar.bat"
strPath = Wscript.ScriptFullName
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)
NewPath = objFSO.BuildPath(strFolder, SCRIPT)
set objshell = createobject("wscript.shell")
objshell.Run (script),1,True
Fso.MoveFile homedrive & "\test\bar.bat", homedrive & "\test\bar.txt"
this is my problem:
on a Windows 2003 server I've a folder (c:\test), and every day an application put 3 new files on it.
1° file:
31201610181207000100000000630001
31201610181213000100000000440001
31201610181227000100000000630001
....
2° file:
31201610181214000100000000380002
31201610181234000100000009830002
31201610181344000100000000380002
...
3° file:
31201610181826000100000000580003
31201610190722000100000000580003
31201610191801000100000000580003
...
My goal is to replace ONLY the last 4 characters on each file with a .bat or .vbs script (0001 --> 0031) (0002 --> 0032) (0003 --> 0033).
I've done a .vbs file who works, but it search on all string and not on the last 4 characters.
Option Explicit
Dim objFSO, strFolder, objFolder, objFile
Dim strOldValue1, strNewValue1, strNewValue2, strOldValue2, strNewValue3,
strOldValue3, objRead, strContents, objWrite
Const ForReading = 1
Const ForWriting = 2
strFolder = "c:\test"
strOldValue1 = "0001"
strNewValue1 = "0031"
strOldValue2 = "0002"
strNewValue2 = "0032"
strOldValue3 = "0003"
strNewValue3 = "0033"
' I take the folder
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
' I count the file on the folder
For Each objFile In objFolder.Files
' Read file with textstream object.
Set objRead = objFSO.OpenTextFile(objFile.Path, ForReading)
' Trap error if file is empty or cannot read.
On Error Resume Next
strContents = objRead.readall
If (Err.Number <> 0) Then
On Error GoTo 0
Wscript.Echo "Cannot read: " & objFile.Path
strContents = ""
End If
On Error GoTo 0
objRead.Close
' check what's is inside the folder
If (InStr(strContents, strOldValue1) > 0) Then
strContents = Replace(strContents, strOldValue1, strNewValue1)
Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
If (InStr(strContents, strOldValue2) > 0) Then
strContents = Replace(strContents, strOldValue2, strNewValue2)
Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
If (InStr(strContents, strOldValue3) > 0) Then
strContents = Replace(strContents, strOldValue3, strNewValue3)
Set objWrite = objFSO.OpenTextFile(objFile.Path, ForWriting)
objWrite.Write strContents
objWrite.Close
End If
next
Thanks for any help!!
Here is a short batch script, which immediately modifies all files C:\test\*.* accordingly:
for %%F in ("C:\test\*.*") do (
for /F "delims=" %%L in ('type "%%~F" ^& ^> "%%~F" rem/') do (
set "LINE=%%L"
setlocal EnableDelayedExpansion
set "LEFT=!LINE:~,-4!"
set "RIGHT=!LINE:~-4!"
if "!RIGHT!"=="0001" set "RIGHT=0031"
if "!RIGHT!"=="0002" set "RIGHT=0032"
if "!RIGHT!"=="0003" set "RIGHT=0033"
>> "%%~F" echo(!LEFT!!RIGHT!
endlocal
)
)
Using JREPL.BAT - a regular expression find/replace utility
for %%F in (c:\test\*) do call jrepl "000(?=[123]$)" "003" /f "%%F" /o -
The above looks at the end of each line for "000" before a "1", "2", or "3", and substitutes "003" for the "000".
JREPL is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - No 3rd party exe file required.
Thank you very much!!!! it works!!
Also, if you're interested I've found how the make my script work:
I've to add the & VBCrlf to the variable, in this way the script will search for the value + the new line.
strOldValue1 = "0001" & VBCrlf
strNewValue1 = "0031" & VBCrlf
strOldValue2 = "0002" & VBCrlf
strNewValue2 = "0032" & VBCrlf
strOldValue3 = "0003" & VBCrlf
strNewValue3 = "0033" & VBCrlf
I am trying to write a windows batch script that will run all the time and when a usb flash drive will be inserted it will copy files from usb to computer.
I've found a lot of script that do different parts of it but none of them works as I want.
Can sombody help me ?
I posted before a vbscript here to do what you want just take a look and try it !
Vbscript to copy files with specific extension from usb when plugged in
Edit on 19/07/2016 #10:42 :
I improved this vbsript to run as admin, and to let executing just one insctance of this script.
AutoSave_USB_SDCARD.vbs to copy into My Documents folder
Option Explicit
' Run as Admin
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
' To let executing just one insctance of this script
If AppPrevInstance() Then
MsgBox "There is an existing proceeding !" & VbCrLF &_
CommandLineLike(WScript.ScriptName),VbExclamation,"There is an existing proceeding !"
WScript.Quit
Else
Do
Call AutoSave_USB_SDCARD()
Pause(30)
Loop
End If
'**************************************************************************
Function AppPrevInstance()
With GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
With .ExecQuery("SELECT * FROM Win32_Process WHERE CommandLine LIKE "_
& CommandLineLike(WScript.ScriptFullName) & _
" AND CommandLine LIKE '%WScript%' OR CommandLine LIKE '%cscript%'")
AppPrevInstance = (.Count > 1)
End With
End With
End Function
'**************************************************************************
Function CommandLineLike(ProcessPath)
ProcessPath = Replace(ProcessPath, "\", "\\")
CommandLineLike = "'%" & ProcessPath & "%'"
End Function
'*************************AutoSave_USB_SDCARD()****************************
Sub AutoSave_USB_SDCARD()
Dim Ws,WshNetwork,NomMachine,MyDoc,strComputer,objWMIService,objDisk,colDisks
Dim fso,Drive,NumSerie,volume,cible,Amovible,Dossier,chemin,Command,Result
Set Ws = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")
NomMachine = WshNetwork.ComputerName
MyDoc = Ws.SpecialFolders("Mydocuments")
cible = MyDoc & "\"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
("SELECT * FROM Win32_LogicalDisk")
For Each objDisk in colDisks
If objDisk.DriveType = 2 Then
Set fso = CreateObject("Scripting.FileSystemObject")
For Each Drive In fso.Drives
If Drive.IsReady Then
If Drive.DriveType = 1 Then
NumSerie=fso.Drives(Drive + "\").SerialNumber
Amovible=fso.Drives(Drive + "\")
Numserie=ABS(INT(Numserie))
volume=fso.Drives(Drive + "\").VolumeName
Dossier=NomMachine & "_" & volume &"_"& NumSerie
chemin=cible & Dossier
Command = "cmd /c Xcopy.exe " & Amovible &" "& chemin &" /I /D /Y /S /J /C"
Result = Ws.Run(Command,0,True)
end if
End If
Next
End If
Next
End Sub
'**************************End of AutoSave_USB_SDCARD()*******************
Sub Pause(Sec)
Wscript.Sleep(Sec*1000)
End Sub
'************************************************************************
This waits for the volumes to change, then copies the USB to c:\test. Lots of message boxes so you can see what's happening. Remove them for production.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2")
Set evtDevice = objWMIService.ExecNotificationQuery ("SELECT * FROM Win32_VolumeChangeEvent")
Wscript.Echo "Waiting for events ..."
Do
Set objReceivedEvent = evtDevice.NextEvent
'report an event
Wscript.Echo " Win32_Device Changed event occurred" & VBNewLine
If objReceivedEvent.EventType = 1 Then
Wscript.Echo "Type = Config Changed"
ElseIf objReceivedEvent.EventType = 2 Then
Wscript.Echo "Type = Device Arrived"
Set colItems = objWMIService.ExecQuery("Select * From Win32_Volume")
For Each objItem in colItems
Wscript.Echo objitem.DriveType
If objitem.DriveType = 2 then
Wscript.Echo objItem.DriveType & " " & objItem.Name & " " & objItem.driveletter
Wscript.Echo "Starting Copying"
Set objShell = CreateObject("Shell.Application")
Set Ag=Wscript.Arguments
set WshShell = WScript.CreateObject("WScript.Shell")
Set SrcFldr=objShell.NameSpace(objitem.driveletter)
Set DestFldr=objShell.NameSpace("c:\test\")
Set FldrItems=SrcFldr.Items
DestFldr.CopyHere FldrItems, &H214
Wscript.Echo "Finished Copying"
End If
Next
ElseIf objReceivedEvent.EventType = 3 Then
Wscript.Echo "Type = Device Left"
ElseIf objReceivedEvent.EventType = 4 Then
Wscript.Echo "Type = Computer Docked"
End If
Loop
Try this:
#echo off
set backupcmd=xcopy /s /c /d /e /h /i /r /y /
%backupcmd% "%USERPROFILE%\Pictures" "%drive%\all\My pics"
%backupcmd% "%USERPROFILE%\Favorites" "%drive%\all\Favorites"
%backupcmd% "%USERPROFILE%\Videos" "%drive%\all\Vids"
%backupcmd% "%USERPROFILE%\Documents" "%drive%\all\Docs"
%backupcmd% "%USERPROFILE%\OneDrive" "%drive%\all\Onedrive"
%backupcmd% "%USERPROFILE%\Desktop" "%drive%\all\Desktop"
%backupcmd% "%USERPROFILE%\Network" "%drive%\all\Other devices"
I want to remove the Google Chrome pinned icon on the taskbar. The uninstall does NOT remove the icon. I modified code to remove just the Google Chrome.lnk. What I want to do (knowing about VBS) is to loop through all the user folders not just the current user which is I believe defined as strCurrentUserAppData. The other desire I would like to do with this code is to use it with SCCM to do a clean install of Chrome. I installed the x64 version and need to replace it with the x86 version. When I do the uninstall using the enterprise MSI it leaves the pinned icon. If I use a bat to remove the icon from the directory, the lnk is deleted but there is left a white paper icon on the task bar. So far this is the only code that works on removing the pinned icon.
Option Explicit
Const CSIDL_APPDATA = &H1A
Dim objShell
Dim objFolder
Dim objFolderItem
Dim objVerb
Dim objCurrentUserAppData
Dim strCurrentUserAppData
Set objShell = CreateObject("Shell.Application")
Set objCurrentUserAppData = objShell.NameSpace(CSIDL_APPDATA)
strCurrentUserAppData = objCurrentUserAppData.Self.Path
'===================''==================='
' - Remove All Pinned Items -
'===================''==================='
Set objFolder = objShell.Namespace(strCurrentUserAppData & "\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar")
For Each objFolderItem in objFolder.Items
'WScript.Echo objFolderItem
If objFolderItem = "Google Chrome" then
For Each objVerb in objFolderItem.Verbs
If Replace(objVerb.name, "&", "") = "Unpin from Taskbar" Then objVerb.DoIt
Next
End if
Next
The BATCH Code I have run still will not remove the icon even after rebooting.
taskkill /im chrome.exe /f /t
taskkill /im GoogleUpdate.exe /f /t
taskkill /im GoogleCrashHandler.exe /f /t
taskkill /im GoogleCrashHandler64.exe /f /t
taskkill /im GoogleUpdateBroker.exe /f /t
taskkill /im GoogleUpdateHelper.msi /f /t
taskkill /im GoogleUpdateOnDemand.exe /f /t
taskkill /im GoogleUpdateSetup.exe /f /t
taskkill /im chrmstp.exe /f /t
MsiExec.exe /X{3EDA268B-C905-37D1-89DF-7049B39FB069} /q/n
MsiExec.exe /X{6A21C1E8-DAC1-3C18-BCDC-2DBB4B352AD8} /q/n
rem app files
rd "%userprofile%\AppData\Local\Google" /s/q
rd "C:\Users\Default\AppData\Local\Google" /s/q
rd "\Google" /s/q
rd "%PROGRAMFILES%\Google" /s/q
rd "%PROGRAMFILES(X86)%\Google" /s/q
rem desktop shorcuts
del "%PUBLIC%\Desktop\Google Chrome.lnk" /q
del "%userprofile%\Desktop\Google Chrome.lnk" /q
rem start menu folders
rd "%PROGRAMDATA%\Microsoft\Windows\Start Menu\Programs\Google Chrome" /s/q
rd "%userprofile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Google Chrome" /s/q
rem pinned items
del "%userprofile%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\Google Chrome*.lnk" /q
del "%userprofile%\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar\Google Chrome*.lnk" /q
taskkill /f /im explorer.exe
start explorer.exe
Try this hybrid code (Vbscript/PowerShell):
Option Explicit
Dim Title,Ws,ByPassPSFile,AppPath,Example,PSFile,MyCmd,Result,MyArray,MyApp,FolderPath,fso
Title = "UnPin application from Taskbar on Windows 7 by Hackoo"
Set Ws = CreateObject("wscript.Shell")
Set fso = Createobject("Scripting.FileSystemObject")
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
ByPassPSFile = "cmd /k PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Example = "C:\Program Files\Google\Chrome\Application\Chrome.exe "
AppPath = InputBox("Enter the path of your application in order to unpin it from the taskbar " & vbcr & "Example : " & vbcr & Dblquote(Example) & "",Title,Example)
If AppPath = "" or IsEmpty(AppPath) Then Wscript.Quit()
MyArray = Split(AppPath,"\")
MyApp = MyArray(UBound(MyArray))
FolderPath = fso.GetParentFolderName(AppPath)
MyCmd = "$sa = new-object -c shell.application" & VbCrlF
MyCmd = MyCmd & "$FolderPath = "& DblQuote(FolderPath) & VbCrlF
MyCmd = MyCmd & "$pn = $sa.namespace($FolderPath).parsename('"& MyApp &"')" & VbCrlF
MyCmd = MyCmd & "$pn.invokeverb('taskbarunpin')"
Call WriteMyPSFile(MyCmd)
Result = Ws.run(ByPassPSFile & PSFile,1,True)
'**********************************************************************************************
Sub WriteMyPSFile(strText)
Dim fs,ts,PSFile
Const ForWriting = 2
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
ts.WriteLine strText
ts.Close
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
EDIT : 22/06/2015 : UnPinfromTaskBarHiddenConsole.vbs
Option Explicit
Dim Title,Ws,ByPassPSFile,AppPath,Example,PSFile,MyCmd,Result,MyArray,MyApp,FolderPath,fso
Title = "UnPin application from Taskbar on Windows 7 by Hackoo"
Set Ws = CreateObject("wscript.Shell")
Set fso = Createobject("Scripting.FileSystemObject")
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Example = "C:\Program Files\Google\Chrome\Application\Chrome.exe "
AppPath = InputBox("Enter the path of your application in order to unpin it from the taskbar " & vbcr & "Example : " & vbcr & Dblquote(Example) & "",Title,Example)
If AppPath = "" or IsEmpty(AppPath) Then Wscript.Quit()
MyArray = Split(AppPath,"\")
MyApp = MyArray(UBound(MyArray))
FolderPath = fso.GetParentFolderName(AppPath)
MyCmd = "$sa = new-object -c shell.application" & VbCrlF
MyCmd = MyCmd & "$FolderPath = "& DblQuote(FolderPath) & VbCrlF
MyCmd = MyCmd & "$pn = $sa.namespace($FolderPath).parsename('"& MyApp &"')" & VbCrlF
MyCmd = MyCmd & "$pn.invokeverb('taskbarunpin')"
Call WriteMyPSFile(MyCmd)
Result = Ws.run(ByPassPSFile & PSFile,0,True)
MsgBox "The Unpin of " & DblQuote(MyApp) & " from the taskbar is done !",VbInformation,Title
'**********************************************************************************************
Sub WriteMyPSFile(strText)
Dim fs,ts,PSFile
Const ForWriting = 2
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fs = CreateObject("Scripting.FileSystemObject")
Set ts = fs.OpenTextFile(PSFile,ForWriting,True)
ts.WriteLine strText
ts.Close
End Sub
'**********************************************************************************************
Function DblQuote(Str)
DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************************************************************
I tried to do it this way:
Dim objFSO, outFile, wshShell
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set outFile = objFSO.CreateTextFile("paint.bat", True)
outFile.WriteLine "taskkill /f /im mspaint.exe"
Set wshShell = CreateObject("WScript.Shell")
wshShell.Run "paint.bat", 0, false
that was to work but of an error saying "The file is already being used by another process"
The file is already being used by your own cscript or wscript process. You should use outFile.Close (and maybe moreover Set outFile = Nothing) before run.
Terminates paint directly in vbscript.
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * From Win32_Process")
For Each objItem in colItems
If objitem.Name = "mspaint.exe" Then
msgbox objitem.name & " PID=" & objItem.ProcessID & " SessionID=" & objitem.sessionid
objitem.terminate
End If
Next