rename a file using VBScript, launch file, wait, and rename again - vbscript

I need to create a vbs script (for maintenance purposes) that renames foo.txt to a foo.bat and launch foo.bat and when foo.bat ends, rename foo.bat again to foo.txt
This is my script vbs:
On Error Resume next
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 NewPath, vbHide, true
Fso.MoveFile "foo.bat", "foo.txt"
On Error GoTo 0
the script executes well. Rename foo.txt to foo.bat. Launches foo.bat, but does not expect foo.bat to end and renames it to foo.txt.
I changed this line, nothing happens:
objshell.Run NewPath, vbHide, 1, true
What do I need or what did I do wrong?
Alternative Solution (no VBScript): (By suggestion of #KenWhite)
code:
On Error Resume next
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 NewPath, true
On Error GoTo 0
And at the end of foo.bat:
ren foo.bat foo.txt
exit
Thanks

Here is a possible solution just in case anyone is wondering how to solve this problem without resorting to the alternate proposal mentioned above.
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"

Related

.vbs GhostScript folder loop PDF to JPEG [duplicate]

This question already has answers here:
Run Command Line & Command From VBS
(2 answers)
Closed 10 months ago.
how can I transform this innocent cmd batch code file to work in a ".vbs" file?
#echo off
setlocal
for %%I in (*.pdf) do (
md "%%~nI"
"C:\GS\gswin32c.exe" -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=100 -r600 -sOutputFile="%%~nI\p%%02d.jpeg" "%%~I")
I got this far, I don't know how to write the GS script parameters
dim sFolder,MyArray
dim FSO,OutPutFile
dim networkInfo
Dim objShell
sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork")
Set objShell = WScript.CreateObject( "WScript.Shell" )
For Each oFile In oFSO.GetFolder(sFolder).Files
If Instr( 1, oFile.Name, ".pdf", vbTextCompare )>0 Then
dirname = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
on error resume next
oFSO.CreateFolder dirname
on error goto 0
objShell.Run "C:\GS\gswin32c.exe" '/////// here help!
Set objShell = Nothing
End if
Next
Set oFSO = Nothing
well, I finished the code, works as intended
In the end, I generated a ".bat" and run it because if processed as an individual "objShell.Run" it opened for every pdf a separate cmd window, which was not acceptable.
thanks all for the hints. :)
dim sFolder,MyArray
dim FSO,OutPutFile
dim networkInfo
Dim objShell
dim commmand,commmand2
dim ffso, crit
Dim intAnswer
dim nn
sFolder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(WScript.ScriptFullName)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set networkInfo = CreateObject("WScript.NetWork")
Set objShell = WScript.CreateObject("WScript.Shell")
Set ffso = CreateObject("Scripting.FileSystemObject")
tmpfile=ffso.getspecialfolder(2) & "\" & ffso.gettempname
tmpfile=tmpfile & ".bat"
set ffso=createobject("scripting.filesystemobject")
set ots=ffso.opentextfile(tmpfile,2,true)
ots.writeline "#echo off"
ots.writeline "setlocal"
intAnswer = _
Msgbox("Procesare doar cu Rutare?" & vbNewLine & "Yes - doar cele cu #"& vbNewLine & "No - toate fisierele", _
vbYesNo, "Selectie ")
If intAnswer = vbYes Then
crit = "#.pdf"
Else
crit = ".pdf"
End If
nn = 0
For Each oFile In oFSO.GetFolder(sFolder).Files
If Instr( 1, oFile.Name, crit, vbTextCompare )>0 Then
nn=nn+1
dirname = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
on error resume next
oFSO.CreateFolder dirname
on error goto 0
if Instr( 1, oFile.Name, "#.pdf", vbTextCompare )>0 then
MyArray = Split(oFile.Name, "#", -1, 1)
rutare = MyArray(UBound(MyArray)-1)
else
rutare = Replace(oFile.Name,"." & oFSO.GetExtensionName(oFile.Path),"")
end if
Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutPutFile = FSO.OpenTextFile( sFolder & "\" & dirname & "\" & rutare & ".txt" ,8 , True)
OutPutFile.WriteLine(FormatDateTime(Now, vbGeneralDate) & vbtab & networkInfo.UserName & vbtab & networkInfo.ComputerName)
OutPutFile.close
Set FSO= Nothing
commmand = "C:\GS\gswin32c.exe -dNOPAUSE -dBATCH -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dJPEGQ=100 -r600 -sOutputFile=" & chr(34) & sFolder & "\" & dirname & "\p%%02d.jpeg" & chr(34) & " " & chr(34) & sFolder & "\" & oFile.Name & chr(34)
ots.writeline "#echo -----------------------------------------------------------------------------"
ots.writeline "#echo Procesare - " & oFile.Name
ots.writeline "#echo -----------------------------------------------------------------------------"
ots.writeline commmand
End if
Next
if nn=0 then
ots.writeline "#echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
ots.writeline "#echo Nu exista PDF uri sau cu extensia " & crit
ots.writeline "#echo Pentru toate PDF-urile se selecteaza No!"
ots.writeline "#echo xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
end if
ots.writeline "timeout 6"
objShell.run "%comspec% /c " & tmpfile,1, True
ots.close
ffso.deletefile tmpfile,true
set ffso=nothing
Set oFSO = Nothing

Clarification of VB Script Program to Zip Files Error Message

Below is a small VB script that I believe should zip files in a folder.
I am new to VB script.
Set objArgs = WScript.Arguments
InputFolder = objArgs(0)
ZipFile = objArgs(1)
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 2000
I found the script here. Batch file script to zip files
I run the script with the following
CScript zipt.vbs ..\AFolder AFolder.zip
I get the following error:
zipIt.vbs(6, 1) Microsoft VBScript runtime error: Object required
: 'objShell.NameSpace(...)'
..\AFolder is not empty. The zip file is created and is empty.
Debugging the script, the error is on this line.
Set source = objShell.NameSpace(InputFolder).Items
What does the error message mean?
Based on this question, try this:
Set objArgs = WScript.Arguments
Set fso = CreateObject("Scripting.FileSystemObject")
InputFolder = fso.GetAbsolutePathName(WScript.Arguments.Item(0))
ZipFile = fso.GetAbsolutePathName(objArgs(1))
CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" & Chr(5) & Chr(6) & String(18, vbNullChar)
Set objShell = CreateObject("Shell.Application")
Set source = objShell.NameSpace(InputFolder).Items
objShell.NameSpace(ZipFile).CopyHere(source)
wScript.Sleep 2000
To clarify, the script essentially didn't know what "AFolder.zip" was so it needs the entire, absolute path.

Create a folder on desktop via Vbscript

I have 2 scripts but could not combine them
Create a folder script
Option Explicit
Dim objNetwork, objComputer
Dim objFSO, objFSOText, objFolder, objFile
Dim strDirectory, strFile, MakeObject
strDirectory = "Folder"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strDirectory) Then
Wscript.Echo strDirectory & " already exists"
Else
'Below is the added line
Set objFolder = objFSO.CreateFolder(strDirectory)
Wscript.Echo "The folder " & strDirectory & " has just been created"
End if
Wscript.Quit
desktop path script
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
Con some one help me please ?
Use .BuildPath to combine the Desktop folder path and the intended (sub)folder(name):
>> Set objFSO = CreateObject("Scripting.FileSystemObject")
>> Set WshShell = WScript.CreateObject("WScript.Shell")
>> strDirectory = "Folder"
>> strDirectory = objFSO.BuildPath(WshShell.SpecialFolders("Desktop"), strDirectory)
>> WScript.Echo strDirectory
>>
C:\Documents and Settings\eh\Desktop\Folder

vbs run program with objShell.run

I have the following vbscript that fail with an error on last line
Option Explicit
Dim objShell
Dim strComputer, strCmd , strVar
strComputer = "."
Set objShell = CreateObject("WScript.Shell")
' strVar = objShell.ExpandEnvironmentStrings("%ProgramFiles(x86)%")
' strCmd = strVar & "\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
' wscript.echo strCmd
objShell.Run "taskkill /im Autobites.exe",,True
WScript.Sleep(5000)
objShell.run """%ProgramFiles(x86)%""" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"
The problem is that script open the folder %ProgramFiles(x86)% but don't run the exe autobites.exe
Can you help me to debug it
The whole file spec has to be quoted, not only the first part:
objShell.run """%ProgramFiles(x86)%" &"\CaptureBites\Express\Programs\AutoBites\Autobites.exe"""

How to compare the result of cmd in the vbscript?

I ask about getting the result of this cmd command netstat -a | find /c "TCP"
and compare it with specific value using VBSCRIPT
thanks
It's difficult to answer question with so less details, but lets try anyway...
Example code below will illustrate 2 ways to store the command-line output to a variable in your vbs. WScript.Echo is used just as evidence (display the result).
'** VAR#1 (using Exec & StdOut) ----------
Dim ObjExec
Dim strFromProc
Set objShell = CreateObject("WScript.Shell")
Set ObjExec = objShell.Exec("%comspec% /c " _
& "netstat -a | find /c " & Chr(34) & "TCP" & Chr(34))
Do Until ObjExec.Stdout.atEndOfStream
strFromProc = strFromProc & ObjExec.StdOut.ReadLine & vbNewLine
Loop
WScript.Echo strFromProc ' display result from variable strFromProc
Set objShell = Nothing
Set ObjExec = Nothing
'** VAR#2 (using Run) --------------------
Const cLogFile = "result.txt"
Set objShell = CreateObject("WScript.Shell")
objShell.Run "%comspec% /c netstat -a | find /c " _
& Chr(34) & "TCP" & Chr(34) & ">" & cLogFile, 0, True
Dim oFile, Result
With CreateObject("Scripting.FileSystemObject")
If .FileExists(cLogFile) Then
Set oFile = .OpenTextFile(cLogFile)
Result = oFile.ReadLine
oFile.Close
Set oFile = .GetFile(cLogFile)
oFile.Delete
End If
End With
WScript.Echo Result ' display result from variable Result
Set oFile = Nothing
Set objShell = Nothing

Resources