0

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.

0

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot for your answer.Can you tell me what to do if I want the output file to be in some other folder,not in current directory?
@RajatSaxena - Simply prefix the output file name with the path to your folder. I've updated the answer with that trivial change. The quotes are there in case the path includes spaces.
Please don't mind but what if the directory in which this new file will be,is dynamically generated using the same script?How to specify the path then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.