1

I'm not sure whether it is possible, but what I need is a plain bat/cmd file that runs on windows 7 and does such things:

Step 1. findstr - it should find a specific string using regular expressions engine. Suppose we're looking for a number enclosed in tags <id>123</id> (suppose such a file is unique, so one value is returned). The command would print 123 to the screen, but I need to save it in a variable (don't know how).

Step 2. Another call to findstr on another directory. Now we want to find a file NAME (/m option) containing the value that we saved on Step 1 (in another group of files i.e. another directory). And again, save the result (name of the file) in a variable. Say, file_123.txt matches the criteria.

Step 3. Copy the file that we got as a result of the second findstr call (file_123.txt) to another location.

The whole question turns around the point about how to save the result of windows commands to variables to be able to provide these values to subsequent commands as parameters.

2 Answers 2

3

The general way of getting command output in variables is

for /f %%x in ('some command') do set Var=%%x

(with various variations, depending on context and what exactly is desired).

As for your steps, I elaborate after lunch. There are some intricacies.

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

Comments

0

Step 1:

FOR /F "USEBACKQ tokens=1-20 delims=<>" %%A in (`FINDSTR "123" "path of file to search in"`) DO (
 SET var=%%B
)

ECHO %var%

Understand that delims will change depending on what 'separates' the parts of the output (whether its a space, a special character, etc.)

Step 2 & 3:

FOR /F "USEBACKQ tokens=*" %%A IN (`DIR "Path" /A ^| FIND /I "%var%"`) DO (
 COPY /Y "%%A" "C:\New\Path\%%~nxA"
)

Comments

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.