0

Assume I use dir commend and it returns:

02/23/2014  03:03 PM    <DIR>          ManageEngine
09/28/2013  02:09 PM    <DIR>          opt
04/30/2014  09:30 AM    <DIR>          Oracle
09/29/2014  12:53 PM    <DIR>          Program Files
09/15/2014  12:59 PM    <DIR>          Program Files (x86)
09/30/2014  01:10 PM    <DIR>          ProgramData
08/10/2014  03:06 PM    <DIR>          Virtual Machines
05/05/2013  12:19 PM    <DIR>          wis
               0 File(s)              0 bytes
               8 Dir(s)  35,379,425,280 bytes free

Now I want to read the second line for example from the previous command line results(dir) and use it in my current command line.

Example:

del opt

How can I read the second line?

Bob
  • 22,810
  • 38
  • 143
  • 225

2 Answers2

0

You use for. See How does “FOR” work in cmd batch file?. But you shouldn't be parsing the output of dir, you can use, again, for to iterate directories:

Directories only

If set contains wildcards (* and ?), the specified command executes for each directory (instead of a set of files in a specified directory) that matches set. The syntax is:

for /D {%% | %}variable in (set) do command [CommandLineOptions]

I'm not going to show how to get the second item in a set since that is a) trivial and b) wrong. Set ranking is random in lack of an order.

Community
  • 1
  • 1
Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
0
@echo off

rem use skip=n-1 where n is the line you want
for /f "skip=1 delims=" %%# in ('dir') do (
    echo %%#
    rem delete echo to activate deletion
    echo rd /s /q "%%#"
    goto :break_for
)
:break_for

if you want to filter the line that contains opt

@echo off


for /f "delims=" %%# in ('dir^|find /i "opt"') do (
    echo %%#
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187