Solved

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

Posted on 2006-06-28
13
2,362 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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote Application Install 5 86
robocopy 6 73
Batch script needed for same file name but different file types 4 58
xcopy using a text file to a folder 13 72
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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