Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

pipe output of one command to set /P

Posted on 2011-03-08
9
Medium Priority
?
910 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Accepted Solution

by:
sjklein42 earned 1200 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
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 800 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 71

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 59

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

971 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