Jump to content

BREAKTHROUGH! Accepting user input in batch files


Recommended Posts

Folks,

This is for the "setup XP in DOS" crowd out there. Many of you who have seen my previous posts know that my specialty is creating DOS-based unattended installers, and this is a HUGE deal to those of us who do it that way. Especially those who need to enter some user input and incorporate it into a batch process in DOS.

The first time I composed this post, I had no idea that tiny DOS utilities were available that would accept user input in the middle of a batch file, then use the input as a variable. Since that time, I have found the wonderful INPUT.COM by Horst Schaeffer and have gone back and rewrote the post:

http://home.mnet-online.de/horst.muc/

You'll find INPUT.COM in the PBAT section on the left of the page - it's one of many DOS utilities. The syntax is painfully simple:

INPUT "Enter phrase here: " variablename

EDIT: You can also download it from my personal website:

http://www.seteq.com/files/INPUT.COM

Will prompt the user to enter any alphanumeric string (words, spaces, etc.) and will save it in variablename after pressing enter.

First, the code. This DOS batch file uses the newer (>64GB) FDISK, AEFDISK, INPUT and CHOICE all in the same location as the batch file. You could just use AEFDISK or FDISK, but I choose to use both for maximum compatibility.

It deletes all partitions on the hard disk for you, then asks you to specify the desired size of the C: drive. It then creates and formats the C: drive using your entered size and uses the remaining space to create and format a D: drive, then auto-reboots. Pretty slick for DOS, I think.

@echo off

goto delete

:delete

CLS

ECHO WARNING!  You are about to delete all partitions and recreate a new

ECHO C: and D: drive.  All data will be lost!  Are you sure you want this?

ECHO.

CHOICE /C:YN %1

IF ERRORLEVEL 2 GOTO QUIT

@FDISK /MBR

@AEFDISK /DELALL

GOTO START

:start

set csize=

@AEFDISK /ALLSIZE

goto specifyc

:specifyc

CLS

ECHO.

ECHO.

ECHO Create custom C: and D: partitions and reboot

ECHO ---------------------------------------------

ECHO.

ECHO Total hard disk capacity is %allsize% MB

ECHO.

INPUT "  Please enter the desired size of C: in MB: " csize

ECHO.

:format

ECHO.

ECHO.

ECHO Total hard disk capacity is %allsize% MB

ECHO.

echo You entered %csize% MB for the C: drive.  Is this correct?

ECHO.

CHOICE /C:YN %3

IF ERRORLEVEL 2 GOTO START

ECHO.

ECHO Creating a %csize% MB C: drive

@AEFDISK 1 /PRI:%csize%:0B

@AEFDISK 1 /FREESIZE

ECHO.

ECHO Creating D: drive from remaining free space (%freesize% MB) ...

@AEFDISK 1 /EXT:%freesize% /log:%freesize%:0B

ECHO.

@AEFDISK /formatfat:1:Hard_Disk_C

@AEFDISK /formatfat:5:Hard_Disk_D

ECHO.

ECHO Done!  PC will reboot to use the new C: and D: drives.

PAUSE

@AEFDISK /reboot

:QUIT

ECHO.

This batch script is much simpler than the CHOICE loop I had designed before. You can also use INPUT to save usernames, XP keys, etc. and insert them into WINNT.SIF or whatever. I'm working on that now.

Reply to this post with any questions or comments. Hopefully this will do someone else out there as much good as it is now doing for me. I'll be happy to repost a copy of AEFDISK, INPUT, and my code if anyone wants it. Just let me know.

JP

Edited by JPamplin
Link to comment
Share on other sites


This is quite cool! I assume it would would for text (by changing the code) also?

I am attempting to create a script to Fdisk, Format and then install Windows XP in one fell-swoop by using a Bootable CD, rather than a floppy disk.

I have become stuck when trying to figure out how to automatically inform the script that the AEFdisk and format has taken place after the needed reboot and to automatically start the Windows installation script.

I will be installing Windows XP on systems that have no partitions (eg brand new disks), FAT32 and NTFS.

Any ideas?

Cheers

Adam.

Link to comment
Share on other sites

@EOTB...

Yes, it would work fine for alpha characters - you would just need to do something like:

:inputloop

CLS

ECHO Enter some text, press ` to accept: %userinput%

CHOICE /C:abcdefghijklmnopqrstuvwxyz` %1

IF ERRORLEVEL 25 GOTO RESULT

IF ERRORLEVEL 24 GOTO ADD-Z

IF ERRORLEVEL 23 GOTO ADD-Y

...

:addz

set userinput=%userinput%z

goto inputloop

, etc.

I think there is a way to check whether the C: drive has been formatted, assuming the disk starts off completely blank - just off the top of my head, I would do it this way:

IF EXISTS c:\nul (goto install script)

@AEFDISK 1 /PRI:...

If the Hard Disk is completely empty, then c:\nul would not return true, so it would skip the install command and go format the C: drive. But if C: IS there, then you skip to your install batch and you're done.

I think that would work. That's the way I would do it (and I would also format all volumes as FAT32 starting out, then do a ConvertNTFS in the WINNT.SIF, but that's me starting out of DOS).

Give it a shot,

JP

Link to comment
Share on other sites

why format the hard drive before installing windows :) the xp install takes care of that :rolleyes: now i am intrested in a dos menu system which would edit my winnt.sif with a little user input such as computername and end user name etc then launch setup but these are being designed to run under winpe which will do for me since i use it as a recovery enviroment it has now become my deployment enviroment as well.

Link to comment
Share on other sites

You may want to read the following post that describes the advantages of a DOS-based install:

http://www.msfn.org/board/index.php?showtopic=17144

I prefer pre-formatting drives into 2 parts for several reasons. First, setting up a C: and D: partition, copying all install files to D:, and running your entire install from D: means you can pop out the CD after the initial file copy (a few minutes). Everything else is done from the HD and it's much faster.

Another advantage of having a D: to install from is that you (or your CD) don't have to be around to rebuild the PC again later. Everything you need is on the D: drive, ready to reinstall with a boot floppy and simple batch scripts.

Another advantage of my approach is that it's VERY fast and small. Booting from a DOS floppy image on a CD takes all of 5 seconds, and the image only takes up 1.44MB of space. The rest of the space is for the archive of XP and all my applications - my one CD has XP, Office, Nero, Norton, and lots of additional stuff on a single image.

Lastly, the user can back up his /her files onto D:, blow away the NTFS C: drive, reformat C: as FAT32, reinstall XP, and reconvert back to NTFS all with a boot floppy. That makes support much easier, and I'd like to see the XP install CD do that.

Now, to your second issue: It would be possible to capture a text string using my approach and append those strings into a WINNT.SIF file (onto a floppy perhaps, since you don't prefer your drives pre-formatted).

You would have to write the beginning of the file, then append the custom strings, then append the bottom chunk of the file. I could do that in DOS, but I can't tell you how to script it in WinPE.

Oh, wait, I guess that's another advantage, isn't it? ;-)

JP

Link to comment
Share on other sites

IF EXISTS c:\nul (goto install script)

@AEFDISK 1 /PRI:...

If the Hard Disk is completely empty, then c:\nul would not return true, so it would skip the install command and go format the C: drive. But if C: IS there, then you skip to your install batch and you're done.

Cheers for that it works a treat! I had totally forgotten about the c:\nul jobby!

C:\NUL is also found on a non-formatted partition!

Unfortunatly is does not solve the problem if there is already a FAT partition on the HD :) I could run the IF EXIST command and search for files which exist on various OSs, but there is always the chance of overlooking on, which would mean that it would not Fdisk and try to format to existing partiton instead.

Any other ideas

Cheers

Adam

Link to comment
Share on other sites

why format the hard drive before installing windows  the xp install takes care of that

I am installing from DOS from a network share. I have to fdisk/format the drive, copy the i386 directory over to the c:\ (otherwise it cannot find some files later in the install) and then run winnt from the c:\.

Do you know of a way to run the install from a network share, and use the answer file to format the drive an copy the entire i386 folder to the c:\??

Cheers

Adam.

Link to comment
Share on other sites

@EOTB

OK, well that's a little different from your earlier statement, "I will be installing Windows XP on systems that have no partitions (eg brand new disks), FAT32 and NTFS." The C:\nul condition would work fine on an empty disk, but you're right, an existing partition would break that logic.

Let me see if I have this straight:

You are trying to create a single batch file which will handle both an empty hard disk, and a hard disk where there has been a prior operating system installed, and have that batch file automatically delete all partitions, recreate new ones, reboot, copy over the XP I386 folder from a network share, and start the XP install from the local copy.

This batch file would be on a boot floppy image on a CD, so no files could be written to or changed on the floppy or during the batch process.

Is that what you're trying to accomplish?

If so, then perhaps you could take the approach of always blowing away existing partitions if ANY files exist on the C: drive, and creating a new blank FAT32 C: drive. Then after the reboot, if C: exists but is empty, then start the copy and install cycle.

I would assume you are rebuilding PCs with a previous copy of Windows on them. Are these NTFS, FAT32, or both? I'd need to know that to help you with an IF EXISTS condition.

JP

Link to comment
Share on other sites

JPamplin,

Your previous post was correct - that is what I am trying to accomplish

If so, then perhaps you could take the approach of always blowing away existing partitions if ANY files exist on the C: drive, and creating a new blank FAT32 C: drive. Then after the reboot, if C: exists but is empty, then start the copy and install cycle.
This is what I am doing. The problem is, on the reboot, how does the script know that the drive has been fdisked and/or formatted (using AEFdisk)? For example, if the script is run on a PC that has a C:\ partiton of say 500MB with no files on it, it would automatically try to format the partition and start the install cycle.

Basically, you need a condition in the script before the Fdisk command to see if the Fdisk has been run and created the partition.

I would assume you are rebuilding PCs with a previous copy of Windows on them. Are these NTFS, FAT32, or both? I'd need to know that to help you with an IF EXISTS condition.

I will not install Windows XP just Windows systems. I have to assume they will have random OS on them.

I would be quite happy to install on a smaller partition (eg 5GB) and use the extend partition and the convert to NTFS functions in the answer file - if this helps as I think you can use AEFisk to query the size of existing partitons?

Cheers

Adam.

Link to comment
Share on other sites

OK, Adam, I did some quick "back of the envelope" coding and I think I have a good batch file that will do everything unattended for you. This is assuming that the hard disks you are working with are either totally blank, or just formatted as one volume.

Here's the logic tree:

Test 1: Is drive empty?

This test uses AEFDisk's ALLSIZE (HD capacity) compared to FREESIZE (unallocated space) - if they are identical, the drive is empty.

If Yes - GOTO format subroutine, create a C: and D: drive, auto-reboot

If No - Check for Drive D: existence

Test 2: Does D: exist?

Again, this script assumes that your existing drives do not have a D: partition - only THIS script creates that, so if it's there, we know the HD is ready to install on.

If Yes - drive has been formatted using AEFDISK, so goto install script

If No - pre-existing C: drive exists, GOTO DeleteALL and remove all partitions, then goto formatCD and format C: and D: drives, and reboot.

So, here's the code:

@echo off

GOTO IsHDempty

:IsHDempty

@AEFDisk 1 /FREESIZE

@AEFDisk 1 /ALLSIZE

IF "%freesize%"=="%allsize%" GOTO formatCD

GOTO CheckForD

:CheckForD

IF EXISTS d:\nul GOTO install

GOTO deleteALL

:deleteALL

@AEFDisk 1 /DELALL

GOTO formatCD

:formatCD

(however you wish to define csize goes here)

@AEFDisk 1 /PRI:%csize%:0B

@AEFDisk 1 /FREESIZE

@AEFDisk 1 /EXT:%freesize% /LOG:%freesize%:0B

@AEFDisk 1 /formatfat:1:Hard_Disk_C

@AEFDisk 1 /formatfat:5:Hard_Disk_D /reboot

:install

ECHO.

ECHO Creating Install Folder and copying from source ...

ECHO.

IF NOT EXISTS d:\XP\nul (MD d:\XP)

xcopy (path to winxp CD)\*.* d:\XP\*.* /s /c /e /r /y

ECHO.

ECHO Starting Windows Install ...

ECHO.

d:\XP\i386\winnt /s:d:\XP\i386 /u:d:\XP\i386\winnt.sif

You'll need to add to it where indicated, and feel free to use my CHOICE code above. But this could work for you completely automated as well if you play around with it.

Let us know how it goes,

JP

Link to comment
Share on other sites

choice has been around forever... i was using it 10 years ago to make game-loading batch files :) hehehe... amazing what people will do for games...

i first used choice in a 400 line batch file to load doom2 that had a menu screen, then i had a 1200 line batch file to load quake and a 1400 line batch file to load duke nukem 3d (yes, i had too much free time)

unfortunately the choice command doesn't come with winxp, so it will have to be sourced from a Win9x or earlier installation.... it will work under 2k or xp, it just isn't included

there are another few methods that can be used to get user input through the command prompt:

If you have ANSI loaded, you can use something like this:

ECHO Enter your preferred Computer Name, and press Enter when ready . . .ECHO [13;0;64;13pCOPY CON USRINPUT.TMPECHO [13;13pCLSECHO The Computer Name you typed in was:TYPE USRINPUT.TMP

The line "ECHO [13;0;64;13p' turns on ANSI key stroke checking, then the line "ECHO [13;13p" turns it off....

If you don't have ANSI loaded, you have to press F6 and then Enter, here is a demonstration batch file:

This example checks for ANSI being loaded and assigns user input to a variable

SET USRINPUT=SET ANSI=1MEM /C | FIND "ANSI" > NULIF ERRORLEVEL 1 SET ANSI=0IF "%ANSI%"=="1" ECHO [13;0;64;13pIF "%ANSI%"=="0" ECHO Enter one word only, and press F6 followed by Enter . . .IF "%ANSI%"=="1" ECHO Enter one word only, and press Enter . . .COPY CON %TEMP%.\~USRINP.TMPIF "%ANSI%"=="0" CLSIF "%ANSI%"=="1" ECHO [13;13p[3A[K[1B[K[1B[K[2AECHO.>> %TEMP%.\~USRINP.TMPECHO.>> %TEMP%.\~USRINP.TMPTYPE %TEMP%.\~USRINP.TMP | DATE | FIND "):" > %TEMP%.\~USRINP.BATCALL %TEMP%.\~USRINP.BATIF "%ANSI%"=="0" ECHO You typed: %USRINPUT%IF "%ANSI%"=="1" ECHO You typed: [1m%USRINPUT%[0mECHO.PAUSEFOR %%A IN (%TEMP%.\~USRINP.BAT %TEMP%.\~USRINP.TMP VOER.BAT TYP.BAT CURRENT.BAT) DO DEL %%ASET ANSI=

Under windows 2000 and XP, the following should work as well:

:: Only one single command line is needed to receive user inputFOR /F "tokens=*" %%A IN ('TYPE CON') DO SET INPUT=%%A:: Use quotes if you want to display redirection characters as wellECHO You typed: "%INPUT%"

with that example, you have to press ENTER, F6, ENTER after typing in the information.... let's try make that better?

:: Only one single command line is needed to receive user inputFOR /F "tokens=*" %%A IN ('MORE') DO SET INPUT=%%A:: Use quotes if you want to display redirection characters as wellECHO You typed: "%INPUT%"

now we only have to press F6 and ENTER...

SET ANSI=1MEM /C | FIND "ANSI" > NULIF ERRORLEVEL 1 SET ANSI=0IF "%ANSI%"=="1" ECHO [13;0;64;13pIF "%ANSI%"=="0" ECHO Enter some text, and press F6 followed by Enter . . .IF "%ANSI%"=="1" ECHO Enter some text, and press Enter . . .FOR /F "tokens=*" %%A IN ('MORE') DO SET INPUT=%%AIF "%ANSI%"=="1" ECHO [13;13pECHO You typed: "%INPUT%"

OK, that one should allow you to capture user input strings and they should only have to Press ENTER (Not F6 first or anything) if ANSI is loaded...

Link to comment
Share on other sites

@[bM]Crusher

Thanks for that extra info and examples - I think that's what I was thinking of when I referred to other examples - the ANSI code is just too confusing in my head to be able to work with.

The whole point of the post was to come up with an easy way to capture input in a pure DOS (like, Win98 boot disk DOS) environment. And yes, CHOICE is an old tool, but I think I've come up with an easy to understand, quick to implement way to use it to do something that DOS does not.

I'll keep you're code snippets around, though, as they are very informative. Thanks again for the input.

JP

Link to comment
Share on other sites

choice only works good for a SET list of arguments....

with my method, you could have the batch file ask the user to type in their product key (rather from choosing from a list) for example... or their UserName... then you could create the WINNT.SIF based off this info (have a template where you can copy the files together, like 'copy winnt.1+cdkey+user+org+comp+winnt.2' where cdkey, user, org and comp are filenames created using the users inputs and winnt.1 and winnt.2 are the winnt.sif file split into 2 (you normally want userdata near the top of the winnt.sif just to make it look better))

i must admit though, choice is GREAT :)

Link to comment
Share on other sites

@[bM]Crusher,

Actually, the whole point of the original post was to show how you can use CHOICE to capture an entire string of characters and save them as a variable, not just choosing from a list of arguments.

I completely agree with your approach, I'm just trying to demonstrate a fairly simple way to append user input into a batch file that doesn't use complicated code to do it.

Hopefully the MSFN community can take both your code and my examples and create something functional out of them.

JP

Link to comment
Share on other sites

I have a little knowledge on batch files that maybe I can help..

For user input, either letters or line input I would use two simple programs named Getkey and Getvar. Similar to choice but much butter, less annoying, sets input to variables. They can be found here. He has other programs on his site that may also be useful.

For putting the info into winnt.sif maybe redirection pipes would be easier. Instead of outputting information on the screen use >> to append it to a text-based file. If the file doesn't exist, it will be created. For example:

say %drivers% is set to DRIVERS\VIDEO or whatever, based on input or previous commands.

ECHO;SetupMgrTag >> winnt.sifECHO [Data] >> winnt.sifECHO     AutoPartition=0 >> winnt.sifECHO     MsDosInitiated="0" >> winnt.sifECHO     UnattendedInstall="Yes" >> winnt.sifECHO. >> winnt.sifECHO [unattended] >> winnt.sifECHO     UnattendMode=FullUnattended >> winnt.sifECHO     OemSkipEula=Yes >> winnt.sifECHO     OemPreinstall=Yes >> winnt.sifECHO     TargetPath=WinXP >> winnt.sifECHO     UnattendSwitch="yes" >> winnt.sifECHO     FileSystem=* >> winnt.sifECHO     DriverSigningPolicy=Ignore >> winnt.sifECHO     OemPnPDriversPath="%driver%" >> winnt.sifECHO. >> winnt.sif

and so on.. Hope this gives someone more ideas. If not that, then maybe some of the links below will. They are various websites I've found about DOS programming..

FreeDOS (open-source DOS replacement, best)

Batch Scripting Site

MS DOS 6.22+ Refrence

Laura's DOS Batches

:)

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