Jump to content

Batch Script Tips and Tricks for XPCDs


Recommended Posts

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

Edited by gunsmokingman
Link to comment
Share on other sites


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

Link to comment
Share on other sites

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%

Link to comment
Share on other sites

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

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).

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%

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.

Link to comment
Share on other sites

@ 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>

Edited by Yzöwl
Link to comment
Share on other sites

I am using this:

@echo off
For /f "usebackq tokens=1,2 delims==" %%i IN (`WMIC CDROM Where "VolumeName='UACDROM'" list full`) DO If %%i EQU Drive Set strCdrom=%%j

It will search for cd label UACDROM and assign it to variable strCdrom

Link to comment
Share on other sites

Thanks Yzöwl. Appreciate it.

Here's some additional info - fsutil is only available on XP/2003. 2000 does not recognize this command.

@ DarkShadows

Good explanation.

set CDROM=%~d0 is very simplistic and can be quite useful like you said.

But a warning if you use Quick Batch File Compiler to compile your scripts -- set CDROM=%~d0 will not give you the correct drive. However, Yzöwl method will.

Thanks y'all. :)

Link to comment
Share on other sites

Here is a basic template utilising my CD Label method (XP /2003)

@echo off&setlocal enableextensions

:: enter your CD label below e.g. set lab=UWXPCD (case insensitive)
set lab=

call :findrive

:: put your commands below here (%CDROM% variable is set)


:: do not add or replace anything below here
endlocal&goto :eof
:findrive
set drv=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 %%a in (%drv%) do (
fsutil fsinfo volumeinfo %%a:|find /i "%lab%">nul 2>&1&&set CDROM=%%a:
)
goto :eof

Hope this simplifies it for everyone!

Edited by Yzöwl
Link to comment
Share on other sites

But a warning if you use Quick Batch File Compiler to compile your scripts -- set CDROM=%~d0 will not give you the correct drive. However, Yzöwl method will.

@vcBlackBox: I never compile my batch scripts, so this isn't an issue for me. However, this is really good information for people to know. Thanks for bringing it forward!
fsutil fsinfo volumeinfo %%a:|find /i "%lab%">nul 2>&1&&set CDROM=%%a:

@Yzöwl: I like this script to automatically find my unslipstreamed Windows XP CD when I'm running my slipstream.cmd. (This is an example scenario when using Set CDROM=%~d0 will simply not work.)

@Yzöwl and/or Martin Zugec:

Now I have a question. In the line above, I understand nearly every technique you two have applied in your code there except one segment. The "">nul 2>&1&&set" Portion.

The ">nul" just redirects output to the Nul device correct? So how does the remaining portion of the code get executed?

The "&" is usually used to concatenate another command to the command line. But why do you have two of them before your Set command?

It's a little off topic, but could you break down this a little?

Link to comment
Share on other sites

@ DarkShadows

It redirects standard output, STDOUT, (1>nul), and standard error, STDERR, (2>nul), to nul.

STDOUT, channel 1, is the default therefore doesn't require the channel number stipulating, (>nul).

You can use

  • >nul 2>nul

which will redirect each channel to its own nul

Or you can use

  • >nul 2>&1

which redirects STDERR to the same nul as the STDOUT channel.

As for the (&), (&&)

  • & separates multiple commands on one command line.

  • && causes the command following this symbol to run if the command preceding the symbol is successful.

Link to comment
Share on other sites

Here is a 1 line Vbs script that deletes a file called test.vbs if the test.vbs is not there then it will say missing file it a long line of code

Green is the file you can change it to txt, cmd or what ever

Tested This Script From My Desktop And It was checking System drive That Is why the File Name Looks Like this "\Test.vbs"

This lines runs from 0 - 192 in Notepad

  If CreateObject("Scripting.FileSystemObject").FileExists("\Test.vbs") Then CreateObject("Scripting.FileSystemObject").DeleteFile("\Test.vbs") Else MsgBox "Missing", 0 + 32,"Missing" End If
This Is the same code In 2 Lines

The Longest Line runs from 0 - 113 in Notepad

Set Fso = CreateObject("Scripting.FileSystemObject")

If Fso.FileExists("\Test.vbs") Then Fso.DeleteFile("\Test.vbs") Else MsgBox "Missing", 0 + 32,"Missing" End If

This Is The Biggest at 7 lines it reports the file been deleted or if it missing

The Longest Line runs from 0 - 98 in Notepad

  Set Fso = CreateObject("Scripting.FileSystemObject")

  Mlist = "\Test.vbs"

Function DelMlist

Fso.DeleteFile(Mlist)

MsgBox "The File Been Deleted", 0 + 32,"Gsm Test"

End Function

If Fso.FileExists(Mlist) Then DelMlist() Else Gsm = MsgBox ("Missing", 0 + 32,"Missing") End If

Link to comment
Share on other sites

Had to add this cause I needed it and had forgotten so I searched far and wide

If you want to answer a prompt AUTOmatically as I need occasionally.

echo y| cacls <filename> /g <username>:<permission>  

NOTE: There is no space between the y and the | (pipe). Works with some things and not with others so TEST it.

Link to comment
Share on other sites

Here is a .vbs script that I wrote to allow users to change their User Name, Company, and Computer Name. Usefull if you want to change it without going into the Registry or something that you can run at the end of your XP installation during Cleanup.cmd

Option Explicit

Set ws = WScript.CreateObject("WScript.Shell")
Dim ws, t, p1, p2, n, g, cn, cg, o, co

p1 = "HKLM\Software\Microsoft\Windows NT\CurrentVersion\"
p2 = "HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName\"

n = ws.RegRead(p1 & "RegisteredOwner")
g = ws.RegRead(p1 & "RegisteredOrganization")
o = ws.RegRead(p2 & "ComputerName")
t = "Change Personal Info & Computer Name"
cn = InputBox("Enter Your Full Name", t, n)
If cn <> "" Then
 ws.RegWrite p1 & "RegisteredOwner", cn
End If

cg = InputBox("Enter Your Company Name", t, g)
If cg <> "" Then
 ws.RegWrite p1 & "RegisteredOrganization", cg
End If

co = InputBox("Enter Computer Name", t, o)
If co <> "" Then
 ws.RegWrite p2 & "ComputerName", co
End If

Edited by Gee
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...