Solved

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

Posted on 2006-06-28
13
2,356 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
  • 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
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…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

708 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

14 Experts available now in Live!

Get 1:1 Help Now