Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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].

share|improve this question
Just a side note but Windows Ver. 6.1 is the official version number for Windows 7. – Brent Pabst Jul 2 '12 at 15:18
up vote 4 down vote accepted
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)
share|improve this answer

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.

share|improve this answer

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
share|improve this answer
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:… – Brent Pabst 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 posting your answer, you agree to the privacy policy and terms of service.

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