Solved

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

Posted on 2006-06-28
13
2,357 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

910 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

22 Experts available now in Live!

Get 1:1 Help Now