Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Is there any way to use the dir command to select first file of similar pattern of files
In unix,
ls /u01/opt/incoming/abc_*.txt | head -1something like
dir /O D:\Infa\incoming\abc_*.txt ???Thanks for your help

I suppose it depends on what 'pattern' you want to sort by. You can list by name, date, extension, attributes, and size.. Just type dir/? and enter at the dos prompt for more info.

Actually, I want to select only the fist file for example, one directory has 5 files
abc_0001.txt
abc_0002.txt
abc_0003.txt
abc_0004.txt
abc_0005.txt
by use single command i would like to select first file abc_0001.txtin unix, we use command:
ls /u01/opt/incoming/abc_*.txt | head -1i just wondering is ther any similar command in windows?
Thanks!

If you want to use the DIR command and have only one file show up then you're going to have to know what name variations all the files can take. Or use the DIR command on its exact file name--dir abc_0001.txt. But that seems kind of silly.
In the above list of files dir/o:n will list the files in order with abc_0001.txt being first.
Using wildcards there are several ways the DIR command will return just abc_0001.txt.
dir abc_???1.txt
dir ???_???1.txt
dir a??_???1.txt
dir ab?_??01.txtetc.
where the question mark means 'any alphanumeric character'. I think the underline ( - ) has to be there and can't be replaced by a wildcard. How you use the wildcards depends on what you know about all the files.
In dos a wildcard can replace an alphanumberic character. The asterisk ( * ) replaces an entire file name or its extension. For example *.* means 'any file name with any extension'. *.txt means 'any file name with a txt extension'. abc.* means 'a file named abc with any extension'.

Thanks!
When i issue a command, i would to have the return value as only the first file (abc_0001.txt.). Assume i sort all the file as mentioned.

If you knew all the files started with ABC then you could use something like dir abc_???1.txt or dir ???_???1.txt. Both would return only that one file.
However if, for example, some of the files started with XYZ then something like dir ???_0001.txt would also return XYZ_0001.txt (if it existed)
How you use the wildcards depends on what you know about the other file names in the directory. The more you know about them the better you can use wildcards to isolate the one file you want to show.

![]() |
![]() |
![]() |
| Login or Register to Reply | |
| Login | Register |
| Ads by Google |