Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account



Photo

BREAKTHROUGH! Accepting user input in batch files

- - - - -

  • Please log in to reply
22 replies to this topic

#1
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
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, 20 October 2004 - 05:38 AM.



How to remove advertisement from MSFN

#2
EOTB

EOTB

    Newbie

  • Member
  • 10 posts
  • Joined 23-April 04
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.

#3
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
@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

#4
Jono

Jono

    Member

  • Member
  • PipPip
  • 284 posts
  • Joined 27-September 03
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.
<span style='font-size:14pt;line-height:100%'>Jono</span>

#5
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
You may want to read the following post that describes the advantages of a DOS-based install:

http://www.msfn.org/...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

#6
EOTB

EOTB

    Newbie

  • Member
  • 10 posts
  • Joined 23-April 04

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

#7
EOTB

EOTB

    Newbie

  • Member
  • 10 posts
  • Joined 23-April 04

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.

#8
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
@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

#9
EOTB

EOTB

    Newbie

  • Member
  • 10 posts
  • Joined 23-April 04
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.

#10
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
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

#11
[BM]Crusher

[BM]Crusher

    Got milk?

  • Member
  • PipPipPipPip
  • 570 posts
  • Joined 05-April 04
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 
COPY CON USRINPUT.TMP
ECHO 
CLS
ECHO The Computer Name you typed in was:
TYPE USRINPUT.TMP

The line "ECHO ' turns on ANSI key stroke checking, then the line "ECHO " 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=1
MEM /C | FIND "ANSI" > NUL
IF ERRORLEVEL 1 SET ANSI=0
IF "%ANSI%"=="1" ECHO 
IF "%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.TMP
IF "%ANSI%"=="0" CLS
IF "%ANSI%"=="1" ECHO 
ECHO.>> %TEMP%.\~USRINP.TMP
ECHO.>> %TEMP%.\~USRINP.TMP
TYPE %TEMP%.\~USRINP.TMP | DATE | FIND "):" > %TEMP%.\~USRINP.BAT
CALL %TEMP%.\~USRINP.BAT
IF "%ANSI%"=="0" ECHO You typed: %USRINPUT%
IF "%ANSI%"=="1" ECHO You typed: %USRINPUT%
ECHO.
PAUSE
FOR %%A IN (%TEMP%.\~USRINP.BAT %TEMP%.\~USRINP.TMP VOER.BAT TYP.BAT CURRENT.BAT) DO DEL %%A
SET ANSI=

Under windows 2000 and XP, the following should work as well:
:: Only one single command line is needed to receive user input
FOR /F "tokens=*" %%A IN ('TYPE CON') DO SET INPUT=%%A
:: Use quotes if you want to display redirection characters as well
ECHO 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 input
FOR /F "tokens=*" %%A IN ('MORE') DO SET INPUT=%%A
:: Use quotes if you want to display redirection characters as well
ECHO You typed: "%INPUT%"
now we only have to press F6 and ENTER...
SET ANSI=1
MEM /C | FIND "ANSI" > NUL
IF ERRORLEVEL 1 SET ANSI=0
IF "%ANSI%"=="1" ECHO 
IF "%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=%%A
IF "%ANSI%"=="1" ECHO 
ECHO 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...

#12
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
@[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

#13
[BM]Crusher

[BM]Crusher

    Got milk?

  • Member
  • PipPipPipPip
  • 570 posts
  • Joined 05-April 04
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 :)

#14
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
@[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

#15
RayOK

RayOK

    Member

  • Member
  • PipPip
  • 152 posts
  • Joined 21-September 03
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.sif
ECHO [Data] >> winnt.sif
ECHO     AutoPartition=0 >> winnt.sif
ECHO     MsDosInitiated="0" >> winnt.sif
ECHO     UnattendedInstall="Yes" >> winnt.sif
ECHO. >> winnt.sif
ECHO [Unattended] >> winnt.sif
ECHO     UnattendMode=FullUnattended >> winnt.sif
ECHO     OemSkipEula=Yes >> winnt.sif
ECHO     OemPreinstall=Yes >> winnt.sif
ECHO     TargetPath=WinXP >> winnt.sif
ECHO     UnattendSwitch="yes" >> winnt.sif
ECHO     FileSystem=* >> winnt.sif
ECHO     DriverSigningPolicy=Ignore >> winnt.sif
ECHO     OemPnPDriversPath="%driver%" >> winnt.sif
ECHO. >> 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

:)

#16
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
OK, after a bit more searching, I've come across exactly what we're looking for in a 1K freeware DOS utility - INPUT.COM by Horst Schaeffer

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

You'll find INPUT.COM in the PBAT sction on the left. The syntax is painfully simple:

INPUT "Enter phrase here: " variablename

Will prompt the user to enter any alphanumeric string (words, spaces, etc.) and will save it in variablename after pressing enter. Sheesh, and I thought I was coming up with something new. :)

Folks, this will solve your input problems. But at least Adam got some good code out of it. Ignore my CHOICE code, I am going to use INPUT from now on.

Sigh,

JP

#17
ZoSTeR

ZoSTeR

    Member

  • Member
  • PipPip
  • 227 posts
  • Joined 04-November 03
Hmm why don't you use the /p switch of the set command?

set /P disksize="Please enter the desired size of C: in MB: "


#18
RayOK

RayOK

    Member

  • Member
  • PipPip
  • 152 posts
  • Joined 21-September 03
getvar does the exact same thing as input.com.. haven't compared the two so maybe input.com may be a little smaller and user friendly.

I'm pretty sure set /p only works in Windows 2000 and XP with cmd.exe, not in a older, pure DOS environment, with command.com..

#19
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
@Zoster,

Thanks, but the SET /P doesn't work in DOS - that's what I was shooting for.

@RayOK

Yes, I think GETVAR is even made by the same guy (I saw it on his website as well), but it's alot larger than INPUT. I'm putting INPUT on a boot floppy with lots of other tools, so size matters.

I wonder what the WinME version of COMMAND.com would buy us, in terms of additional features. Anyone know?

JP

#20
RayOK

RayOK

    Member

  • Member
  • PipPip
  • 152 posts
  • Joined 21-September 03
Personally I would scrap MSDOS and use FreeDOS. Free and much more powerful.
FreeDOS

#21
Denney

Denney

    *shrug*

  • Member
  • PipPipPipPip
  • 685 posts
  • Joined 11-September 03
I have a problem with the INPUT.COM program.

Here's my current test batch file:

@echo off
Utilities\INPUT "Enter a new administrator name: " newadmin
echo %newadmin%
pause

Now, when I run this, I get this output:

Enter a new administrator name: SomeAdmin
ECHO is off.
Press any key to continue

as you can see, it's not actually setting the variable. Even if I set the variable beforehand, it just retains it's old value. Can anyone help me with this?

Edit: Never mind.. just say this: Note: INPUT does not work under Win NT/2000/XP

#22
JPamplin

JPamplin

    format c: /u - It is your desssssstiny.

  • Member
  • PipPip
  • 143 posts
  • Joined 06-January 04
Folks,

You will notice that I've rewritten my original post to include the INPUT.COM user input utility and simplified my script quite a bit.

JP

#23
Dj-Throll

Dj-Throll

    Newbie

  • Member
  • 10 posts
  • Joined 28-June 04
Hello.

How can I put onto a boot cd ?

Please help me.... I'm struggeling.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users