Question:
Command Line Script in Windows XP: How to read strings from a file and assign to variables?
Excelsior Pilot
2008-05-09 05:18:15 UTC
I have a text file that contains a list of filenames, one per line. For example, a file "fnlist.txt" with the following contents:

fname1
fname2
fname3
fname4

From the command line, I need to read these filenames and assign them to separate variables (e.g., string1, string2, string3, string4). This would allow me to use the variables simultaneously as input parameters to program started from the command line.

Can anyone suggest a command line script for Windows XP that can do this?
Three answers:
Random Malefactor
2008-05-13 18:08:04 UTC
You can use Windows Scripting Host scripts (either .JS or .VBS) to do things like this, the file system objects will allow you to open a file and read it line for line.



If you know the file will always contain the same number of lines, you can define a variable for each line, otherwise an array is a better fit. (Both languages make it possible to declare a dynamic array, JScript is a little easier as it automatically reallocates, you must use the ReDim Preserve statement to reallocate a dynamic array in VBScript.)



As for passing the contents of the file as arguments, the WshShell.Run method will execute a program for you. Building a list of arguments from a file could be as easy as this:



FileName = "[full or relative file name]"

Cmd = "[command to execute]"

ForReading = 1

Set oFs = CreateObject("Scripting.FileSystemObject")

Set oStrm = oFs.OpenTextFile(FileName, ForReading, False)

buf = oStrm.ReadAll()

buf = Cmd & " """ & Replace(buf, chr(13) & chr(10), """ """) & """"

Set WshShell = Wscript.CreateObject("Wscript.Shell")

WshShell.Run buf



This code will open and read a text file, and use the contents to create a list of quote-enclosed strings on one line. It does this by replacing the new line sequences with [quote] [space] [quote], and then adding leading and trailing quotes. It also concatenates the list with the program to pass them to. Finally, it executes the command with arguments.



Notes: quotes around arguments are necessary if any of the args contain embedded spaces (but are benign if they do not.) The command processor removes enclosing quotes before the program sees them. Also, literal quotes in string constants must be escaped, """" is a string with one double-quote character.
2008-05-09 05:54:05 UTC
No - the program that uses them has to be able to read the input in that form. If it needs them in a single line, you'll have to write a program to rewrite the file into a single line. Then just tell the program to use the file as input using redirect:



programname < filename



You can't easily write a batch file (that's about the only "script" available on the command line) that will rewrite the format of a text file - it would have to call an external program to do that.



Some command-line programs can take files as input - the command would be in the general form of



programname -i filename

or

prgramname @filename
2016-10-10 11:38:21 UTC
No, do no longer reproduction any documents. decision is constrained for use in autoexec.bat for in spite of reason. The command you desire is desperate /p tempvar= this might save the fee entered on the keyboard into the atmosphere variable tempvar for later use. remember that those variabels are worldwide, so call it some thing utility particular. in case you have the time to income it, i might suggestion using VBScripts or perl, which supply so plenty greater in terms of flow administration and such.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...