while [1 = 1]
do
eject
sleep 1
eject -t
sleep 1
done
And this is said to be the same:
watch -n 1 eject -T
What does it do?What's the equivalent in batch?
you can try this vbscript
Const CDROM = 4
Set objFS=CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("Shell.Application")
For Each drives in objFS.Drives
If drives.DriveType = CDROM Then
cd=drives.DriveLetter & ":\"
'Eject
End If
Next
Do
shell.Namespace(17).ParseName(cd).InvokeVerb("E&ject")
WScript.Sleep 1000 'in ms
Loop While 1=1
save as eject.vbs run it as
c:\test> cscript //nologo eject.vbs
Related
I am new to Windows Scripting. I have a simple script for archiving using WinRAR CLI utility. I have to schedule this script using batch file. During archiving there are some errors and I want them to write in a simple text file or at least I can write entire output of archiving in a file. How can I change my code to do this?
Dim MyDate
Dim OutputFile
const WaitUntilFinished = true, DontWaitUntilFinished = false, ShowWindow = 1, DontShowWindow = 0
MyDate = Replace(Date, "/", "-")
OutputFile = "backup-" & mydate & ".rar"
Set objShell = WScript.CreateObject("WScript.Shell")
objShell.CurrentDirectory = "C:\Users\ABC\Desktop\"
objShell.Run "C:\windows\Rar.exe a .\VBScripts\backups\" & OutputFile & " software", ShowWindow, WaitUntilFinished
objShell.Popup "Archiving Completed Successfully!",5, "Scheduled Backup"
Set objShell = Nothing
Batch file is like this;
#echo off
start /wait C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs
Change your command line to include redirection to a log file:
logfile = "C:\path\to\your.log"
objShell.Run "%COMSPEC% /c C:\windows\Rar.exe a .\VBScripts\backups\" & _
OutputFile & " software >""" & logfile & """", ShowWindow, WaitUntilFinished
Use this function instead of WScript.Shell.Run:
' Runs an external program and pipes it's output to
' the StdOut and StdErr streams of the current script.
' Returns the exit code of the external program.
Function Run (ByVal cmd)
Dim sh: Set sh = CreateObject("WScript.Shell")
Dim wsx: Set wsx = Sh.Exec(cmd)
If wsx.ProcessID = 0 And wsx.Status = 1 Then
' (The Win98 version of VBScript does not detect WshShell.Exec errors)
Err.Raise vbObjectError,,"WshShell.Exec failed."
End If
Do
Dim Status: Status = wsx.Status
WScript.StdOut.Write wsx.StdOut.ReadAll()
WScript.StdErr.Write wsx.StdErr.ReadAll()
If Status <> 0 Then Exit Do
WScript.Sleep 10
Loop
Run = wsx.ExitCode
End Function
Call script instead of start in your batch and use redirection:
script //nologo C:\Users\ABC\Desktop\VBScripts\scheduled_backup.vbs 2> errors.txt
Please lend some help, I'm a beginner with batch file and vbscript.
I would be grateful if you could help me solve my problem.
Given:
a:
#echo off
pushd "C:\Program Files\appName" && popd
IF ERRORLEVEL 1 EXIT 0
pushd "C:\Program Files\appName"
IF EXIST application.exe GOTO Installed
popd
EXIT 1
:Installed
EXIT 2
b:
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Set objFile = objFSO.OpenTextFile(listFile, 1)
dim sh , res
objName = objFSO.GetTempName
objTempFile = objName
'---- Sample Code 0 ----------------------'
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("fileFullPath",0,true)
'wScript.echo res
'----Sample Code 1 ---------------'
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("fileName",0,true)
'wScript.echo res
'---- Sample Code 2 ---------------------
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("cmd /c fileName",0,true)
'wScript.echo res
'---- Sample Code 3 ---------------------
'set sh = WScript.CreateObject("WScript.shell")
'res = sh.run("cmd /c fileFullPath",0,true)
'wScript.echo res
The b:sample code 1 & 2 works fine with me but I need the sample code 0 & 3.
with sample code 1 I always get an error : The system cannot find the file selected
,while with sample code 3 it would only return 0 when failed then 1 when successful, and not the returned value of the batch file.
Thanks,
I already got my problem solved just a while ago. I found out that even-though i already have had "cmd /c fileFullPath" double-quoted. When a folder name(s) consists some spaces, it would cause an issue --> file can't be found. Now I'm using "cmd /c ""fileFullPath""" instead.
Sorry for having my first post lack of information. I will do my best to make it clearer next time. Thanks a lot. :D
Here is my VBS code
Set wshshell = wscript.CreateObject("WScript.Shell")
Wshshell.run "C:\Temp\Executable.exe -c -dir C:\Productdir"
'Wait till "This will install the product on your computer. Press OK, Cancel" appears
WScript.Sleep 10000
WshShell.SendKeys "~"
Is it possible "rather than hard-coded sleep of 10 secs" to add something like this for e.g. if consolemessage="This will install the product on your computer. Press OK, Cancel" then WshShell.SendKeys "~"?
Can WScript.StdOut be used to capture the console messages in the above case? I was not able to do it.
You can read StdOut of a process when you execute the program using the Exec method.
Set wshshell = wscript.CreateObject("WScript.Shell")
Set p = Wshshell.Exec("C:\Temp\Executable.exe -c -dir C:\Productdir")
Do While p.Status = 0
output = ""
Do Until p.StdOut.AtEndOfStream
c = p.StdOut.Read(1)
WScript.StdOut.Write c 'write read characters to the command prompt
output = output & c
If InStr(output, "This will install the product") > 0 Then
'do stuff
Exit Do
End If
Loop
WScript.Sleep 100
Loop
Got a simple script that executes a command to a server - briefly:
//Create shell
set WshShell=CreateObject("WScript.Shell")
WshShell.run "cmd.exe"
//send commands
WshShell.SendKeys "telnet IP_ADDRESS"
WshShell.Sendkeys "dir"
Server offers feedback which I want to capture. I just need to capture the first line into a variable, and then just print that variable out to confirm.
Can you help? Thanks.
Do not use the Windows telnet client for automation purposes. The telnet client that ships with Windows was made for interactive use only.
I'd use plink (from the PuTTY suite) in batch mode for this:
plink.exe -telnet -batch IP_ADDRESS dir
The tool doesn't require installation, so you can simply deploy it alongside your script.
Run it either in a batch file using head/tail, or in a VBScript using the Exec method, so you can read from StdOut:
addr = "62.39.x.x"
port = 24
timeout = 300 'seconds
timedOut = False
cmdline = "echo ""mute near get"" | plink.exe -telnet -batch " & addr & " -P " & port
Set sh = CreateObject("WScript.Shell")
'change working directory to directory containing script and plink executable
Set fso = CreateObject("Scripting.FileSystemObject")
sh.CurrentDirectory = fso.GetParentFolderName(WScript.ScriptFullName)
'wait until command completes or timeout expires
expiration = DateAdd("s", timeout, Now)
Set cmd = sh.Exec("%COMSPEC% /c " & cmdline)
Do While cmd.Status = 0 And Now < expiration
WScript.Sleep 100
Loop
If cmd.Status = 0 Then
cmd.Terminate
timedOut = True
End If
WScript.Echo cmd.StdOut.ReadAll
If cmd.ExitCode <> 0 Then
WScript.Echo "Command terminated with exit code " & cmd.ExitCode & "."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 1
ElseIf timedOut Then
WScript.Echo "Command timed out."
WScript.Echo cmd.StdErr.ReadAll
WScript.Quit 2
End If
It might not be the best method, but worked for me:
Windows telnet command can save the output in client side using -f arguments. Therefore, you could use:
WshShell.SendKeys "telnet -f D:\output\telnet.out IP_ADDRESS"
and at the end of your script, simply process the content of telnet.out
I'm using VBScript, and my goal is to be able to substitute a drive letter for a path of my choosing. I need the D drive, and if it's not available I need to check if it's already mapped to the right spot; then notify the user if it's not. I found this: http://technet.microsoft.com/en-us/library/ee156605.aspx and I'm trying to adapt their second example:
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c ping -n 3 -w 1000 157.59.0.1")
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
If Instr(strText, "Reply") > 0 Then
Wscript.Echo "Reply received."
Exit Do
End If
Loop
(my adaptations):
Set objShell = WScript.CreateObject("WScript.Shell")
Set objExecObject = objShell.Exec("cmd /c substr")
strText = ""
Do While Not objExecObject.StdOut.AtEndOfStream
strText = strText & objExecObject.StdOut.ReadLine()
Loop
Wscript.Echo strText
Then I'll probably search for the string that tells where the D drive is mapped. I've also tried objShell.Exec("subst"), but I still don't get any output. Does anyone have any ideas on what I might be doing wrong? Or is there a better way to tell about drive mappings? Thanks,
213897
Your script doesn't work because you've mistyped the command name - it's subst, not substr.