8

i have wrote a batch script like below

for -f %%a in ('dir /b') do command

This script works in Windows XP but now I want to run it in Windows 3.11.

It gives a syntax error; it seems like Windows 3.1's DOS not support `for -f %%a in ('command').

Can you suggest what command can I use in Windows 3.1 to achieve equivalent functionality?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
issac
  • 81
  • 1
  • 6
    Why on _earth_ are you using Windows 3.1? – SLaks Dec 20 '09 at 17:26
  • Maybe he's connecting various dinosaur OS's onto a single network like I did for my Networking class. – Sergio Tapia Dec 20 '09 at 17:30
  • Are you using Windows 3.1 or Windows For Workgroups 3.11? – SLaks Dec 20 '09 at 17:30
  • 1
    OMG: http://en.wikipedia.org/wiki/Windows_3.11#Legacy It was being used for embedded apps until 2008!! – T.J. Crowder Dec 20 '09 at 17:31
  • LOL! Check out the taglist on the right -- nothing else has ever been tagged "windows-3.1". I love it. – T.J. Crowder Dec 20 '09 at 17:36
  • 1
    People: we witness a event of historical importance: Windows 3.1 arose from the dead! – Abel Dec 20 '09 at 17:42
  • 1
    +1, for mentioning Windows 3.1 – MAK Dec 20 '09 at 18:30
  • +1 for an example of "indirection" or "output redirection" or whatever (if somebody knows how this is called in exact terms, i would appreciate a better definition) in MS-DOS/Windows command prompt - in particular, many DOS text mention the usual input/output redirection with <,>,>> and pipe, but the equivalent of the "backquote thing" as you use it in Unix bash (`for a in ``some command arg1 arg2`` do something done`) was something i was wondering about... - glad to hear that for command implements it (don't know if it is a "generic" functionality or something specific to FOR command only) – hello_earth Apr 29 '11 at 11:06
  • another thing is that in my version of command prompt (Microsoft Windows XP [Version 5.1.2600]) the `-F` switch is replaced with `/F` switch, and that actually this switch is enabled when you have Command Extensions turned on (which is the default actually, so makes almost no difference - see CMD.EXE command-line switches) – hello_earth Apr 29 '11 at 11:11

2 Answers2

8

In DOS 5.0, you cannot use a command inside the IN (...) part of the statement. What you can do is the following though:

FOR -F %%A IN (*.txt) DO command

which will execute the command for each file with the extension txt. In other words, the dir command is implicit.

I got this information from Jeff Prosise's DOS 5. At the time indispensable, now rather dusty. Never knew I'd ever use it again ;-)

EDIT: it appeared that the indirection (see history) was not necessary. The above statement is all you need. I.e., the following works and prints each file:

FOR -F %%A IN (*.txt) DO TYPE %%A
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
Abel
  • 56,041
  • 24
  • 146
  • 247
  • A little note: there should be *no quotes* around `*.txt`, those were not supported at the time (spaces in filenames or long filenames were not supported anyway). – Abel Dec 20 '09 at 18:47
  • The equivalent to the original question would be `(*)`, not `(*.txt)`. – Eli Acherkan Dec 20 '09 at 19:14
  • 1
    Abel: While the quotes didn't exist, spaces were indeed legal in MS-DOS file names: http://blogs.msdn.com/oldnewthing/archive/2009/07/09/9825126.aspx – Joey Dec 20 '09 at 22:45
  • @Rössel: interesting. I was aware of that, but was under the impression that internally, spaces were replaced with Ext-ASCII 0xFF, but apparently that assumption was false (it's all 15 years ago...) – Abel Dec 21 '09 at 15:59
1

You are correct; this syntax is not supported by Windows 3.1.
It was added by cmd.exe in Windows NT.

I don't think you'll find an equivalent command included with Windows 3.1.
EDIT: I was wrong; see Abel's answer.

Why are you using such a pre-historic OS?

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964