0

I want to copy names of files to text in a directory based on their file extension.

As of now I am using dir /b >i67.txt which works fine for me but its not resolving problem of specific file extensions.

Can someone help me in getting a batch script for the same.

2 Answers2

0

You are looking for the following command, run it in the context of the directory which contains your files:

dir /b /s /-p *.txt /o:n | findstr /E .txt > i67.txt

Using the above code example, you will be able to find all *.txt files in the directory and output the results into the i67.txt file (will be outputted to the same directory).

Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
  • Thanks for your suggestion, I have done it. Here is what I am using now: dir /b | findstr [a-z].*ovr>i67.txt && dir /b | findstr [a-z].*inc>>i67.txt && dir /b | findstr [a-z].*dat>>i67.txt What it does?? --- It copies all names(remember,only name except files itself which are ending with extension .ovr .dat and .cpi ) present in a directory and copy it to a text file(here name is i67.txt) – aks_webmethods Mar 09 '14 at 08:47
  • @aks_webmethods - That will not do what you think. See the end of [my answer](http://stackoverflow.com/a/22282941/1012053). – dbenham Mar 09 '14 at 14:24
0

You can specify multiple file masks within one DIR /B command. Based on your comment to Yair Nevet's answer, it seems you want the following extensions: .ovr, .inc, and .dat. That can be done simply using:

dir /b /s *.ovr *.inc *.dat >i67.txt

If the files are on an NTFS volume that has short 8.3 names enabled, then you might get additional undesired file extensions if you have any file extensions longer than 4 characters that begin with your wanted extension. For example someName.data would show up in your output because it most likely would have a short name of SOMENA~1.DAT that matches your file mask.

You can prevent short name inclusion by piping the output to FINDSTR. The /L option forces a literal search as opposed to regular expressions, the /I option ignores case, and the /E option matches only the end of each line. Multiple search terms are delimited by spaces.

dir /b /s *.ovr *.inc *.dat | findstr /lie ".ovr .inc .dat"

Regarding your following comment:

Here is what I am using now: dir /b | findstr [a-z].*ovr>i67.txt && dir /b | findstr [a-z].*inc>>i67.txt && dir /b | findstr [a-z].*dat>>i67.txt What it does?? --- It copies all names(remember,only name except files itself which are ending with extension .ovr .dat and .cpi ) present in a directory and copy it to a text file(here name is i67.txt)

That will not actually do what you want for several reasons.

  • Windows file names are not case sensitive. Windows would treat NAME.OVR and name.ovr the same, so you should as well. That requires the /I option.

  • There is nothing in your search to anchor ovr to the extension. It will look for your pattern anywhere within the file name. And the dot is a meta character that represents any character - not a literal dot. The asterisk allows the dot to match any number of characters.

I can't be sure, but it looks like perhaps you only want to match files that begin with a letter. The following modification to my answer should do the trick:

dir /b /s *.ovr *.inc *.dat | findstr /ri "^[a-z].*\.ovr$ ^[a-z].*\.inc$ ^[a-z].*\.dat$"

The \R option forces a regular expression match instead of a literal. It is the default behavior for the given search, but it is a good idea to be explicit with regard to regex vs literal search.

^ anchors the search to the beginning of the name

[a-z] matches any letter (sort of). Remember it is not case sensitive because of the /I option. Without the /I option, it would not match upper case Z. See Why does findstr not handle case properly (in some circumstances)? for an explanation.

.* matches any number of characters, without restriction

\. matches a dot literal, marking the beginning of your extension

Then comes your extension

$ anchors the match to the end of the name

Community
  • 1
  • 1
dbenham
  • 127,446
  • 28
  • 251
  • 390