Solved

pipe output of one command to set /P

Posted on 2011-03-08
9
893 Views
Last Modified: 2012-05-11
I want the first line of a DIR output in an environment variable. I tried

dir *.bat /T:C /O:-D /B | set /P test= 

Open in new window


That didn't set the variable. I got it running using the following command:

FOR /F "tokens=*" %I IN ('dir *.bat /T:C /O:D /B') DO SET test=%I) >NUL

Open in new window


Since FOR will loop through the whole directory listing, I don't really like it and would prefer to use the piping instead. Going through an external file is not an option.
0
Comment
Question by:cmnt
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35073546
You can try this. It basically inverts the sorting so the first line becomes the last for the FOR to work.

FOR /F "tokens=*" %I IN ('dir *.bat /T:C /O:-D /B') DO @SET test=%I

Open in new window

0
 

Author Comment

by:cmnt
ID: 35073658
With the piping I intended to catch the first line of the DIR output. After running FOR the test variable will contain the last line. Therefore piping and FOR need to have opposite sorting order. The FOR works as expected. The piping does not work at all. I am looking for a solution using piping, so I don't need to go through all entries in the folder. The folder may contain several hundred entries and I am only concerned about the newest one.
0
 

Author Comment

by:cmnt
ID: 35073883
BTW, I missed one character in the second code line in my question. The correct line needs to be

(FOR /F "tokens=*" %I IN ('dir *.bat /T:C /O:D /B') DO SET test=%I) >NUL

Open in new window


Of course, I can leave out the outer parenthesis with the >NUL and just use @SET instead of SET as wdosanjos suggested. The sort order must be as stated in my question.
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 300 total points
ID: 35074221
test.bat

Exits loop after first file found (and set)

usage:

call test.bat >nul:

Open in new window


@FOR /F "tokens=*" %%I IN ('dir *.bat /T:C /O:D /B') DO (
@set test=%%I
@goto :EOF
)

Open in new window

0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 35074833
To answer the original question: Piping into set /P never works. You can only redirect a file. The reason for that is unknown, but it has been tried what has to be one google times, I suppose, to no avail.

There is indeed no other way than to use a file, or process the command stream with FOR.
Just to add another one (which isn't better or worse - this is the non-batch version):
>nul (set test=& for /F "delims=" %F in ('dir *.bat /t:c /o:-d /b') do if not defined test set test=%F)

Open in new window


sjklein42,
You need to use the reversed sort for that, as used in the very first code line shown by the asker.
0
 
LVL 23

Expert Comment

by:wdosanjos
ID: 35075134
@cmnt,

> I am looking for a solution using piping, so I don't need to go through all entries in the folder.
> The folder may contain several hundred entries and I am only concerned about the newest one.

I think there is no way to avoid the IO required to iterate through all the entries in the folder.  First, dir has to find all entries that end with .bat, and then it needs retrieve the create dates for the sort.  By the time dir starts displaying the results all the IO on the folder is already done.
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 35075300
wdosanjos is correct about that. Unlike in UNIX or Linux, a process writing into a pipe is always executed completely before passing the output to the pipe consumer. The dir is performed completely.
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 35075890
Not much different than what has already been discussed, but here's how I typically handle this in a batch file:

FOR /F "tokens=*" %%I IN ('dir *.bat /T:C /O:-D /B') DO SET test=%%I & goto DONE
:DONE

Open in new window

But behind the scenes DOS still does have to read all the files in the directory, filter out just the BAT ones, and then sort by date.  There's no way to get the most recent one "directly".

~bp
0
 

Author Comment

by:cmnt
ID: 35082795
Thanks everyone for their comments. I will go with the FOR...GOTO combination. Regarding the io issue I was a little bit unclear. I wasn't concerned about the Dir command, which I expected to go through the whole directory. I just didn't wanted to iterate through the whole result set.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now