Solved

pipe output of one command to set /P

Posted on 2011-03-08
9
895 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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
 
LVL 69

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 69

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 53

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

825 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