I can't find anything to replace %~dp0 in my VBScript
On error resume next
Set shell= createobject("WSCRIPT.SHELL")
Shell.run "%~dp0test.bat", vbhide
Set fso = CreateObject("Scripting.FileSystemObject")
GetTheParent = fso.GetParentFolderName(Wscript.ScriptFullName)
Taken from here:
how to get working drive and directory?
To use the obtained value in your script, add a reference to the variable where you need its value. For instance:
Shell.run GetTheParent & "\test.bat", vbhide
To get the folder the executing .VBS resides in:
type dp0.vbs
Option Explicit
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo "WScript.ScriptFullName", WScript.ScriptFullName
WScript.Echo "oFS.GetParentFolderName(WScript.ScriptFullName)", oFS.GetParentFolderName(WScript.ScriptFullName
)
cscript dp0.vbs
WScript.ScriptFullName E:\trials\SoTrials\answers\tools\jscript\ijs\dp0.vbs
oFS.GetParentFolderName(WScript.ScriptFullName) E:\trials\SoTrials\answers\tools\jscript\ijs
This should work.
set objShell = Createobject("wscript.shell")
strPath = Left(WScript.ScriptFullName, Len(WScript.ScriptFullName) - Len(WScript.ScriptName))
Shell.run strPath & "test.bat", vbhide
Related
This is so far what I have. Don't know how to do it.
Option Explicit
Dim fso,WshShell,strName
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")
strName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )
If fso.FileExists("C:\Users\strName\Downloads\run.vbs") then
fso.MoveFile "C:\Users\strName\Downloads\run.vbs" , "C:\Users\strName\Desktop"
Else
wscript.echo "doesn't exist"
End If
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"
I'm trying to write this code in VBs, (I'm quite new)
I want to have the code go through a folder/directory, and pick out all ".txt" files, in something like a for loop. These text files could then be listed using MsgBox ("txt filename"). This is what I've got so far:
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt" Then
Wscript.Echo objFile.Name
Next
It doesn't seem to be picking out a txt file called "name.txt". Any help would be greatly appreciated.
ps:
please ignore and bad spelling and my terrible formatting (im new to stackoverflow) Thanks! (also the code above is mostly mashed together code i found off the internet)
You should modify this line ; if you use UCase :
If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt"
To
If UCase(objFSO.GetExtensionName(objFile)) = "TXT"
Or if you use LCase
If LCase(objFSO.GetExtensionName(objFile)) = "txt"
And your code looks like this one :
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
If LCase(objFSO.GetExtensionName(objFile)) = "txt" Then
Wscript.Echo objFile.Name
End If
Next
GetExtensionName Method
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
I Want To Get The Current Folder Path From
where the vbs file is running,and store it into a variable
then pass the variable to a batch file.
My VBS is:
dim fso: set fso = CreateObject("Scripting.FileSystemObject")
CD = fso.GetAbsolutePathName(".")
dim WshShell
set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & CD
My BAT Code is:
#echo off
echo get path is='%1'
pause
exit
The VbS is working but problem Is if the folder is like
"c:\Program Files\" the batch file echo only "C:\Program"
How Can I Get The Full Path that passed from the vbs.
I think you need to add a:
CD = """" & CD & """"
Which will help the string, when interpreted by the batch file, be interpreted as a whole. (You will be passing "C:\Program Files\" rather than C:\Program Files\ which will be interpreted as a.bat "C:\Program" "Files\")
For example:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
CD = """" & fso.GetAbsolutePathName(".") & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD
or:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
CD = """" & CD & """"
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
'WshShell.run "a.bat " & CD
or as Ekkehard.Horner and MC ND have pointed out:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & """" & CD & """"
As MC ND has used, you could substitute """" for char(34) if you think it is easier to read.
To further improve readability as Ekkehard.Horner has suggested, you could create a function:
Function qq(s)
qq = """" & s & """"
End Function
And use as:
'Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")
'CD = fso.GetAbsolutePathName(".")
'Dim WshShell
'Set WshShell=Wscript.CreateObject("Wscript.shell")
WshShell.run "a.bat " & qq(CD)
You need to (double) quote the path
WshShell.run "a.bat " & """" & CD & """"
Otherwise each space separated part of CD will be passed as one argument.
You could use a function like
Function qq(s)
qq = """" & s & """"
End Function
to avoid the noise in more complex concatenations.
WshShell.run "a.bat " & qq(CD)
WshShell.run "a.bat " & Chr(34) & CD & Chr(34)
WshShell.run "a.bat """ & CD & """"
A string with a space in it, when passed to a batch file as argument, is received as two strings. So, it is necessary to enclose it in quotes to pass it as only one argument.
Then in batch file you will get the path with the quotes. So from batch file
echo %1
will show the quoted path and
echo %~1
will show the path without quotes