2

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

ver

is Microsoft Windows XP [Versione 5.1.2600].

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

MDMarra
  • 100,734
  • 32
  • 197
  • 329

3 Answers3

7

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.

jscott
  • 24,484
  • 8
  • 79
  • 100
August
  • 3,114
  • 16
  • 17
4
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)
Stephane
  • 6,432
  • 3
  • 26
  • 47
0

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
Brent Pabst
  • 6,069
  • 2
  • 24
  • 36
  • So how does this explain the behavior OP is seeing with `cmd` and `FOR`? – jscott Jul 02 '12 at 15:34
  • How about in this pre-existing question: http://stackoverflow.com/questions/1523043/how-to-loop-through-files-and-rename-using-powershell – Brent Pabst Jul 02 '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 02 '12 at 15:48
  • "Better technology" -- Sorry, I think you're missing the point of the OP's question. – jscott Jul 02 '12 at 16:02