Solved

How to chain together two DOS commands in a single line?

Posted on 2006-06-28
13
2,364 Views
Last Modified: 2010-05-04
Basically I need to:
> CHDIR C:\folder\subfolder
> dir /b /a

But I need to be able to do this in one line. I tried using piping similiar in *nix, but nothing worked.
Any Suggestions?

-A
0
Comment
Question by:cl0ckwork
[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
  • 5
  • 2
  • 2
13 Comments
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17003324
cl0ckwork,

My suggestion would be to create a batch file (.bat), put the commands in the file, and then run the file when you want to run the commands.  The name of the file does not matter but the extension would be bat.  The file is a basic text file and can be created using Window's Notepad.  A sample file, I called it folddir.bat, is below.  Save the file to your computer in a system folder (e.g. your Windows folder) or, if you will always run the file from one location, in that directory.

REM ## Contents of folddir.bat
chdir c:\folder\subfolder
dir /b /a

b0lsc0tt
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
ID: 17003440
Use the ampersand:

CHDIR C:\folder\subfolder&dir /b /a

Good Luck,
Steve
0
 
LVL 30

Assisted Solution

by:callrs
callrs earned 250 total points
ID: 17004603
The '&' sign (for NT/2k/XP) or the '|' sign (for 98)  often allow use of multiple commands on one line.
See http://computerhope.com/issues/ch000177.htm "Can you type more than one command at one command prompt?".

HOWEVER, note these cautions: The usage fails in at least these cases from test's I've run::

 '|' after 'if' or 'call' statement;
'&'  after 'if'
'&' after 'set' as in:set x=y&if  %x%==y echo y; the set doesn't happen until AFTER a line break!

Note: to echo the '&'  you must 'echo ^&'
0
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
LVL 30

Expert Comment

by:SteveGTR
ID: 17004777
This should have been a split... My answer correcly answers the question. callrs answer, although informative, reiterates my answer and offers additional information that is beyond the scope of the question.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17006049
Thanks :)
0
 
LVL 30

Expert Comment

by:callrs
ID: 17006065
SteveGTR  deserves some credit.

But his solution is incomplete.
- it's NT-specific (what if user is uses Win98 as I & many others do?)
- it's cryptic. What if user needs later to use commands such as echo, set, if, etc.? At the beginning I found myself frustrated & wasting time in trying to find out WHY it didn't work. My solution from many hours of past testing now benefits the user in that it will save him/her much time & headaches in use of the feature.

User picks the answer which best meets his/her needs & the better the answer, the more encouragement & joy it brings & so it becomes a matter of "YES! Thank YOU!", as often happens -- from many of my time-consuming-to-produce but worthwhile answers. Agreed though, that points should be split at times.

"How to chain together two DOS commands in a single line?" was better answered in the second post. And so asker jumped in joy at the detail & picked the one that will do the job & cause less trouble. A little knowledge frustrates & can be dangerous. Give a more complete answer & it's just what is desired with or without a clear-cut query by the asker.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17006093
You might want to look at your answer. It said nothing about chaining the actual commands specified in the original question. If the user has questions about my posts I answer them. Some users don't need lectures on MS-DOS...
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17006107
My point is that if you are not adding anything new to a previously answered question then don't post. It's sort of an expert to expert etiquette.

Take for instance you post regarding the other question posted today:

My answer:

@echo off

setlocal

for /f "delims=" %%a in ('dir /b /ad 2^>NUL') do echo %%a&goto :EOF

You answer posted after mine:

for /f "usebackq tokens=1" %%a in (`dir /ad /b`) DO @echo %%a&goto :eof

They are the same processing. Although as I previously posted your code won't work with directories with spaces. You have just capitalized or uncapitalized the code. Used backquotes instead of the default and introduced the tokens switch where in the bug resides.
0
 
LVL 54

Expert Comment

by:b0lsc0tt
ID: 17006158
Please unaccept my comment.  My answer was definitely not the best answer to the question and I believe it was accept by mistake when the points were split.  Instead I believe this comment http:Q_21902366.html#17003440 was actually meant to be accepted.  Thank you though.

Thanks to Clockwork for the fun question and the informative comments.  I'm glad that I could participate but do not deserve points.

bol
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

628 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