MSFN Forum: Batch Script Tips and Tricks for XPCDs - MSFN Forum

Jump to content



  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

Batch Script Tips and Tricks for XPCDs Batch Script Tips and Tricks for XPCDs Rate Topic: ***** 1 Votes

#1 User is offline   DarkShadows 

  • The brighter the light, the darker the shadows.
  • PipPip
  • Group: Members
  • Posts: 271
  • Joined: 12-January 05

Posted 29 June 2005 - 06:08 PM

One problem in building our XPCDs is that we cannot know the CD-ROM's drive letter in advance. This is because we may have several hard drive partitions, optical drives, and other removable drives. Windows Setup Text Mode assigns it's own letters to these volumes. And from PC to PC the assigned letters will be different, based on a number factors: drive type; volume format; partition is active, primary, or extended; et. al.

To counter this you may have seen many command script examples at this web site and others which include a routine to identify the CDROM drive letter. Most of these techniques rely on checking for the existence of a known file on the CD-ROM at each possible drive letter (checking all letters C through Z inside of a For-In-Do loop). There are also other variations on these technique (many you will see in this thread).

But there are issues with such techniques:
• The code is less modular. I prefer modular code so when I move to my next project, I copy and past modules from previous scripts to new ones. When you check for a specific file, folder, or volume label, then you must make damned sure it is actually on your XPCD, and you must also make damned sure it is not on any other drive that may be attached at the time you install from your XPCD. What happens when you rebuild your PC, but you leave your other HDD partitions intact? The For-In-Do Loop Checks search all drives looking for its check file/folder/label. Such a check doesn't stop once it has found the first instance. So if you have the same check file/folder/label in more than one drive, you will not get the correct drive letter assigned to the %CDROM% variable.

• Even more critical is something that happens if you have more than one CD-ROM drive installed (which I do). A For-In-Do with a File/folder/volumename check searches all drives (C-Z), including a second CD drive that you may or may not have a CD in. It doesn't matter what these techniques are looking for, it is the very fact that they check every drive C-Z that causes the problem. Empty CD drives (and other drive types such as Iomega Rev) will cause errors when the Windows shell tries to check them for a file, or a label or whatever. Windows will put a dialog up on the screen and halt the installation until you respond:

"'Windows No Disk' 'There is no disk in the drive. [Cancel] [Try Again] [Continue]"

I personally find this problematic. In my opinion the behavior above just chopped the "Unattended " off of "Unattended Installation." I don't know about you, but I want code that allows me to stick in a XPCD and go work on something else while the PC installs itself.

But, there is a much simpler technique provided by the Windows XP Command Prompt script language, if one is executing a command script from the CD-ROM itself (as opposed to first copying the script to the hard drive and executing it from there). Assuming you are using Windows XP, or later, you can use Batch Parameter Expansion (search for "Using batch parameters" in the Windows XP Help and Support Center). When any command script executes, its first parameter, %0, equals the command script file name. However using parameter expansion, we can derive the CD-ROM drive letter from %0 by using: %~d0.

For a simple practical example, simply copy the code below and paste it into a command file and execute it:
 
@Echo OFF
SetLocal enableextensions
CLS
Echo.
Echo     I am file (not yet expanded):         %0
Echo     My Expanded File Name:                %~n0
Echo     My Expanded Extension:                %~x0
Echo     My Expanded Name and Extension:       %~nx0
Echo     My Expanded Drive:                    %~d0
Echo     The Expanded Root of My Drive:        %~d0\
Echo     Some Other Expanded Path on My Drive: %~d0\SomeOtherPath
Echo     My Expanded Path:                     %~p0
Echo     My Expanded Drive and Path:           %~dp0
Echo.
Pause

EndLocal
GoTo :EOF 
Like Yzöwl states below for Windows 2000, the Endlocal, and Goto:EOF at the end are not strictly required (but they are great programming structure). There is always one implicit EndLocal at the end of each command script in Windows XP. However, if you nest Setlocals, you should definitely use matching EndLocal statements.

While undocumented in the Windows XP Help and Support Center (surprise :realmad:), testing reveals that command extensions also NEED to be enabled in Windows XP for batch parameter expansion to work (but they are enabled by default in XP). There are two methods available to enable command extensions. One is to use:
SetLocal enableextensions
The other is to start your command script with the following command:
Cmd /e:on ScriptName.cmd
the code above to shows the first method.


This technique allows you programmatically account for differing CD/DVD-ROM drive letters from PC to PC. It only works because the command script resides on the CD-ROM drive. If you move the script above to another volume, you will get different values. To illustrate, copy the sample script above to different volumes and execute it from each--all the values above that use the d modifier will change. You cannot manipulate batch parameters in the same manner that you can manipulate environment variables. You cannot search and replace values or examine substrings. However, you can assign the parameter to an environment variable, and then manipulate the environment variable.

What makes the technique so powerful is that it doesn't require a check file, folder, or label. It doesn't care how many drives you have--it's not looking for any of them. When any script with
Set CDROM=%~d0
in it runs from the CD/DVD drive your XPCD is in, it will automatically and programmatically know the CD-ROM drive letter. It's just so much simpler to type, takes less time to execute, and is much less problematic. All the upside in the world, and no downside.

[Edit: 2005-09-22]
Since I've received questions about this original topic offline, I've decided to create and share a script people can plug into their XPCDs that solves all ambiguity as to how this technique should be employed. It is tested on Windows XP, and should work as well on Windows 2000. Download the .zip file, extract it, open the .cmd and read it. It should explain everything in sufficient detail. Post any questions to this thread.

Variables set in any script launched from CmdLines.txt do not live past the life of that script. MapCD must be called from the top of each script launched from CmdLines.txt, as %CDROM% will destroyed after each script called from CmdLines.txt ends.
[/Edit: 2005-09-22]

[Edit: 2005-09-23]
Attached File  MakeMapCD.zip (2.97K)
Number of downloads: 218Current Version: v1.1.1
Changes from v1.1 are mostly organizational and documentation. The original version I posted (v1.1) will still work just fine, so long as MapCD is called from the top of any of your scripts (which was my recommendation all along).
[/Edit: 2005-09-23]


Hope you find this useful! B)

If you have a script tip or trick that applies to unattended XPCDs post it to this thread.
Keywords: CD CD-ROM DVD DVD-ROM drive drives letter letters find locate assign obtain acquire get set %CDROM% unattended installation XPCD windows xp 2000 batch command script tip tips trick tricks technique techniques

This post has been edited by DarkShadows: 30 September 2005 - 09:55 AM



#2 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 29 June 2005 - 06:14 PM

For win2k just start the batch with
    @echo off
    setlocal enableextensions
you should end it with
    endlocal
    goto :eof
correct but not strictly necessary
<Edit>I've added a couple of extras to this one, there are other combinations though
@echo off
setlocal enableextensions
cls
echo.
echo     I am file (not yet expanded):         %0
echo     My Expanded File Name:                %~n0
echo     My Expanded Extension:                %~x0
echo     My Expanded Name and Extension:       %~nx0
echo     My Expanded Drive:                    %~d0
echo     The Expanded Root of My Drive:        %~d0\
echo     Some Other Expanded Path on My Drive: %~d0\SomeOtherPath
echo     My Expanded Path:                     %~p0
echo     My Expanded Drive and Path:           %~dp0
echo     My Expanded full filename and path    %~f0
echo     My Expanded short filename path       %~dps0
echo.
pause
endlocal
goto :eof
</Edit>

<Edit2>
Although not related to XPCDs, this one gives you a nice little example using the above
@echo off&setlocal enableextensions
md "%~dp0test"
copy %0 "%~dp0test"
del %0
Put this one on your Desktop naming it test.cmd, double click it, and keep your eyes on it at the same time.
</Edit2>

This post has been edited by Yzöwl: 29 June 2005 - 06:48 PM


#3 User is offline   Astalavista 

  • MSFN loyalist
  • PipPipPipPipPipPipPipPipPip
  • Group: Banned
  • Posts: 3,338
  • Joined: 02-December 03

Posted 29 June 2005 - 06:30 PM

nice...

#4 User is offline   Vadikan 

  • MSFT MVP Windows Expert: Consumers
  • PipPipPip
  • Group: Members
  • Posts: 349
  • Joined: 22-February 04
  • OS:Windows 7 x86
  • Country: Country Flag

Posted 29 June 2005 - 07:17 PM

I beleive the uA guide should have
SET CDROM=%~d0
in the RunOnceEx from CD article as an addition (but not replacement) to the current script.

#5 User is offline   Gee 

  • Member
  • PipPip
  • Group: Members
  • Posts: 275
  • Joined: 31-October 03

Posted 29 June 2005 - 09:19 PM

Is there a reason why you have

SetLocal enableextensions
@Echo OFF

instead of

@Echo OFF
SetLocal enableextensions

It should make a difference, but your method will echo that first line.

This post has been edited by Gee: 29 June 2005 - 09:19 PM


#6 User is offline   clavicle 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 454
  • Joined: 28-January 04
  • OS:Windows 7 x64
  • Country: Country Flag

Posted 29 June 2005 - 09:44 PM

Superb! @DarkShadows, your effort is really appreciable! The guide is really great especially for persons like me who keep hunting for sources here and there. Good job! :thumbup

#7 User is offline   Noise 

  • Windows Guru
  • PipPipPip
  • Group: Members
  • Posts: 425
  • Joined: 27-February 04

Posted 29 June 2005 - 09:45 PM

Nooooo! All my secrets! Published for all to see!

#8 User is offline   DarkShadows 

  • The brighter the light, the darker the shadows.
  • PipPip
  • Group: Members
  • Posts: 271
  • Joined: 12-January 05

Posted 29 June 2005 - 11:24 PM

Gee, on Jun 29 2005, 11:19 PM, said:

Is there a reason why you have

SetLocal enableextensions
@Echo OFF

instead of

@Echo OFF
SetLocal enableextensions

It should make a difference, but your method will echo that first line.
<{POST_SNAPBACK}>
:w00t: Whoops! Thanks for pointing that out! I'll edit the first post accordingly

clavicle, on Jun 29 2005, 11:44 PM, said:

Superb!  @DarkShadows, your effort is really appreciable!  The guide is really great especially for persons like me who keep hunting for sources here and there.  Good job! :thumbup
<{POST_SNAPBACK}>
Thank you. Glad it helps.

Nois3, on Jun 29 2005, 11:45 PM, said:

Nooooo! All my secrets! Published for all to see!
<{POST_SNAPBACK}>
:} Sorry Nois3! But for a small ransom, I won't reveal your secret identity, nor the location of your hideout! :sneaky:

#9 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 29 June 2005 - 11:46 PM

Please do not let the size of this bother you almost 1/3 of the lines are for asking if you want to keep or delete the the file.
This is not a batch file but a VBS script it will list all instances of a file named win51ip regardless of extention.
Save This As Seekwin51ip.vbs
Red Is The Question To Either keep Or Delete The File. This Can Be Removed From The Script
Green Is The File It Searches For

Quote

Dim Act : Set Act = CreateObject("Wscript.shell")
Sd = Act.ExpandEnvironmentStrings("%systemdrive%")
UN = Act.ExpandEnvironmentStrings("%UserName%")
Dim StrComputer, Tfile
TFile = (Sd & "\TempList.txt")
strComputer = "."
  Function Search_File
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where FileName =  'win51ip'")
  If colFiles.Count = 0 Then
  Act.Popup "No File Exists With That Name", 3,"Gsm No File", 0 + 32 '''' If The File Is Not There
  Exit Function
  End If
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objTextFile = objFSO.CreateTextFile(Tfile) '''' The Text File That It Makes
  For Each objFile in colFiles
  Kbname = Ucase(objFile.FileName & "." & objFile.Extension) '''' Makes The Whole File Name In Upper Case
  KbPath = UCase(objFile.Drive & objFile.Path) '''' Makes The Whole Path To File In Upper Case
  TheFile = KbPath & Kbname '''' This Makes The Full Path And File Name And Extention
  Line1 ="=========================================" 
  objTextFile.Write( Line1 & "{File Name And Location}" & Line1 & vbCrlf & vbcrlf & Kbpath & Kbname & vbCrLf & Vbcrlf)
  Next
  objTextFile.Close
  Act.Popup "Completed The Search", 3, "Gsm Search File", 0 + 32
  Act.Run(Sd & "\TempList.txt"),1,True
  '''' Ask If You Want To keep Or Delete The File
  Question = Act.popup(_
  Un & VbCrlf & "Did You want To Keep The " & Tfile & VbCrlf & "Yes To Keep The File" &_
  VbCrlf & "No To Delete The File" & VbCrlf & "If Nothing Is Selected After 15 Seconds Then" &_
  VbCrlf & "It Will Delete The File", 15, "Gsm Keep Or Delete", 4 + 32)
  If Question = 6 Then
  Exit Function
  End If
  If Question = 7 Then
  ObjFSO.DeleteFile(Sd & "\TempList.txt")
  Exit Function
  End If 
  If Question = -1 Then
  ObjFSO.DeleteFile(Sd & "\TempList.txt")
  Exit Function
  End If 

  End Function
  Search_File
And This One Is Where You Type The Name Of The file You Would Like To Search For. Save As UserSeekFile.vbs
Purple Is The User Input That Get The Name, Plus A Part That deal With Quiting And Continuing On with The script.
Red Is The Question To Either keep Or Delete The File. This Can Be Removed From The Script

Quote

Dim Act : Set Act = CreateObject("Wscript.shell")
Sd = Act.ExpandEnvironmentStrings("%systemdrive%")
UN = Act.ExpandEnvironmentStrings("%UserName%")
Dim FName, StrComputer, Tfile
TFile = (Sd & "\TempList.txt")
strComputer = "."
  Function Search_File
  UserIn = Inputbox(Un & VbCrlf & "Type In The Name Of The File You would Like To Search For." & VbCrlf & "This Does Not Need Any Extention, Adding The Extention Will Cause A False Search Result","Gsm Name The File","")
  If UserIn <> "" Then
  Else
  Gsm1 = Act.popup (Un & VbCrlf & "This Needs A Name To Complete The Search" & VbCrlf & Un & " Did You Want To Quit Or Continue?" & VbCrlf & "Yes To Continue" & VbCrlf & "No To Exit" & VbCrlf & "After 30 Seconds This Will Exit", 30,"Gsm Continue Or Quit", 4 + 64)
  If Gsm1 = 6 Then
  Search_File
  Else
  If Gsm1 = 7 Then
  Wscript.quit
  Exit Function
  End If
  If Gsm1 = -1 Then
  Wscript.quit
  Exit Function
  End If
  End If
  End If

  FName = UserIn
  Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
  Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile Where FileName = '" & FName & "'")
  If colFiles.Count = 0 Then
  Act.Popup "No File Exists With That Name", 3,"Gsm No File", 0 + 32 '''' If The File Is Not There
  Exit Function
  End If
  Set objFSO = CreateObject("Scripting.FileSystemObject")
  Set objTextFile = objFSO.CreateTextFile(Tfile) '''' The Text File That It Makes
  For Each objFile in colFiles
  Kbname = Ucase(objFile.FileName & "." & objFile.Extension) '''' Makes The Whole File Name In Upper Case
  KbPath = UCase(objFile.Drive & objFile.Path) '''' Makes The Whole Path To File In Upper Case
  TheFile = KbPath & Kbname '''' This Makes The Full Path And File Name And Extention
  Line1 ="=========================================" 
  objTextFile.Write( Line1 & "{File Name And Location}" & Line1 & vbCrlf & vbcrlf & Kbpath & Kbname & vbCrLf & Vbcrlf)
  Next
  objTextFile.Close
  Act.Popup "Completed The Search", 3, "Gsm Search File", 0 + 32
  Act.Run(Sd & "\TempList.txt"),1,True
  '''' Ask If You Want To keep Or Delete The File
  Question = Act.popup(_
  Un & VbCrlf & "Did You want To Keep The " & Tfile & VbCrlf & "Yes To Keep The File" &_
  VbCrlf & "No To Delete The File" & VbCrlf & "If Nothing Is Selected After 15 Seconds Then" &_
  VbCrlf & "It Will Delete The File", 15, "Gsm Keep Or Delete", 4 + 32)
  If Question = 6 Then
  Exit Function
  End If
  If Question = 7 Then
  ObjFSO.DeleteFile(Sd & "\TempList.txt")
  Exit Function
  End If 
  If Question = -1 Then
  ObjFSO.DeleteFile(Sd & "\TempList.txt")
  Exit Function
  End If
 
  End Function
  Search_File


#10 User is offline   Ghost82 

  • Member
  • PipPip
  • Group: Members
  • Posts: 138
  • Joined: 21-November 04

Posted 30 June 2005 - 01:12 AM

Nice!!!

I found some more info while searching with Google , check these links:

http://labmice.techt...s/batchcmds.htm

#11 User is offline   eirogge 

  • www.eirogge.de
  • PipPip
  • Group: Members
  • Posts: 218
  • Joined: 07-February 04

Posted 30 June 2005 - 01:52 AM

thats nothing that is not standing in "for /?" ;)

#12 User is offline   n7Epsilon 

  • Currently Learning: C#, JavaScript, PHP
  • PipPip
  • Group: Members
  • Posts: 156
  • Joined: 11-February 05

Posted 30 June 2005 - 06:00 AM

GREAT WORK !!!! :thumbup :thumbup

--> That saves me the hassle of coding my brains out to derive the Windows XP source path from the registry during setup !!!!!

Thanks, so simple and so unthinkable !!!

(NB: Using this method is better in RunOnceEx, as this method guarantees that the batch file will always correctly set the source path of the Windows source!! regardless of whether the source is on the CD-ROM or on a folder on the hard drive !!!!)

(*** runs off to Notepad2 to edit RunOnceEx.cmd code...)...

#13 User is offline   Martin Zugec 

  • MSFN Expert
  • PipPipPipPipPipPip
  • Group: Members
  • Posts: 1,373
  • Joined: 24-January 04

Posted 30 June 2005 - 06:08 AM

I am using this for my UNC based installations... Which is quite good way.

When I wanted to detect CDROM drive, I am using different method than most people here - my solution is based on cd name. E.g. find drive, where name is "Unattended installation" and set it as source (using WMI classes)

#14 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 30 June 2005 - 01:59 PM

I found this code on Microsoft this does not need a check file and will
list the Cd Drive
Just For Drive Letter

Quote

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
  CreateObject("Wscript.Shell").Popup  "Your Cd Is" & vbCrLf &  objItem.Drive & "\" , 4, "Cd = " & objItem.Drive , 0 + 32
Next
This One I Made Based On The Above Script It Checks To See If XP Is In The CDROM

Blue Is The Part Of The Script That Checks To See If A XP In The CDROM
Green Is The Cd Varible And File It Checks For

Quote

Dim strComputer : strComputer = "."
Dim Act : Set Act = CreateObject("Wscript.Shell")
Dim Fso : Set Fso = CreateObject("Scripting.FileSystemObject")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_CDROMDrive")
For Each objItem in colItems
Dim Cd : Cd = objItem.Drive
Next
If Fso.FileExists(CD & "\i386\winnt32.exe") Then
Act.Popup "This Is The Correct Cd" & vbCrLf & CD & "\i386\winnt32.exe", 5, CD & " Confirm"
Else
If Not Fso.FileExists(CD & "\i386\winnt32.exe") Then
Act.Popup "This File Was Missing" & vbCrLf & CD & "\i386\winnt32.exe", 5, CD & " Missing"
End If
End If


#15 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 June 2005 - 03:15 PM

This one is for Windows XP
It will tell you your CD-Drive letter(s), without the needing any disk in it
@echo off
for %%a in (a b 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 (
  fsutil fsinfo drivetype %%a:|find "CD-ROM">nul 2>&1&&echo/%%a:
)
pause&goto :eof


#16 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,020
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 30 June 2005 - 04:06 PM

I Have Changed A Couple Of Things So The Cmd Windows Is Smaller
and the Background Is White And Green Text, Sorry If the Name Is Wrong
But Using ö caused a wierd symbol to appear. I have it so it closes in about
4 seconds. The letters are now Capitals.
Thanks Nice code
@echo off && Mode 55,3 && Color f2 && Title Script By Yzowl
for %%a in (A B 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 (
 fsutil fsinfo drivetype %%a:|find "CD-ROM">nul 2>&1&&echo.&&echo Your Cd Is -^> %%a:\
)
ping -n 4 127.0.0.1>nul&goto :eof

This post has been edited by gunsmokingman: 30 June 2005 - 04:08 PM


#17 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 30 June 2005 - 04:57 PM

Just another slight change gunsmokingman, because if you have more than one cd-drive the first one will disappear before you have time to see it!
@echo off&Mode 55,3&Color f2&Title Script By Yzöwl
for %%a in (a b 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 (
  fsutil fsinfo drivetype %%a:|find "CD-ROM">nul 2>&1&&echo Your CD-ROM drive^(s^) -^> %%a:\
)
ping -n 4 127.0.0.1>nul&goto :eof


#18 User is offline   Synapse 

  • *Previously Bi0haZarD*
  • PipPipPipPipPip
  • Group: Members
  • Posts: 980
  • Joined: 29-July 04

Posted 30 June 2005 - 06:08 PM

why not just use this?

@echo off
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:\WPI.ico set CDROM=%%i:\
echo Found CD-Rom as drive %CDROM%
pause

Source: WPI.cmd from the WPI Project


substitute the WPI.ico for any file on the CD thats right off the root... such as autorun.inf or whatever. it scans through each drive letter looking for that file and when it finds it sets the variable %CDROM%

#19 User is offline   DarkShadows 

  • The brighter the light, the darker the shadows.
  • PipPip
  • Group: Members
  • Posts: 271
  • Joined: 12-January 05

Posted 30 June 2005 - 11:23 PM

Yzöwl, on Jun 30 2005, 05:15 PM, said:

This one is for Windows XP It will tell you your CD-Drive letter(s), without the needing any disk in it
@echo off
for %%a in (a b 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 (
  fsutil fsinfo drivetype %%a:|find "CD-ROM">nul 2>&1&&echo/%%a:
)
pause&goto :eof
<{POST_SNAPBACK}>

Yzöwl I like that script. Very nice. (It's the first time I've even heard of fsutil.)

Can you tell us if fsutil will be available when we are installing Windows XP from an Unattended Installation CD?

I have 2 CD ROM drives and an Iomega Rev drive (which appears to the OS as a CD-ROM drive) So I end up seeing three drive letters when the script executes on my PC. If I where installing Windows unattended, this code would show me all the drive letters. So wouldn't I still have to make a human decision as to which one to use?

However, it seems that this would be an excellent addition to my batch script which enables me to build my XPCDs (like to prompt the user which drive should be used).

Bi0haZarD, on Jun 30 2005, 08:08 PM, said:

why not just use this?
@echo off
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:\WPI.ico set CDROM=%%i:\
echo Found CD-Rom as drive %CDROM%
pause
Source: WPI.cmd from the WPI Project
substitute the WPI.ico for any file on the CD thats right off the root... such as autorun.inf or whatever. it scans through each drive letter looking for that file and when it finds it sets the variable %CDROM%
<{POST_SNAPBACK}>

One reason I do not like using file checks is that I try to write modular code so when I move to my next project, I copy and past code modules from my previous scripts. When you check for a specific file or a folder, then you must make damned sure that it is actually on the CD, and you must also make damned sure it is not on any other drive that you may have. What happens when you rebuild your PC, but you leave your other HDD partitions intact? The code you posted searches all drives looking for its check file--it does not stop when it has found the first instance. So if you had the same check file in more than one drive, you may not get the correct drive letter assigned to your %CDROM% variable. And sure, you could easily be careful enough to ensure that no two drives have the same check file. However, there is still more to deal with.

Even more critical is something that could happen if you have more than one CD-ROM drive (which I do). Because the code you posted searches all drives, it will also search my second CD drive that I do not have CD in. At which time Windows will generate a dialog with a message stating that media is not ready in the drive (or something like that). I personally find this problematic.

That's what makes the technique I started this topic with so powerful. It doesn't require a check file or folder, and it simply doesn't care how many drives you have--it's not looking for any of them. When any script with
Set CDROM=%~d0
in it runs (again running from the CD ROM itself), that script will automatically and programmatically know which drive letter the script is running from; and since it runs from the CD-ROM, we automatically and programmatically know the CD-ROM drive letter that we are executing from. It's just sooo much simpler to type, takes less time to execute, and is much less problematic. All the upside in the world, and no downside.

If you prefer to execute your scripts from %SystemDrive%, then you need only execute one short script from the CD-ROM to set the %CDROM% variable and then launch your %SystemDrive% scripts.

Keep in mind, I posted my technique to be used from your XPCD when installing Windows and executing your Cmdlines.txt-launched (or other method-launched) batch scripts, not to create the XPCD itself.

#20 User is offline   Yzöwl 

  • Wise Owl
  • Group: Super Moderator
  • Posts: 4,195
  • Joined: 13-October 04
  • OS:Windows 7 x64

Posted 01 July 2005 - 03:15 AM

@ DarkShadows

Here's a couple you may find more useful then:

This one will only give the drive letter if there's a CD loaded
@echo off&Mode 55,3&Color f2&Title Script By YzOwl
for %%a 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 (
  fsutil fsinfo volumeinfo %%a:|find "CDFS">nul 2>&1&&echo Your CD-ROM drive^(s^) -^> %%a:\
)
ping -n 4 127.0.0.1>nul&goto :eof


This one will only give the drive letter of a volume with a label of 'UWXPCD'
@echo off&Mode 55,3&Color f2&Title Script By YzOwl
for %%a 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 (
  fsutil fsinfo volumeinfo %%a:|find "UWXPCD">nul 2>&1&&echo Your specified drive is -^> %%a:\
)
ping -n 4 127.0.0.1>nul&goto :eof
<Edit>
Changed description of second example to avoid confusion.
</Edit>

This post has been edited by Yzöwl: 01 July 2005 - 03:24 AM


Share this topic:


  • 4 Pages +
  • 1
  • 2
  • 3
  • Last »
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy