Solved

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

Posted on 2006-06-28
13
2,359 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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

803 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