Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for ripcd.vbs

(view source code of ripcd.vbs as plain text)

  1. Option Explicit
  2.  
  3. Const ForAppending = 8
  4. Const ForReading   = 1
  5. Const ForWriting   = 2
  6.  
  7. Dim gvbForceOverwrite, gvbInteractive, gvbLanguageFile, gvbMyMusic
  8. Dim gvdMessages
  9. Dim gvbRetry, gvbQuiet
  10. Dim i, intAnswer, intButtons, intFirstTrack, intLastTrack, intTracks, gviCDROMDrives, gviAudioCDsLoaded, intValidArgs
  11. Dim gvoAudioCDsList, gvoCDROMDrivesList, gvoConfigFile, objFile, gvoFSO, gvoWMIService, wshShell
  12. Dim gvsCDROMDrive, gvsDrives, gvsTargetFolder, gvsVLCPath, strFile, strFileList, strMessage, strTitle, strPrefCDROM, gvsLanguage, gvsProgVer
  13.  
  14. gvsProgVer = "1.00"
  15.  
  16. Set wshShell           = CreateObject( "WScript.Shell" )
  17. Set gvoFSO             = CreateObject( "Scripting.FileSystemObject" )
  18. Set gvoCDROMDrivesList = CreateObject( "System.Collections.ArrayList" )
  19. Set gvoAudioCDsList    = CreateObject( "System.Collections.ArrayList" )
  20. Set gvoWMIService      = GetObject( "winmgmts://./root/CIMV2" )
  21. Set gvdMessages        = CreateObject( "Scripting.Dictionary" )
  22.  
  23. With WScript.Arguments.Named
  24. 	intValidArgs = 0
  25. 	If .Exists( "L" ) Then ' Use translated or customized messages
  26. 		gvbLanguageFile = True
  27. 		gvsLanguage     = .Item( "L" )
  28. 		intValidArgs    = intValidArgs + 1
  29. 	Else
  30. 		gvbLanguageFile = False
  31. 	End If
  32. 	GetMessages ' Get a list of default error and status messages, and replace them by translated or customized messages if applicable
  33. 	If .Exists( "F" ) Then ' Overwrite existing MP3 files in the target folder without prompt for confirmation
  34. 		gvbForceOverwrite = True
  35. 		intValidArgs      = intValidArgs + 1
  36. 	Else
  37. 		gvbForceOverwrite = False
  38. 	End If
  39. 	If .Exists( "I" ) Then ' Interactive mode: prompt for anything not specified on the command line
  40. 		gvbInteractive = True
  41. 		intValidArgs   = intValidArgs + 1
  42. 		If .Exists( "M" ) Then
  43. 			gvbMyMusic   = True
  44. 			intValidArgs = intValidArgs + 1
  45. 		Else
  46. 			gvbMyMusic   = False
  47. 		End If
  48. 	Else
  49. 		gvbInteractive = False
  50. 	End If
  51. 	If .Exists( "Q" ) Then ' Quiet mode: no status messages on screen, error messages only
  52. 		gvbQuiet     = True
  53. 		intValidArgs = intValidArgs + 1
  54. 	Else
  55. 		gvbQuiet     = False
  56. 	End If
  57. 	If .Exists( "?" ) Then Syntax ""
  58. 	If intValidArgs <> .Count Then Syntax gvdMessages.Item( "InvalidSwitches" )
  59. End With
  60.  
  61. With WScript.Arguments.Unnamed
  62. 	Select Case .Count
  63. 		Case 0 ' No "unnamed" arguments only allowed in interactive mode
  64. 			If gvbInteractive Then
  65. 				' Prompt for target folder
  66. 				If gvbMyMusic Then
  67. 					gvsTargetFolder = BrowseFolder( gvdMessages.Item( "SelectTargetFolder" ), wshShell.RegRead( "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Music" ) )
  68. 				Else
  69. 					gvsTargetFolder = BrowseFolder( gvdMessages.Item( "SelectTargetFolder" ), wshShell.SpecialFolders( "DESKTOP" ) )
  70. 				End If
  71. 				' Abort if "Cancel" button was clicked
  72. 				If IsNull( gvsTargetFolder ) Then
  73. 					WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  74. 					AbortScript 1
  75. 				End If
  76. 			Else
  77. 				Syntax ""
  78. 			End If
  79. 		Case 1 ' First "unnamed" argument is the target folder
  80. 			gvsTargetFolder = .Item(0)
  81. 		Case 2 ' First "unnamed" argument is the target folder, second is the CDROM drive to read
  82. 			gvsTargetFolder = .Item(0)
  83. 			strPrefCDROM    = UCase( .Item(1) )
  84. 		Case Else ' There is no case where more than 2 "unnamed" arguments are valid
  85. 			Syntax gvdMessages.Item( "InvalidArgs" )
  86. 	End Select
  87. End With
  88.  
  89. ' Create target folder if necessary
  90. If Not MakeDir( gvsTargetFolder ) Then ' Target folder does not exist and creating it failed
  91. 	If gvbInteractive Then ' Prompt for a retry in interactive mode
  92. 		' Ask if the user wants to try again
  93. 		strTitle   = gvdmessages.Item( "AlternateTarget" )
  94. 		intButtons = vbYesNoCancel + vbQuestion + vbApplicationModal
  95. 		strMessage = gvdMessages.Item( "MakeDirError" )
  96. 		If Not IsNull( gvsTargetFolder ) Then strMessage = strMessage & " """ & gvsTargetFolder & """"
  97. 		strMessage = strMessage & vbCrLf & vbCrLf & gvdMessages.Item( "AlternatePrompt" )
  98. 		intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  99. 		If intAnswer = vbYes Then ' User wants to try a different target folder
  100. 			' Prompt for alternate target folder
  101. 			If gvbMyMusic Then ' /M switch was used to start browsing in "My Music"
  102. 				gvsTargetFolder = BrowseFolder( gvdMessages.Item( "AlternateTarget" ), wshShell.RegRead( "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\My Music" ) )
  103. 			Else ' /M not used, start browsing in "Desktop"
  104. 				gvsTargetFolder = BrowseFolder( gvdMessages.Item( "AlternateTarget" ), wshShell.SpecialFolders( "DESKTOP" ) )
  105. 			End If
  106. 			If IsNull( gvsTargetFolder ) Then ' Cancel button was clicked
  107. 				WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  108. 				AbortScript 1
  109. 			End If
  110. 			If Not MakeDir( gvsTargetFolder ) Then ' Alternate target folder does not exist and creating it failed
  111. 				Syntax Replace( gvdMessages.Item( "MakeDirError" ), "%1", gvsTargetFolder )
  112. 			End If
  113. 		Else ' User gave up after first MakeDir error
  114. 			WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  115. 			AbortScript 1
  116. 		End If
  117. 	Else ' Abort unless in interactive mode
  118. 		Syntax Replace( gvdMessages.Item( "MakeDirError" ), "%1", gvsTargetFolder )
  119. 	End If
  120. End If
  121.  
  122. ' Check for existing Track*.mp3 files ik the target folder
  123. strFileList   =    ""
  124. intTracks     =     0 ' Total number of tracks found
  125. intFirstTrack = 10000 ' Lowest track index number found so far
  126. intLastTrack  =     0 ' Highest track index number found so far
  127. With gvoFSO
  128. 	For Each objFile In .GetFolder( gvsTargetFolder ).Files
  129. 		strFile = objFile.Name
  130. 		If LCase( .GetExtensionName( strFile ) ) = "mp3" Then
  131. 			If Left( LCase( .GetBaseName( strFile ) ), 5 ) = "track" Then
  132. 				If IsNumeric( Mid( .GetBaseName( strFile ), 6 ) ) Then
  133. 					intFirstTrack = Min( intFirstTrack, CInt( Mid( .GetBaseName( strFile ), 6 ) ) )
  134. 					intLastTrack  = Max( intLastTrack,  CInt( Mid( .GetBaseName( strFile ), 6 ) ) )
  135. 					intTracks     = intTracks + 1
  136. 					strFileList   = strFileList & ", """ & .GetFileName( strFile ) & """"
  137. 				End If
  138. 			End If
  139. 		End If
  140. 	Next
  141. End With
  142. ' Display as range if possible, e.g. "Track1.mp3 .. Track4.mp3" instead of "Track1.mp3, Track2.mp3, Track3.mp3, Track4.mp3"
  143. If intLastTrack - intFirstTrack + 1 = intTracks And intTracks > 2 Then
  144. 	strFileList = "Track" & intFirstTrack & ".mp3 .. Track" & intLastTrack & ".mp3"
  145. Else
  146. 	' Remove leading comma and space
  147. 	strFileList = Mid( strFileList, 3 )
  148. End If
  149.  
  150. Select Case intTracks
  151. 	Case 0
  152. 		strMessage = Replace( Replace( gvdMessages.Item( "FilesInTargetFolder" ), "%1", gvdMessages.Item( "ZeroNo" ) ), "%2", "s" )
  153. 	Case 1
  154. 		strMessage = gvdMessages.Item( "FileInTargetFolder" ) & ": " & strFileList
  155. 	Case Else
  156. 		strMessage = Replace( Replace( gvdMessages.Item( "FilesInTargetFolder" ), "%1", intTracks ), "%2", "s" ) & ": " & strFileList
  157. End Select
  158. If gvbInteractive Then
  159. 	If intTracks > 0 Then ' Prompt for confirmation to overwrite existing MP3 files in target folder
  160. 		If intTracks = 1 Then
  161. 			strMessage = strMessage & vbCrLf & vbCrLf & gvdMessages.Item( "OverwritePrompt1" )
  162. 		Else
  163. 			strMessage = strMessage & vbCrLf & vbCrLf & gvdMessages.Item( "OverwritePromptMulti" )
  164. 		End If
  165. 		intButtons = vbYesNoCancel + vbQuestion + vbApplicationModal
  166. 		strTitle   = gvdMessages.Item( "OverwritePromptTitle" )
  167. 		intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  168. 		If intAnswer = vbYes Then
  169. 			WScript.Echo gvdMessages.Item( "Overwriting" ) & " . . ."
  170. 			gvbForceOverwrite = True
  171. 		Else
  172. 			WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  173. 			AbortScript 1
  174. 		End If
  175. 	End If
  176. Else
  177. 	If intTracks > 0 Or Not gvbQuiet Then WScript.Echo strMessage
  178. End If
  179. If intTracks > 0 And Not gvbForceOverwrite Then
  180. 	' Existing tracks were found in target folder and overwrite is not allowed
  181. 	WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  182. 	AbortScript 1
  183. End If
  184.  
  185. ' List all CDROM drives
  186. If Not GetCDROMDrivesList( ) Then
  187. 	' Abort if no CDROM drive is found
  188. 	WScript.Echo vbCrLf & gvdMessages.Item( "Aborting" ) & " . . ."
  189. 	AbortScript 1
  190. End If
  191.  
  192. ' Check which one contains an audio CD
  193. Do
  194. 	If Not GetAudioCDDrive( strPrefCDROM ) Then AbortScript 1 ' Abort if Abort button is clicked
  195. Loop While gvbRetry ' Retry if no audio CD or multiple audio CDs are found and Retry is clicked
  196.  
  197. ' Get path of VLC executable
  198. On Error Resume Next
  199. gvsVLCPath = wshShell.RegRead( "HKEY_LOCAL_MACHINE\SOFTWARE\VideoLAN\VLC\" )
  200. On Error Goto 0
  201. If gvsVLCPath = "" Then
  202. 	strMessage = gvdMessages.Item( "VLCNotFoundText" )
  203. 	strTitle   = gvdMessages.Item( "VLCNotFoundTitle" )
  204. 	intButtons = vbYesNoCancel + vbInformation + vbApplicationModal
  205. 	intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  206. 	If intAnswer = vbYes Then
  207. 		wshShell.Run "http://www.videolan.org/", 3, False
  208. 	End If
  209. 	AbortScript 1
  210. End If
  211.  
  212. strMessage = gvdMessages.Item( "ReadingFromCDROM" ) & " " & gvsCDROMDrive & vbCrLf _
  213.            & gvdMessages.Item( "ExportingToFolder" ) & " """ & gvsTargetFolder & """"
  214. If Not gvbQuiet Then WScript.Echo strMessage
  215.  
  216. ' Run export command for each track on the audio CD
  217. For Each objFile In gvoFSO.GetDrive( gvsCDROMDrive ).RootFolder.Files
  218. 	ExtractTrack objFile.Name
  219. Next
  220.  
  221. ' Done
  222. AbortScript 0
  223.  
  224.  
  225.  
  226.  
  227. Function BrowseFolder( strPrompt, strDefaultFolder )
  228. 	Dim objFolder, objShell
  229.  
  230. 	Const BIF_RETURNONLYFSDIRS    =     1 ' Only return file system directories. If the user selects folders that are not part of the file system, the OK button is grayed. The OK button remains enabled for UNC items.
  231.     Const BIF_DONTGOBELOWDOMAIN   =     2 ' Do not include network folders below the domain level in the dialog box's tree view control.
  232. 	Const BIF_STATUSTEXT          =     4 ' Include a status area in the dialog box. The callback function can set the status text by sending messages to the dialog box. This flag is not supported when BIF_NEWDIALOGSTYLE is specified.
  233. 	Const BIF_RETURNFSANCESTORS   =     8 ' Only return file system ancestors. An ancestor is a subfolder that is beneath the root folder in the namespace hierarchy. If the user selects an ancestor of the root folder that is not part of the file system, the OK button is grayed.
  234. 	Const BIF_EDITBOX             =    16 ' Include an edit control in the browse dialog box that allows the user to type the name of an item.
  235. 	Const BIF_VALIDATE            =    32 ' If the user types an invalid name into the edit box, the browse dialog box calls the application's BrowseCallbackProc with the BFFM_VALIDATEFAILED message. This flag is ignored if BIF_EDITBOX is not specified.
  236. 	Const BIF_NEWDIALOGSTYLE      =    64 ' Use the new user interface. Setting this flag provides the user with a larger dialog box that can be resized. The dialog box has several new capabilities, including: drag-and-drop capability within the dialog box, reordering, shortcut menus, new folders, delete, and other shortcut menu commands.
  237. 	Const BIF_USENEWUI            =    80 ' Use the new user interface, including an edit box. This flag is equivalent to BIF_EDITBOX | BIF_NEWDIALOGSTYLE.
  238. 	Const BIF_BROWSEINCLUDEURLS   =   128 ' The browse dialog box can display URLs. The BIF_USENEWUI and BIF_BROWSEINCLUDEFILES flags must also be set. If any of these three flags are not set, the browser dialog box rejects URLs. Even when these flags are set, the browse dialog box displays URLs only if the folder that contains the selected item supports URLs. When the folder's IShellFolder::GetAttributesOf method is called to request the selected item's attributes, the folder must set the SFGAO_FOLDER attribute flag. Otherwise, the browse dialog box will not display the URL.
  239. 	Const BIF_UAHINT              =   256 ' When combined with BIF_NEWDIALOGSTYLE, adds a usage hint to the dialog box, in place of the edit box. BIF_EDITBOX overrides this flag.
  240. 	Const BIF_NONEWFOLDERBUTTON   =   512 ' Do not include the New Folder button in the browse dialog box.
  241. 	Const BIF_NOTRANSLATETARGETS  =  1024 ' When the selected item is a shortcut, return the PIDL of the shortcut itself rather than its target.
  242. 	Const BIF_BROWSEFORCOMPUTER   =  4096 ' Only return computers. If the user selects anything other than a computer, the OK button is grayed.
  243. 	Const BIF_BROWSEFORPRINTER    =  8192 ' Only allow the selection of printers. If the user selects anything other than a printer, the OK button is grayed. In Windows XP and later systems, the best practice is to use a Windows XP-style dialog, setting the root of the dialog to the Printers and Faxes folder (CSIDL_PRINTERS).
  244. 	Const BIF_BROWSEINCLUDEFILES  = 16384 ' The browse dialog box displays files as well as folders.
  245. 	Const BIF_SHAREABLE           = 32768 ' The browse dialog box can display sharable resources on remote systems. This is intended for applications that want to expose remote shares on a local system. The BIF_NEWDIALOGSTYLE flag must also be set.
  246. 	Const BIF_BROWSEFILEJUNCTIONS = 65536 ' Windows 7 and later. Allow folder junctions such as a library or a compressed file with a .zip file name extension to be browsed.
  247.  
  248. 	BrowseFolder  = Null
  249.  
  250. 	On Error Resume Next
  251. 	Set objShell  = CreateObject( "Shell.Application" )
  252. 	Set objFolder = objShell.BrowseForFolder( 0, strPrompt, BIF_RETURNONLYFSDIRS + BIF_RETURNFSANCESTORS + BIF_VALIDATE + BIF_NEWDIALOGSTYLE, strDefaultFolder )
  253. 	If IsObject( objFolder ) Then BrowseFolder = objFolder.Self.Path
  254. 	Set objFolder = Nothing
  255. 	Set objshell  = Nothing
  256. 	On Error Goto 0
  257. End Function
  258.  
  259.  
  260.  
  261.  
  262. Sub ExtractTrack( strTrack )
  263. 	Dim intTrack, strMessage, strMP3, strVLCCommand
  264. 	intTrack = Right( gvoFSO.GetBaseName( strTrack ), 3 )
  265. 	If Not IsNumeric( intTrack ) Then intTrack = Right( gvoFSO.GetBaseName( strTrack ), 2 )
  266. 	If Not IsNumeric( intTrack ) Then intTrack = Right( gvoFSO.GetBaseName( strTrack ), 1 )
  267. 	If Not IsNumeric( intTrack ) Then
  268. 		WScript.Echo vbCrLf & gvdMessages.Item( "SkippedUnknown" ) & ": """ & strTrack & """"
  269. 		Exit Sub
  270. 	End If
  271. 	intTrack = CInt( intTrack )
  272. 	With gvoFSO
  273. 		strMP3        = .BuildPath( gvsTargetFolder, .GetBaseName( strTrack ) & ".mp3" )
  274. 		strVLCCommand = """" & gvsVLCPath & """ -I http cdda:///" & gvsCDROMDrive & "/ --cdda-track=" & intTrack & " :sout=#transcode{vcodec=none,acodec=mp3,ab=128,channels=2,samplerate=44100}:std{access=""file"",mux=raw,dst=""" & strMP3 & """} vlc://quit"
  275. 		strMessage = vbCrLf _
  276. 		           & gvdMessages.Item( "Transcoding" ) & " " & strTrack & vbCrLf _
  277. 		           & gvdMessages.Item( "OutputTo" ) & " """ & strMP3 & """" & vbCrLf _
  278. 		           & gvdMessages.Item( "Exporting" ) & " . . ." '& vbCrLf & strVLCCommand
  279. 		If Not gvbQuiet Then WScript.Echo strMessage
  280. 		wshShell.Run strVLCCommand, 7, True
  281. 	End With
  282. End Sub
  283.  
  284.  
  285.  
  286.  
  287. ' Detect CDROM drives and audio CDs.
  288. ' Return true if only 1 audio CD is loaded, and store the drive letter.
  289. Function GetAudioCDDrive( strDrive )
  290. 	GetAudioCDDrive = True
  291. 	Dim i, intAnswer, intButtons
  292. 	Dim colItems, objItem
  293. 	Dim strAudioCDs, strMessage, strQuery, strTitle
  294. 	gvbRetry = False
  295. 	strQuery = "SELECT * FROM Win32_CDROMDrive WHERE MediaLoaded=True AND VolumeName LIKE 'Audio%'"
  296. 	If Not strDrive = "" Then strQuery = strQuery & " AND Drive='" & UCase( strDrive ) & "'"
  297. 	Set colItems = gvoWMIService.ExecQuery( strQuery )
  298. 	gviAudioCDsLoaded = colItems.Count
  299. 	Select Case gviAudioCDsLoaded
  300. 		Case 0
  301. 			If strDrive = "" Then
  302. 				strMessage = gvdMessages.Item( "NoAudioCDText1" )
  303. 				If gviCDROMDrives > 1 Then
  304. 					strMessage = Replace( strMessage, "%1", Replace( gvdMessages.Item( "NoAudioCDText2" ), "%1", gvsDrives ) ) ' gvsDrives is set in GetCDROMDrivesList( )
  305. 				Else
  306. 					strMessage = Replace( strMessage, "%1", "" )
  307. 				End If
  308. 			Else
  309. 				strMessage = Replace( gvdMessages.Item( "NoAudioDriveSpecText" ), "%1", UCase( strDrive ) )
  310. 			End If
  311. 			strMessage = Replace( strMessage, "\n", vbCrLf )
  312. 			strTitle   = gvdMessages.Item( "NoAudioCDTitle" )
  313. 			intButtons = vbRetryCancel + vbInformation + vbApplicationModal
  314. 			intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  315. 			If intAnswer = vbRetry Then
  316. 				gvbRetry = True
  317. 			Else
  318. 				GetAudioCDDrive = False
  319. 			End If
  320. 		Case 1
  321. 			For Each objItem In colItems
  322. 				gvsCDROMDrive = objItem.Drive
  323. 			Next
  324. 		Case Else
  325. 			For Each objItem In colItems
  326. 				gvoAudioCDsList.Add objItem.Drive
  327. 			Next
  328. 			strAudioCDs = gvoAudioCDsList.Item(0)
  329. 			For i = 1 To gviAudioCDsLoaded - 1
  330. 				If i = gviAudioCDsLoaded - 1 Then
  331. 					strAudioCDs = strAudioCDs & " or " & gvoAudioCDsList.Item(i)
  332. 				Else
  333. 					strAudioCDs = strAudioCDs & ", " & gvoAudioCDsList.Item(i)
  334. 				End If
  335. 			Next
  336. 			strMessage = gvdMessages.Item( "MultipleAudioCDsText" )
  337. 			strTitle   = gvdMessages.Item( "MultipleAudioCDsTitle" )
  338. 			intButtons = vbRetryCancel + vbInformation + vbApplicationModal
  339. 			intAnswer  = MsgBox( strMessage, intButtons, strTitle )
  340. 			If intAnswer = vbRetry Then
  341. 				gvbRetry = True
  342. 			Else
  343. 				GetAudioCDDrive = False
  344. 			End If
  345. 	End Select
  346. 	Set colItems = Nothing
  347. End Function
  348.  
  349.  
  350.  
  351.  
  352. ' Get CDROM drive letter(s), return False if none found
  353. Function GetCDROMDrivesList( )
  354. 	GetCDROMDrivesList = True
  355. 	Dim intButtons, intTracks
  356. 	Dim colItems, objItem
  357. 	Dim strDrive, strMessage, strQuery, strTitle
  358. 	strQuery = "SELECT * FROM Win32_CDROMDrive"
  359. 	Set colItems = gvoWMIService.ExecQuery( strQuery )
  360. 	gviCDROMDrives = colItems.Count
  361. 	For Each objItem In colItems
  362. 		strDrive   = objItem.Drive
  363. 		strMessage = strMessage & Replace( gvdMessages.Item( "FoundCDROMDrive" ), "%1", strDrive )
  364. 		gvoCDROMDrivesList.Add strDrive
  365. 		If LCase( Left( objItem.VolumeName, 5 ) ) = "audio" Then
  366. 			strMessage = Replace( strMessage, "%2", gvdMessages.Item( "AudioCDLoadedYes" ) ) & vbCrLf
  367. 			intTracks  = gvoFSO.GetFolder( strDrive & "\" ).Files.Count
  368. 			If intTracks = 1 Then
  369. 				strMessage = Replace( strMessage, "%3", ", 1 " & gvdMessages.Item( "Track" ) )
  370. 			Else
  371. 				strMessage = Replace( strMessage, "%3", ", " & intTracks & " " & gvdMessages.Item( "Tracks" ) )
  372. 			End If
  373. 		Else
  374. 			strMessage = Replace( Replace( strMessage, "%2", gvdMessages.Item( "AudioCDLoadedNo" ) ), "%3", "" ) & vbCrLf
  375. 		End If
  376. 	Next
  377. 	If Not gvbQuiet Then WScript.Echo strMessage
  378. 	gvoCDROMDrivesList.Sort
  379. 	gvsDrives = ""
  380. 	Select Case gviCDROMDrives
  381. 		Case 0
  382. 			strMessage = gvdMessages.Item( "NoCDROMDriveText" )
  383. 			strTitle   = gvdMessages.Item( "NoCDROMDriveTitle" )
  384. 			intButtons = vbOKOnly + vbExclamation + vbApplicationModal
  385. 			MsgBox strMessage, intButtons, strTitle
  386. 			GetCDROMDrivesList = False
  387. 		Case 1
  388. 			gvsDrives = gvoCDROMDrivesList.Item(0)
  389. 		Case Else
  390. 			gvsDrives = gvoCDROMDrivesList.Item(0)
  391. 			For i = 1 To gviCDROMDrives - 1
  392. 				If i = gviCDROMDrives - 1 Then
  393. 					gvsDrives = gvsDrives & " " & gvdMessages.Item( "or" ) & " " & gvoCDROMDrivesList.Item(i)
  394. 				Else
  395. 					gvsDrives = gvsDrives & ", " & gvoCDROMDrivesList.Item(i)
  396. 				End If
  397. 			Next
  398. 	End Select
  399. 	Set colItems = Nothing
  400. End Function
  401.  
  402.  
  403.  
  404.  
  405. ' Get a list of default error and status messages, and replace them by translated messages if applicable
  406. Sub GetMessages( )
  407. 	Dim strConfigFile, strConfigText, strKey, strLine, strVal
  408. 	gvdMessages.Item( "Aborting" )              = "Aborting"
  409. 	gvdMessages.Item( "AlternatePrompt" )       = "Do you want to specify an alternative target folder?"
  410. 	gvdMessages.Item( "AlternateTarget" )       = "Please select/create an alternative target folder"
  411. 	gvdMessages.Item( "AudioCDLoadedNo" )       = "no"
  412. 	gvdMessages.Item( "AudioCDLoadedYes" )      = "with"
  413. 	gvdMessages.Item( "CreatingFolder" )        = "Creating folder: ""%1"""
  414. 	gvdMessages.Item( "CustomFileNotFound" )    = "Custom messages file not found"
  415. 	gvdMessages.Item( "Error" )                 = "ERROR"
  416. 	gvdMessages.Item( "Exporting" )             = "Exporting"
  417. 	gvdMessages.Item( "ExportingToFolder" )     = "Exporting to output folder"
  418. 	gvdMessages.Item( "FileInTargetFolder" )    = "Found a file in target folder"
  419. 	gvdMessages.Item( "FilesInTargetFolder" )   = "Found %1 file%2 in target folder"
  420. 	gvdMessages.Item( "FoundCDROMDrive" )       = "Found CDROM drive %1 (%2 audio CD loaded%3)"
  421. 	gvdMessages.Item( "InvalidArgs" )           = "Invalid command line arguments."
  422. 	gvdMessages.Item( "InvalidSwitches" )       = "Invalid or duplicate command line switches."
  423. 	gvdMessages.Item( "MakeDirError" )          = "Unable to create target folder"
  424. 	gvdMessages.Item( "MultipleAudioCDsText" )  = "The script found multiple CDROM drives with an audio CD inserted.\n\nEither specify the CDROM drive on the command line, or remove all audio CDs but one and click Retry to try again."
  425. 	gvdMessages.Item( "MultipleAudioCDsTitle" ) = "Multiple Audio CDs"
  426. 	gvdMessages.Item( "NoAudioCDText1" )        = "The script was unable to locate a CDROM drive with an audio CD inserted.\n\nPlease insert an audio CD%1 and click Retry to try again."
  427. 	gvdMessages.Item( "NoAudioCDText2" )        = " in one of the CDROM drives (%1)"
  428. 	gvdMessages.Item( "NoAudioDriveSpecText" )  = "No audio CD found in the specified CDROM drive (%1)\n\nPlease insert an audio CD in CDROM drive %1 and click Retry to try again."
  429. 	gvdMessages.Item( "NoAudioCDTitle" )        = "No Audio CD"
  430. 	gvdMessages.Item( "NoCDROMDriveText" )      = "The script was unable to locate a CDROM drive.\n\nPlease specify a CDROM drive on the command line."
  431. 	gvdMessages.Item( "NoCDROMDriveTitle" )     = "No CDROM Drive"
  432. 	gvdMessages.Item( "or" )                    = "or"
  433. 	gvdMessages.Item( "OutputTo" )              = "Output to"
  434. 	gvdMessages.Item( "OverwritePrompt1" )      = "Do you want to overwrite it?"
  435. 	gvdMessages.Item( "OverwritePromptMulti" )  = "Do you want to overwrite them?"
  436. 	gvdMessages.Item( "OverwritePromptTitle" )  = "Overwrite Files?"
  437. 	gvdMessages.Item( "Overwriting" )           = "Overwriting files"
  438. 	gvdMessages.Item( "ReadingFromCDROM" )      = "Reading from CDROM drive"
  439. 	gvdMessages.Item( "SelectTargetFolder" )    = "Select a target folder"
  440. 	gvdMessages.Item( "SkippedUnknown" )        = "Skipped unknown content"
  441. 	gvdMessages.Item( "Track" )                 = "track"
  442. 	gvdMessages.Item( "Tracks" )                = "tracks"
  443. 	gvdMessages.Item( "Transcoding" )           = "Transcoding"
  444. 	gvdMessages.Item( "VLCNotFoundText" )       = "The script could not find a VLC Media Player installation.\n\nVLC Media Player is required for this script.\n\nDo you want to open the VLC download page in your browser?"
  445. 	gvdMessages.Item( "VLCNotFoundTitle" )      = "VLC Not Found"
  446. 	gvdMessages.Item( "ZeroNo" )                = "no"
  447. 	' Help Text lines
  448. 	gvdMessages.Item( "SyntaxTitleLine" )       = "RipCD.vbs,  Version %1"
  449. 	gvdMessages.Item( "SyntaxDescrLine" )       = "Save audio CD tracks as MP3s"
  450. 	gvdMessages.Item( "SyntaxUsageLine" )       = "Usage:   RIPCD.VBS targetfolder [drive:] [/F] [/I [/M]] [/L:lang] [/Q]"
  451. 	gvdMessages.Item( "SyntaxWhereTarget" )     = "Where:   ""targetfolder""  is the fully qualified path to the folder where the\nMP3s are to be stored"
  452. 	gvdMessages.Item( "SyntaxWhereDrive" )      = "drive:          is the CDROM drive letter where the audio CD is located"
  453. 	gvdMessages.Item( "SyntaxWhereSwF" )        = "/F              Force overwrite of existing files in the target folder"
  454. 	gvdMessages.Item( "SyntaxWhereSwI" )        = "/I              Interactive mode: prompt for input when necessary"
  455. 	gvdMessages.Item( "SyntaxWhereSwL" )        = "/L[:lang]       use translated or custom messages (see note 3 below)"
  456. 	gvdMessages.Item( "SyntaxWhereSwM" )        = "/M              suggest ""My Music"" or subfolders only as targetfolder\nwhen in Interactive mode (requires /I)"
  457. 	gvdMessages.Item( "SyntaxWhereSwQ" )        = "/Q              Quiet mode: less screen output (errors only)"
  458. 	gvdMessages.Item( "SyntaxNotes1"  )         = "Notes: 1 This script requires VLC Media Player; if it isn't found, you will\nbe prompted to open the VLC download page in your default web browser."
  459. 	gvdMessages.Item( "SyntaxNotes2"  )         = "2 By default, the script will abort if ""Track*.mp3"" files are detected\nin the target folder. Use /F to overwrite without confirmation."
  460. 	gvdMessages.Item( "SyntaxNotes3"  )         = "3 The default messages can be replaced by translated or custom messages;\nto do so, run the script with the /L switch (no value) to create a\ntemplate file ""RipCD.en"". Open that file in a text editor, modify\nthe messages you want to change or translate, and save the file with\na different extension (e.g. ""ripcd.NL"" for a Dutch translation).\nTo use the modified messages, run the script with /L:extension\n(e.g. /L:NL for a Dutch translation located in ""ripcd.NL"")."
  461. 	gvdMessages.Item( "SyntaxCredits" )         = "Credits: Main functionality by elrobis (http://cartometric.com/blog/2015/03/07)\nAutomatic CDROM drive and VLC path detection by Rob van der Woude"
  462. 	gvdMessages.Item( "SyntaxAuthor"  )         = "Written by Rob van der Woude\nhttp://www.robvanderwoude.com"
  463. 	If gvbLanguageFile Then
  464. 		With gvoFSO
  465. 			If gvsLanguage = "" Then
  466. 				' Write current messages to a "template" file
  467. 				strConfigFile = .BuildPath( .GetParentFolderName( WScript.ScriptFullName ), .GetBaseName( WScript.ScriptName ) & ".en" )
  468. 				Set gvoConfigFile = .OpenTextFile( strConfigFile, ForWriting, True )
  469. 				gvoConfigFile.WriteLine "// Do not replace or translate %1 .. %3, these are ""placeholders"" for variables in messages"
  470. 				For Each strKey In gvdMessages.Keys
  471. 					strVal  = gvdMessages.Item( strKey )
  472. 					strLine = strKey & "=" & strVal
  473. 					gvoConfigFile.WriteLine strLine
  474. 				Next
  475. 				gvoConfigFile.Close
  476. 			Else
  477. 				' Read translated or custom messages from text file
  478. 				strConfigFile = .BuildPath( .GetParentFolderName( WScript.ScriptFullName ), .GetBaseName( WScript.ScriptName ) & "." & gvsLanguage )
  479. 				If .FileExists( strConfigFile ) Then
  480. 					Set gvoConfigFile = .OpenTextFile( strConfigFile, ForReading, False )
  481. 					strConfigText = gvoConfigFile.ReadAll( )
  482. 					For Each strLine In Split( strConfigText, vbCrLf )
  483. 						If InStr( strLine, "=" ) Then
  484. 							strKey = Left( strLine, InStr( strLine, "=" ) - 1 )
  485. 							strVal = Mid(  strLine, InStr( strLine, "=" ) + 1 )
  486. 							gvdMessages.Item( strKey ) = strVal
  487. 						End If
  488. 					Next
  489. 				Else
  490. 					Syntax gvdMessages.Item( "CustomFileNotFound" ) & ": """ & strConfigFile & """"
  491. 				End If
  492. 			End If
  493. 			Set gvoConfigFile = Nothing
  494. 		End With
  495. 	End If
  496. End Sub
  497.  
  498.  
  499.  
  500.  
  501. Function MakeDir( strFolder )
  502. 	MakeDir = False
  503. 	On Error Resume Next
  504. 	Dim gvoFSO, strFullPath, strLeft, strNextFolder, strRoot, strTreeSoFar
  505. 	Set gvoFSO = CreateObject( "Scripting.FileSystemObject" )
  506. 	strFullPath  = gvoFSO.GetAbsolutePathName( gvsTargetFolder )
  507. 	strRoot      = gvoFSO.GetDriveName( gvsTargetFolder )
  508. 	If Len( strRoot ) = 2 Then
  509. 		strLeft      = Mid( strFullPath, Len( strRoot ) + 2 )
  510. 		strTreeSoFar = strRoot
  511. 		For Each strNextFolder In Split( strLeft, "\" )
  512. 			strTreeSoFar = gvoFSO.BuildPath( strTreeSoFar & "\", strNextFolder )
  513. 			If Not gvoFSO.FolderExists( strTreeSoFar ) Then
  514. 				If Not gvbQuiet Then WScript.Echo Replace( gvdMessages.Item( "CreatingFolder" ), "%1", strTreeSoFar )
  515. 				gvoFSO.CreateFolder( strTreeSoFar )
  516. 			End If
  517. 		Next
  518. 		MakeDir = gvoFSO.FolderExists( strFullPath )
  519. 	End If
  520. 	Set gvoFSO = Nothing
  521. 	On Error Goto 0
  522. End Function
  523.  
  524.  
  525.  
  526.  
  527. Function Max( num1, num2 )
  528. 	If num1 > num2 Then
  529. 		Max = num1
  530. 	Else
  531. 		Max = num2
  532. 	End If
  533. End Function
  534.  
  535.  
  536.  
  537.  
  538. Function Min( num1, num2 )
  539. 	If num1 < num2 Then
  540. 		Min = num1
  541. 	Else
  542. 		Min = num2
  543. 	End If
  544. End Function
  545.  
  546.  
  547.  
  548.  
  549. Sub Syntax( errMsg )
  550. 	Dim strMsg
  551. 	strMsg = vbCrLf
  552. 	If Not errMsg = "" Then strMsg = strMsg & gvdMessages.Item( "Error" ) & ": " & errMsg & vbCrLf & vbCrLf
  553. 	strMsg = strMsg _
  554. 	       &     Replace( Replace( gvdMessages.Item( "SyntaxTitleLine" ),   "\n", vbCrLf ), "%1", gvsProgVer )    & vbCrLf _
  555. 	       &              Replace( gvdMessages.Item( "SyntaxDescrLine" ),   "\n", vbCrLf )               & vbCrLf & vbCrLf _
  556. 	       &              Replace( gvdMessages.Item( "SyntaxUsageLine" ),   "\n", vbCrLf & Space(  9 ) ) & vbCrLf & vbCrLf _
  557. 	       &              Replace( gvdMessages.Item( "SyntaxWhereTarget" ), "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  558. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereDrive" ),  "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  559. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereSwF" ),    "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  560. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereSwI" ),    "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  561. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereSwL" ),    "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  562. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereSwM" ),    "\n", vbCrLf & Space( 25 ) ) & vbCrLf _
  563. 	       & Space( 9 ) & Replace( gvdMessages.Item( "SyntaxWhereSwQ" ),    "\n", vbCrLf & Space( 25 ) ) & vbCrLf & vbCrLf _
  564. 	       &              Replace( gvdMessages.Item( "SyntaxNotes1" ),      "\n", vbCrLf & Space(  9 ) ) & vbCrLf _
  565. 	       & Space( 7 ) & Replace( gvdMessages.Item( "SyntaxNotes2" ),      "\n", vbCrLf & Space(  9 ) ) & vbCrLf _
  566. 	       & Space( 7 ) & Replace( gvdMessages.Item( "SyntaxNotes3" ),      "\n", vbCrLf & Space(  9 ) ) & vbCrLf & vbCrLf _
  567. 	       &              Replace( gvdMessages.Item( "SyntaxCredits" ),     "\n", vbCrLf & Space(  9 ) ) & vbCrLf & vbCrLf _
  568. 	       &              Replace( gvdMessages.Item( "SyntaxAuthor"  ),     "\n", vbCrLf )
  569. 	WScript.Echo strMsg
  570. 	AbortScript 1
  571. End Sub
  572.  
  573.  
  574.  
  575.  
  576. ' Abort the script after cleaning up any leftover objects
  577. Sub AbortScript( intRC )
  578. 	On Error Resume Next
  579. 	Set gvdMessages        = Nothing
  580. 	Set gvoWMIService      = Nothing
  581. 	Set gvoAudioCDsList    = Nothing
  582. 	Set gvoCDROMDrivesList = Nothing
  583. 	Set gvoConfigFile      = Nothing
  584. 	Set gvoFSO             = Nothing
  585. 	Set wshShell           = Nothing
  586. 	On Error Goto 0
  587. 	WScript.Quit intRC
  588. End Sub
  589.  

page last uploaded: 2017-07-06, 12:37