I have a the folder c:\test\ and two files in it a.txt and b.txtv.

I would like to process just the files with extension equal to .txt.

If I write this commands

cd c:\test
for %f in (*.txt) do echo %f

I will get the result where both a.txt and b.txtv are listed.

The same happens with

cd c:\test
dir *.txt

It seems .txt is the same of .txtv.

I have Windows XP SP3 in Italian and the result of


is Microsoft Windows XP [Versione 5.1.2600].

The same result is from Windows 7 in English Microsoft Windows XP [Version 6.1.7601].

  • Just a side note but Windows Ver. 6.1 is the official version number for Windows 7. Jul 2 '12 at 15:18
for %%I in (*.*) do if "%%~xI"==".txt" (echo %%I)

That should do it.

Edit: that's the "scripting" syntax. If you want to type this at the command line, you must remove one of the % sign each time they are doubled, linke this:

for %I in (*.*) do if "%~xI"==".txt" (echo %I)

Try using forfiles instead. It seems to be more restrictive on what it returns for the extension, plus it has the added benefit of a built in for loop. Should already be on the Win7 box, but you might have to install a 2003 Server Resource kit on the XP box to get it on there.


It's really simple in PowerShell and works fine with the following:

PS C:\Users\bpabst\Desktop> ls *.txt

Directory: C:\Users\bpabst\Desktop

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          7/2/2012  11:19 AM         11 test.txt
  • So how does this explain the behavior OP is seeing with cmd and FOR?
    – jscott
    Jul 2 '12 at 15:34
  • How about in this pre-existing question: stackoverflow.com/questions/1523043/… Jul 2 '12 at 15:46
  • I'm still not seeing "powershell" listed anywhere in the OP's question. They even tagged it [batch] and [cmd].
    – jscott
    Jul 2 '12 at 15:48
  • "Better technology" -- Sorry, I think you're missing the point of the OP's question.
    – jscott
    Jul 2 '12 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.