-
Notifications
You must be signed in to change notification settings - Fork 3
/
ejectCD.bat
136 lines (124 loc) · 5.07 KB
/
ejectCD.bat
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
::'@ECHO OFF
::'::(Don't pollute the global environment with the following)
::'SETLOCAL ENABLEDELAYEDEXPANSION
::'::**********************************************************************
::'SET $NAME=%~n0
::'SET $DESCRIPTION=Eject CD
::'SET $AUTHOR=npocmaka https://github.com/npocmaka
::'SET $SOURCE=%~f0
::'::@(#)NAME
::'::@(-) The name of the command or function, followed by a one-line description of what it does.
::'::@(#) %$NAME% -- %$DESCRIPTION%
::'::@(#)
::'::@(#)SYNOPSIS
::'::@(-) In the case of a command, a formal description of how to run it and what command line options it takes.
::'::@(-) For program functions, a list of the parameters the function takes and which header file contains its definition.
::'::@(-)
::'::@(#) %$Name%
::'::@(#)
::'::@ (#)OPTIONS
::'::@(-) Flags, parameters, arguments (NOT the Monty Python way)
::'::@ (#) -h Help page
::'::@ (#)
::'::@(#)DESCRIPTION
::'::@(-) A textual description of the functioning of the command or function.
::'::@(#) Eject CD / DVD
::'::@(#)
::'::@(#)EXAMPLES
::'::@(-) Some examples of common usage.
::'::@(#)
::'::@ (#)EXIT STATUS
::'::@(-) Exit status / errorlevel is 0 if OK, otherwise 1+.
::'::@ (#) The following exit values are returned:
::'::@ (#) 0 Any matches were found.
::'::@ (#) 1 No matches found.
::'::@ (#)
::'::@(#)REQUIRES
::'::@(-) Dependecies
::'::@ (#) _Debug.cmd Setting up debug environment for batch scripts
::'::@ (#) _GetOpt.cmd Parse command line options and create environment vars
::'::@ (#)
::'::@ (#)SEE ALSO
::'::@ (#)
::'::@(#)REFERENCE
::'::@(#) URL: http://stackoverflow.com/a/31008030/7485823
::'::@(#) http://stackoverflow.com/questions/19467792/batch-command-line-to-eject-cd-tray
::'::@(#)
::'::@(#)SOURCE
::'::@(#) %$Source%
::'::@(#)
::'::----------------------------------------------------------------------
::':: History
::'::SET $VERSION=YYYY-MM-DD
::'::SET $REVISION=hh:mm:ss
::'::SET $COMMENT=Init / Description [xx.xxx]
::' SET $VERSION=2015-10-21
::' SET $REVISION=19:05:00
::' SET $Comment=Update usage/ErikBachmann
::'::**********************************************************************
::'::@(#)(c)%$VERSION:~0,4% %$Author%
::'::**********************************************************************
::'"%windir%\System32\cscript.exe" //nologo "%~f0?.WSF" //job:info %~nx0 %*
::'goto :EOF
<job id="info">
<script language="VBScript">
if WScript.Arguments.Count < 2 then
WScript.Echo "No drive letter passed"
WScript.Echo "Usage: "
WScript.Echo " " & WScript.Arguments.Item(0) & " {LETTER|*}"
WScript.Echo " * will eject all cd drives"
WScript.Quit 1
end if
driveletter = WScript.Arguments.Item(1):
driveletter = mid(driveletter,1,1):
Public Function ejectThisDrive (item, iType)
if InStr(Ucase(iType), "CD") Then
WScript.Echo "CD FOUND!!!"
'ejectingThisDrive (item)
set verbs=item.Verbs():
set verb=verbs.Item(verbs.Count-4):
verb.DoIt():
item.InvokeVerb replace(verb,"&","") :
ejectThisDrive = 1
else
WScript.Echo "[" & iType & "] not ejected"
ejectThisDrive = 0
End if
End Function
Public Function ejectDrive (drvLtr)
Set objApp = CreateObject( "Shell.Application" ):
Set objF=objApp.NameSpace(&H11&):
result = 0:
set MyComp = objF.Items():
for each item in objF.Items() :
iName = objF.GetDetailsOf (item,0):
iType = objF.GetDetailsOf (item,1):
if drvLtr = "*" then
WScript.Echo "eject all"
result = result + ejectThisDrive(item, iType)
'exit function:
else
iLabels = split (iName , "(" ) :
if UBound(iLabels) Then
'WScript.Echo "ilabel[" & iLabels(1) & "] "
iLabel = iLabels(1):
'WScript.Echo "ilabel[" & iLabel & "] "
if Ucase(drvLtr) = Mid(iLabel, 1, 1) Then
result = result + ejectThisDrive(item, iType)
ejectDrive = result
exit function:
end if
end if
end if
next
ejectDrive = result
End Function
WScript.Echo "Driveletter[" & driveletter & "] "
result = ejectDrive (driveletter):
WScript.Echo "result [" & result & "]"
if result = 0 then
WScript.Echo "no cd drive found with letter " & driveletter & ":"
WScript.Quit 2
end if
</script>
</job>