Jump to content

Batch Script Tips and Tricks for XPCDs


Recommended Posts

7. Call MapCD (sets %CDROM% Globally for other CmdLines.txt scripts)

I may be wrong here but unless you set a permanent environment variable, due to the way each child cmd process automatically cleans-up on closing, your variable will only last until that specific window is closed.

I'm not sure about permanent environment variables not staying set, but you could find the CD-ROM drive, (with CD loaded), and set it as a system variable with WMIC all at once.

@echo off
for /f "usebackq skip=1" %%a in (
 `wmic cdrom where "MediaLoaded='TRUE'" get drive ^2^>nul`
) do (
 if errorlevel 0 (
   wmic environment create Name="CDROM",UserName="<SYSTEM>",VariableValue="%%a">nul
 )
)
goto :eof

Link to comment
Share on other sites


Here a VBS script that changes the working directory.

Dim Act , CD, Fso, Ts : Set Act = CreateObject("WScript.Shell")

CD = Act.CurrentDirectory

  Set Fso = CreateObject("Scripting.FileSystemObject")

  Set Ts = Fso.OpenTextFile("TestChangeDir.txt", 2,True)

      Ts.writeline Space(5) & "Working Directory Before Change : " & CD

      Ts.Close

    Act.Run("TestChangeDir.txt"),1,True

    Fso.DeleteFile("TestChangeDir.txt")

  Set Ts = Fso.OpenTextFile("TestChangeDir.txt", 2,True)

      CD = "C:\"

      Ts.writeline Space(5) & "Working Directory After Change  : " & CD

      Ts.close

  Act.Run("TestChangeDir.txt"),1,True

  Fso.DeleteFile("TestChangeDir.txt")

Link to comment
Share on other sites

I've been trying to figure out how to have WPI check if a registry key/value exists and if it doesn't exist run a .reg file. I've had no luck so far :( since WPI is JavaScript based I'm looking for a way to do it in JavaScript but if anyone knows another way that would be great also.

Link to comment
Share on other sites

To the best of my knowledge, WPI is just a GUI for installing applications, it wasn't intended as a scripting interface.

To read a registry key in js you would use something like this:

var wsh = new ActiveXObject("WScript.Shell");
var key = "HKLM\\SOFTWARE\\Classes\\MadeUpKey";
wsh.RegRead(key)

You would then need to be able to 'trap' the error messages in order to perform the run commands you need etc.

<Edit>

However to do what you want in cmd /batch

REG QUERY "KeyName" /v "ValueName">nul 2>&1||regedit /s MyRegs.reg

Just input your own KeyName and Optionally ValueName, and of course change the name of the reg file to suit.

</Edit>

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

@Yzöwl

As to WMIC, I just typed in that command at my command line, and Windows had to install it first. I'm not too fond of that. Also, this technique goes back to the issue of having multiple optical drives. But in this case the issue would be if the user has media in more than one optical drive, two drive letters will be returned by WMIC. So you are back to looking for check files again to verify which media is the correct one. this is less risky than the For-In-Do Check file or label technique, but there is no such risks when using the Set CDROM=%~d0 technique (which to me appears faster as well).

7. Call MapCD (sets %CDROM% Globally for other CmdLines.txt scripts)

I may be wrong here but unless you set a permanent environment variable, due to the way each child cmd process automatically cleans-up on closing, your variable will only last until that specific window is closed.

You are correct, 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.

My personal process was actually calling MakeMapCD.cmd from my first CmdLines.txt script (not CmdLines.txt as I've prescribed here for everyone). This is why MakeMapCD v1.1 contained a call to MapCD). When I posted MakeMapCD.cmd v1.1 for others to use, I should've cleaned the Call MapCD.cmd out of it. So all along I've been discussing with you on the basis of what my personal process was doing, not what the posted version of it should be doing. Sorry for the confusion! :crazy: I've since modified my own process to be the same as the one I've posted. And now, I agree that Setlocal can go at the top of MakeMapCD.cmd. (Life is so much more clear when talking apples-to-apples).

@Everyone

I've updated MakeMapCD.zip download accordingly on the first post in thread (now v1.1.1). The changes I've made 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).

The Zip includes a sample CmdLines.txt and SampleScript.cmd that I just tested on Virtual PC. SampleScript simply stops at T-12 and Echo's the %CDROM% value to the screen.

[Edit] Here's a link[/Edit]

MakeMapCD.zip

Edited by DarkShadows
Link to comment
Share on other sites

Also, this technique goes back to the issue of having multiple optical drives.  But in this case the issue would be if the user has media in more than one optical drive, two drive letters will be returned by WMIC.  So you are back to looking for check files again to verify which media is the correct one.  this is less risky than the For-In-Do Check file or label technique, but there is no such risks when using the Set CDROM=%~d0 technique (which to me appears faster as well).
If I was likely to have more than one disk loaded, I wouldn't recommend the MediaLoaded method, but the VolumeName, thus negating that posibility. The example I gave only really used the WMIC method of getting the CD-ROM drive letter, because I wanted to show the thread readers a method of adding a new permanent system variable, which had never been shown before. As you said it was something which didn't always stick, I gave it as a valid alternative for XP+ systems.
Link to comment
Share on other sites

I wanted to show the thread readers a method of adding a new permanent system variable, which had never been shown before.

What you posted was new to me--I learned something today! :o

Which specific part of that script makes the variable permanent?

<Edit> Can we combine these techniques (Set CDROM=%~d0 and the permanent variable portion of what you posted)?

Is there a way to unset a permanet variable?

</Edit>

Edited by DarkShadows
Link to comment
Share on other sites

@ DarkShadows

wmic environment create Name="CDROM",UserName="<SYSTEM>",VariableValue="%%a">nul

where%%a is the returned drive letter variable.

Doing it automatically adds the following info to the registry e.g.

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment]
"CDROM"="H:"

Obviously with the correct drive letter! and it's also added to the system variables section of the environment variables window, from system properties » advanced.

To later remove the variable, if you so wish, use:

wmic environment where Name="CDROM" delete>nul

<Edit>

Using a mixture of both codes, this would do it

@echo off&setlocal enableextensions
wmic environment create Name="CDROM",UserName="<SYSTEM>",VariableValue="%~d0">nul
endlocal&goto :eof

</Edit>

<Edit 2>

Just changed some wording hopefully to help clarify some stuff

</Edit 2>

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

@Yzöwl Good Stuff! :thumbup

@Everyone

I answered a question in another thread that relates to creating/programming Windows NT command scripts. Go check it out. (and tell me what I'm missing) I'm prettry sure that works (it's been so long since I did it though).

How to Add New Windows NT Command Script to File - New Menu

Link to comment
Share on other sites

Using a mixture of both codes, this would do it
@echo off&setlocal enableextensions
wmic environment create Name="CDROM",UserName="<SYSTEM>",VariableValue="%~d0">nul
endlocal&goto :eof

NOW we're talking!

My only question - how do i use this? is it still %CDROM% - as in:

start /wait "%CDROM%\firefox\firefox.exe" ??

Edited by a06lp
Link to comment
Share on other sites

Yes it should be %CDROM% the same way that %SYSTEMDRIVE% etc. works

This would have to be tested, as I am aware of issues with setting environment variables using some methods, but it not being usable in the current session without a reboot. I am also unaware as to the earliest point in the UA setup, when the WMIC stuff will work.

Link to comment
Share on other sites

I use vbs scripts to set my cd varibles, I am also working on a vbs script

that will install the after XP is installed script. This will not need any path

to apps as the vbs file builds the path.

My Test Script, the only error i have found with this is it reports back

that this one BitTorrent-4.0.1.exe /s /v/qn is

not there. This is a not correct, I am working on a fix for that problem.

This is one line of code using this &_

to make it on 2 lines.

Blue Text Is The Array For Apps

With These Switches. The Array Is Separated

by These ,_

Save As NoPathinstall.Vbs

Const ForReading = 1, ForWriting = 2,ForAppending = 8

Dim ColFiles, objWMI, StrComputer

StrComputer = "."

Dim Act : Set Act = CreateObject("Wscript.shell")

Dim Fso :Set Fso = CreateObject("Scripting.FileSystemObject")

'''' ARRAY FOR APPS AND SWITCHES

Dim Test : Test = Array(_

"TweakUi.exe /passive /qr ",_

"NfoReader.msi /QR",_

"BitTorrent-4.0.1.exe /s /v/qn" ,_

"Test.exe Some Switch Here" ,_

"345.msi Some Switch Here" ,_

"345.exe Some Switch Here" ,_

"Hello.msi Some Switch Here" ,_

"hello.exe Some Switch Here" ,_

"Winamp5.msi Xname=Spad Xkey=XXXXX-XXXXX-XXXXX-XXXXX Xagent=1 Xlibrary=0 Xintex=1 "  &_

"Xmodernskin=1 Xaudio=1 Xvideo=0 Xvisual=1 Xextra=1 Xregopt=0 /qr" )

Dim FinishTime, MyT, StartTime : StartTime = Timer : MyT = Time()

Dim Ts 

Set Ts = Fso.OpenTextFile("UaPostInstall.txt", ForWriting, True)

Ts.WriteLine Space(3) & time() & "<----- BEGIN SPLIT TEST ----->" & Space(3) & Date() & vbCrLf

Function UaPost

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' START THE SEARCH FOR THE UA APPS

For Each strtest In Test

CTest = strtest

App = Split(CTest, ".")

App(1) = Left(App(1), 3)

  '''' BUILDS THE PATH TO THE APP

  Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

  Set ColFiles = objWMI.ExecQuery("Select * from CIM_DataFile Where FileName = '" & App(0) & "' AND Extension = '" & App(1) & "'")

  '''' IF THE SOFTWARE IS NOT FOUND

  If ColFiles.count = 0 Then

  Ts.WriteLine "Cannot Find This App" & Space(3) & App(0) & "." & App(1)

  Else

  End if

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' TEMPLATE TO INSTALL UA APPS

  For Each objFile in colFiles

  '''' Extra Varibles If Needed

  FName = Ucase(objFile.FileName)

  Kbname = Ucase(objFile.FileName & "." & objFile.Extension)

  KbPath = UCase(objFile.Drive & objFile.Path)

  TheFile = KbPath & Kbname

    If Fso.FileExists(TheFile) Then

      Ts.WriteLine  "<------------------------------>" & vbCrLf &_

      "Act.Run(""" & KbPath & strtest & """)" & vbCrLf & "<------------------------------>"

      Exit For

      End If

    Next

  Next

  FinishTime = Timer

  Dim Sp3 : Sp3 = Space(3)

  Dim EndTime : EndTime = Timer

  Dim E_Time : E_Time = EndTime - StartTime

    E_Time = E_Time / 60

    E_Time = Left(E_Time,4)

    Ts.WriteLine V & "<-------------------- TEST TIME RESULTS ------------------------->"

    Ts.WriteLine Sp3 & "The Start Time : " & MyT & V & Sp3 & "The End Time : " & Time() & V &_

    Sp3 & "Time Amount Of Time The Script Ran = " & E_Time & " Minutes" & V

    Ts.Close

    The_End

    Exit Function

  End Function

  Function The_End

        Act.Run("UaPostInstall.txt"),1,True

      GSM = Act.Popup ("Did You Want To Keep This File" & vbCrLf &_

      "Yes To Keep The File" & vbCrLf &  "No To Delete The File" & vbCrLf &_

      "Is Nothing Is Selected" & vbCrLf & "After 15 Seconds, Then" & vbCrLf &_

      "It Will Delete The File", 15, "Keeo Or Delete", 4 + 32)

    If GSM = Vbyes Then

      Exit Function

    End If

    If GSM = Vbno Then

      Fso.DeleteFile("UaPostInstall.txt")

      Exit Function

    End If

    If GSM = -1 Then

      Fso.DeleteFile("UaPostInstall.txt") 

      Exit function

    End If

  End Function

  UaPost

What It Reports
  4:39:40 PM<----- BEGIN SPLIT TEST ----->  9/25/2005

<------------------------------>

Act.Run("F:\XSP1\I386\$OEM$\$1\INSTALL\TWEAK\TweakUi.exe /passive /qr ")

<------------------------------>

<------------------------------>

Act.Run("F:\XSP1\I386\$OEM$\$1\INSTALL\NFOREADER\NfoReader.msi /QR")

<------------------------------>

Cannot Find This App  BitTorrent-4.0

<------------------------------>

Act.Run("H:\EDFOLDER\Test.exe Some Switch Here")

<------------------------------>

Cannot Find This App  345.msi

Cannot Find This App  345.exe

Cannot Find This App  Hello.msi

<------------------------------>

Act.Run("F:\DOCUMENTS AND SETTINGS\GUNSMOKINGMAN\DESKTOP\UASCRIPTS\EDDIE-MAKEEXE\MYEXE-COMPLETED\hello.exe Some Switch Here")

<------------------------------>

<------------------------------>

Act.Run("F:\XSP1\I386\$OEM$\$1\INSTALL\WINAMP\Winamp5.msi Xname=Spad Xkey=XXXXX-XXXXX-XXXXX-XXXXX Xagent=1 Xlibrary=0 Xintex=1 Xmodernskin=1 Xaudio=1 Xvideo=0 Xvisual=1 Xextra=1 Xregopt=0 /qr")

<------------------------------>

<-------------------- TEST TIME RESULTS ------------------------->

  The Start Time : 4:39:40 PM  The End Time : 4:42:05 PM  Time Amount Of Time The Script Ran = 2.41 Minutes

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