Plamdi Posted October 26, 2006 Posted October 26, 2006 FOR %%i IN (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:\CD1.txt SET CDROM=%%i:Why is this even suggested here?I love batch files, I've loved .BAT files since I was a kid... I also love working with VBS files too. Have you ever heard of Keep it Simple? Why do they suggest if using:SET CDROM=%~d0That you put in SetLocal enableextensions? Command extensions are enabled by default, and you need to edit the registry to disable them - so during an unattended installation it isn't possible that they've been disabled yet! Nevertheless, it is a good idea to use it because then all environment variables you set will be cancelled once the file has completed - there is actually very little difference in the two methods - in the end result that is - the "CDROM" is an environment variable set - but when you use Setlocal you can cancel the variable with Endlocal, so it does not carry over into other processes.It is a terrible idea to use the file searching method when there is a better method. Batch files take arguments, like:RunOnceEx.cmd Arg1 "Argument 2" Argument3And you can call the arguments using %1, %2, %3, etc... you can also call %0 which is the argument of the filename itself. Calling %0 by itself will simply return the string as it was typed to call the file, for instance the string could have been RunOnceEx or it could have been RunOnceEx.cmd or it may have been $OEM$\RunOnceEx.cmd ... or it could have even had the drive letter in it with the full path: X:\$OEM$\RunOnceEx.cmd.But it doesn't matter because the actual variable as understood by cmd.exe is the file's identity. So no matter how it was typed you can get the full name of the file, the full path of the file - short or long, or the drive letter of the file. In fact, %~f0 will give you the full path to the file, including drive letter, folder(s) and file name (including extension). That's all very interesting but how is it useful? Imagine you've created a batch file for several purposes, defined by an argument... you could do:REG ADD %K%\001 /VE /D "Some function" /fREG ADD %K%\001 /V 1 /D "\"%~f0\" Argument" /fAnd you could save having to use more batch files.Interestingly there is another very useful combination that goes%~dp0That gives you the path to the file. To give you a comparison, this is how I do the same thing in vb script:p=Left(WScript.ScriptFullName,Len(WScript.ScriptFullName)-Len(WScript.ScriptName))It is a very useful combination, for instance you could write "%~dp0PROGRAMNAME.EXE" in your batch file, and as long as your .CMD and .EXE are in the same directory the exe will be called by the cmd file. No need to manually write out the path. Personally I prefer to work with this in my RunOnceEx.cmd:SET P=%~d0\PROGRAMS\SET K=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceExIf you still useFOR %%i IN (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:\CD1.txt SET CDROM=%%i:Then please, delete it and replace it with SET CDROM=%~d0 which does not search for anything - it just goes and gets it.
Yzöwl Posted October 26, 2006 Posted October 26, 2006 FOR %%i IN (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:\CD1.txt SET CDROM=%%i:Why is this even suggested here?<snipped rant>Because it can be utilized when using more than one CD-ROM drive, or when the running batch file is not on the CD-ROM itself. You should have been able to ascertain this fact by your understanding of the scripting language, the contained 'tag file' and perhaps a little thought!
mazin Posted October 26, 2006 Posted October 26, 2006 You restricted the topic title to batch files, but talked about VBScripts, too.Well! Let this discussion be open, then.SET CDROM=%~d0 does NOT fit everywhere!Also, long ago, I stopped using this method: FOR %%i IN (D <-> Z) DO IF EXIST %%i:\CD1.txt SET CDROM=%%iThere are several situations where you want to get a CD letter (drive).Hence, a method someone uses to get a CD letter may vary depending on several factors.For in-depth investigation, see the following situations:1- I have CDSVCPAK.CMD located as i386\SVCPACK\CDSVCPAK.CMD!As you may have already guessed, this file is launcehd (by SVCPACK.INF) from my harddisk during Windows Setup.CDSVCPAK.CMD runs 34 packages right from my UACD, along with some reg files.SET CDROM=%~d0 does NOT fit here! It would set the CD letter to C: (my current System Drive).I use this code, instead:for /f "tokens=3" %%v in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Setup /v SourcePath') do set CDDRIVE=%%vSET CDROM=%CDDRIVE:~0,2%This sets the CD letter to I: (on my own PC). I: is the CD-ROM where I'm installing Windows from.2- I have cmdlines.txt located as $OEM$\cmdlines.txt which launches UserAdd.cmd.UserAdd.cmd, in turn, runs CDLMROEX.INF! CDLMROEX.INF is located as $OEM$\CDLMROEX.INF!CDLMROEX.INF imports some reg keys to: HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnceExThose reg keys should run 12 packages immediately after the first logon, via RunOnceEx.These packages are located in $OEM$\CDLMROEX. So, they will be run right from my UACD.SET CDROM=%~d0 does NOT fit here!I use the %01% variable inside CDLMROEX.INF to read/write the path to CDLMROEX.INF!Hence, MSN Messenger (for example) will be run as "I:\$OEM$\CDLMROEX\MSN75\MSN75.msi /qn".This means that %01% was resolved to I:\$OEM$.3- I have ImgBurn.js located as $OEM$\CDLMROEX\ImgBurn\ImgBurn.js!ImgBurn.js launches ImgBurn.exe from $OEM$\CDLMROEX\ImgBurn\ImgBurn.exe!It runs ImgBurn.exe with its silent switch then sends some keys.For ImgBurn.js to launch ImgBurn.exe from the UACD, I use this code:var path = WScript.ScriptFullName;//getting the full path to this script.var CDROM = path.substring(0,2);//extracting CD letter from the full path.WshShell.RUN (CDROM + "\\$OEM$\\CDLMROEX\\ImgBurn\\ImgBurn.exe /S");//running exe.SET CDROM=%~d0 does NOT fit here!4- I've got a CD-ROM and CD-RW. The CD-ROM was broken down (idle).I received an error when I used this code: FOR %%i IN (D <-> Z) DO IF EXIST %%i:\CD1.txt SET CDROM=%%iThis happened because that code searches ALL drives for CD1.txt (or whatever file).It does not stop the search process when it finds the file.Since my idle CD-ROM wasn't disconnected, the error occurred and the batch file stopped.It was the last time I used that code to get the CD letter.SET CDROM=%~d0 does NOT fit here because the batch file was fired from my HDD.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now