Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

pipe output of one command to set /P

Posted on 2011-03-08
9
Medium Priority
?
908 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
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 57

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
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…

722 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