File Input Methods for Batch Files ---------------------------------- The following batch sets the 'result' variable to the first line of a specified file. To avoid possibly nasty side effects like resetting the date and creating/overwriting files, make sure that the input file doesn't contain a valid date or redirection characters (< > or |). If enter.bat exists in the current dir it will be deleted. This routine is language-dependent, for non-English dos's change the "Enter" string and the name of enter.bat to match the first word of the date command prompt. This method has other limitations, it is only suitable if you either don't mind extra spaces at the end of 'result' or you know ahead of time how many words are present, trim the number of parms in the set result= line. Good for only six words, but on the bright side, you can easily select which words... @echo off copy %1 $tmp$.baf > nul :: set result to $tmp$.baf then delete >>$tmp$.baf echo. > $tmp$.bat type $tmp$.baf | date | find "Enter" :: (adds extra spaces, use only the parms needed) > enter.bat echo set result=%%4 %%5 %%6 %%7 %%8 %%9 call $tmp$ del $tmp$.ba? del enter.bat The following batch avoids the language, date and other problems of the 'date-hack' method (except redirection, no batch solves that) but is (masterly) complex... :: Readln.bat - A procedure that reads the contents of a ONE-line file. :: Data is stored in environment as 'Result'. :: Tom Lavedas :: http://www.pressroom.com/~tglbatch/ :: Note - this batch must NOT end with return/linefeed @echo %dbgs% off %{T}% if exist %0.bat %0.bat %1 if not exist %1 goto:Error goto:Error copy %1 {t}.txt > nul if exist %0 goto 2nd set {T}=goto:1st %0 %Path% c: d: e: f: :1st %comspec% /f /c dir %1.\%0.bat/s/b>{t}.bat copy /b {t}.bat+,, . > nul if exist {t}.bat for %%v in (set {t}.bat) do %%v {T}=goto:2nd if not [%2]==[] %0 %2 %3 %4 %5 %6 %7 %8 %9 :Error echo ERROR goto End :2nd copy %0+{t}.txt {t}.bat > nul set {T}=goto:End call {t}.bat del {t}.??t set Result=%{T}% :End set {T}= When saving make sure that the last line doesn't end with return! This is accomplished in Notepad by moving the cursor to just after the equal sign, holding SHIFT and then pressing Control-End, followed by DELete and then immediately saving the result. It's big, but as far as I know the above is the only all-batch procedure that uses no external commands (except the command interpreter itself) and runs on all languages and at least Dos 6 and Win9x. Should run on dos 5 too. No info on NT, maybe? From alt.msdos.batch... (slightly edited, used by permission) ----------------------------------------------------------- I have just recently found a method that works well in Win 9x, but not in earlier versions. It makes use of some of the PROMPT statement's characteristics, something like this ... :: Readln95.bat - A routine that reads a ONE line file into environment. :: Win 9x only :: Tom Lavedas :: http://www.pressroom.com/~tglbatch/ @echo %dbgr% off if not exist %1 goto:End goto:End if [%2]==[] %0 %1 Result > {a}.bat echo @prompt set {t}= > {b}.bat %comspec% /e:2048 /c {a}.bat | find "set" >>{b}.bat type %1 call {b}.bat > {a}.bat echo prompt $_set %2=%{t}%$_ >>{a}.bat echo exit $ > {b}.bat %comspec% /e:2048 /k < {a}.bat | find/v "$" for %%v in ({b}.bat del) do call %%v {?}.bat set {t}= :End The first PROMPT line helps to create a file that ends without a carriage return character. Appending the target text file to this 'helper' file achieves the desired result, except that a linefeed character prefixes the file's text. The second PROMPT is then used to extract this extra character without making any assumptions about the structure or content of the text being read from the file. If it can be assumed that only one string resides in the target text file, the procedure can be significantly simplified to something like ... :: ReadStrg.bat - A routine that reads a STRING from a ONE line file :: into environment. Win 9x only :: Tom Lavedas :: http://www.pressroom.com/~tglbatch/ @echo %dbgr% off if not exist %1 goto:End goto:End if [%2]==[] %0 %1 Result > {a}.bat echo @prompt set {t}= > {b}.bat %comspec% /e:2048 /c {a}.bat | find "set" >>{b}.bat type %1 call {b}.bat for %%v in (%{t}%) do set %2=%%v set {t}= del {?}.bat :End Or if the line of text in the target file is to be parsed and/or the actual structure of the file's contents is not required, this variation could be used ... :: ReadMany.bat - A routine that reads strings from a ONE line file. :: Win 9x only :: Tom Lavedas :: http://www.pressroom.com/~tglbatch/ @echo %dbgr% off %2 if not exist %1 goto:End goto:End > {a}.bat echo @prompt %0 1 goto:2nd= > {b}.bat %comspec% /e:2048 /c {a}.bat | find "=" >>{b}.bat type %1 {b}.bat :2nd echo For example: %2 %3 %4 %5 %6 %7 %8 %9 for %%v in (%2 %3 %4 %5 %6 %7 %8 %9) do shift if not [%2]==[] goto 2nd del {?}.bat :End Remember that this will work in Win 9x only. ---------------------------------------------------------- Play around with these, they look more complex than they are, extra statements are to re-call with the 2nd parm = Result, the default variable but you can specify whatever variable you want on the calling line - call readstrg input.fil myvar Or rip out the guts to use in your own application. The third batch is an example, replace the part after :2nd with whatever you want to do with the substrings.