Rajat Saxena
Rajat Saxena

Reputation: 3935

Windows batch programming: Getting data of some command into some variable

Hello I want to collect output of following command into some variable.How can I do that?

type somefile.txt | find /v "irritating string"

Actually I want to write the output of above command to some other file in some other folder.

Upvotes: 0

Views: 119

Answers (1)

dbenham
dbenham

Reputation: 130919

Your command may produce multiple lines of output. Windows batch does not have a convenient method to capture multiple lines of command output into a single variable. The FOR /F command can be used to process each line individually, at which point you could concatenate each line into a single variable (up to the ~8191 byte value length limit), or you could create an array of variables.

Type FOR /? or HELP FOR from a command prompt for more info about the FOR command.

But since you want to really capture the output in a file, the solution is exceedingly easy.

If you want to create a new file

type somefile.txt | find /v "irritating string" >"c:\somePath\newFile.txt"

If you want to append the output to an existing file

type somefile.txt | find /v "irritating string" >>"c:\somePath\existingFile.txt"

If the file does not exist yet, it will be created.

Upvotes: 1

Related Questions