Jump to content

CD-ROM variable assignment w/ VBscript


Recommended Posts

I don't remember who came up with this simple and elegant, yet brilliant piece of batch:

set tagfile=\WIN51
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do if exist _
    "%%i:%tagfile%" set CDDRIVE=%%i:

but it inspired me to come up with an equivelent in VBscript, as that is my preferred scripting environment. So here it is:

Dim strTemp, Drives, Drive, strFiles
strTemp = sysdrv & "\Temp"
Drives = Array("C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", _
    "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z")
For Each Drive In Drives
    If fs.FileExists(Drive & ":\WIN51") Then strFiles = Drive & ":\EXTRAS\"
Next

Note that in my case the only directory I need access to on the CD/DVD is the one called "EXTRAS." Obviously you can modify to suit your own needs. The underscore and CR entered for readability only in the case of the batch code. The underscore and CR in the VBscript is for readability also, but is acceptable and functioning code.

Link to comment
Share on other sites


Here one that I made, It makes A Vbs Script call Cd.vbs

It than it starts the CD.vbs passes the Drive letter To This CD.cmd

Which Is Than Echo Back To The Cmd Screen As The Drive Letter

So To Use It In Your Bat Files Is To Edit Out Below This

>> Cd.vbs echo ts.Close

Add What You Need Below To Match Your Needs

It Will Only Echo Back What Your Cd Letter Is, As It Is.

echo off
mode con: cols=55 lines=2
title CdVar
color 9f

> Cd.vbs echo Dim ts
>> Cd.vbs echo Dim strDriveLetter
>> Cd.vbs echo Dim intDriveLetter
>> Cd.vbs echo Dim fs 'As Scripting.FileSystemObject
>> Cd.vbs echo Const CDROM = 4
>> Cd.vbs echo On Error Resume Next
>> Cd.vbs echo Set fs = CreateObject("Scripting.FileSystemObject")
>> Cd.vbs echo strDriveLetter = ""
>> Cd.vbs echo For intDriveLetter = Asc("A") To Asc("Z")
>> Cd.vbs echo Err.Clear
>> Cd.vbs echo If fs.GetDrive(Chr(intDriveLetter)).DriveType = CDROM Then
>> Cd.vbs echo If Err.Number = 0 Then
>> Cd.vbs echo strDriveLetter = Chr(intDriveLetter)
>> Cd.vbs echo Exit For
>> Cd.vbs echo End If
>> Cd.vbs echo End If
>> Cd.vbs echo Next
>> Cd.vbs echo Set ts = fs.CreateTextFile(Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) ^& "cmd", True, False)
>> Cd.vbs echo ts.WriteLine "set " ^& Left(WScript.ScriptName, InStr(WScript.ScriptName, ".") - 1) ^& "=" ^& strDriveLetter
>> Cd.vbs echo ts.Close

ping -n 1 127.0.0.1>nul
start wscript.exe Cd.vbs
ping -n 3 127.0.0.1>nul
del cd.vbs
call cd.cmd
del cd.cmd
cls
mode con: cols=55 lines=5
color 2b
echo.
echo Your Cd Is %CD%
echo.
echo set cdrom=%CD%
echo.
ping -n 4 127.0.0.1>nul

exit

Edited by gunsmokingman
Link to comment
Share on other sites

I believe what is really being looked for in 99% of these cases is the cd letter from which windows was installed: that is where the CD is ...

FOR /F "TOKENS=3" %%I IN ('REG QUERY "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v "SourcePath" ^| FINDSTR "SourcePath"') DO SET CDROM=%%~dI

Link to comment
Share on other sites

but is acceptable and functioning code.

Not really

You forgot to declare fs

To match your thread title....

Set fs = CreateObject("Scripting.FileSystemObject")

Drives = Array("C:", "D:", "E:", "F:", "G:", "H:", "I:", "J:", "K:", "L:", "M:", _
   "N:", "O:", "P:", "Q:", "R:", "S:", "T:", "U:", "V:", "W:", "X:", "Y:", "Z:")

For Each Drive In Drives
   If fs.FileExists(Drive & "\WIN51") Then
       CDROM = Drive
       Exit For
   End If
Next

For Windows XP only because of WIN51

Link to comment
Share on other sites

@jdoe :blushing: oops. That's what I get for copy/pasting code out of a much larger script. Thanks for the correction.

@GM One thing I do right out of the gate is copy over i386 to the hard disk and then reassign that registry key. Also, was just showing a way to go about it using VBscript.

Link to comment
Share on other sites

... @GM  One thing I do right out of the gate is copy over i386 to the hard disk and then reassign that registry key.  Also, was just showing a way to go about it using VBscript.

Sorry: Wasn't trying to show you up ... I re-assign that key myself, after changing the drive letters to something more logical. At the same time I create a new system wide environmental variable with the CD letter.

The reason I went this route was that I would sometimes get "Drive not ready" errors. I cannot remember which kind of drive did it, but not all like being referenced when empty.

Link to comment
Share on other sites

@GM, another thing is that I seem to have a lot of difficulties with batch compared with VB. I used to use batch exclusively years back, and I never did take to some of the more complicated things. So I started to fiddle with Kix, which is very nice BTW if you want to disable WSH for security purposes, and I eventually settled on VBscript.

Also, that was nice tight code; only one line. I always like the simple, yet effective solutions to a problem.

One last thing. Have you looked at the batch files in BTS's Driver Packs? Made me go :wacko: Felt almost like I never wrote a line in my life. Good stuff.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...