How to Grab the first file in dir

March 17, 2009 at 12:34:18
Specs: Windows XP
Is there any way to use the dir command / some DOS Script to select only first file of similar pattern of files in a direcotory and rename it for example, one directory has 5 files
abc_1005.txt
abc_5256.txt
abc_2001.txt
abc_2003.txt
abc_3006.txt
by use script I would like to select only first file abc_1005.txt and rename(move) it as abc.txt;
For next run, directory has 4 files, script returns the first file as abc_5256.txt and rename it as abc.txt...

In UNIX, the we can use a single line command as
mv `ls /u01/opt/incoming/abc_*.txt | head -1 ` abc.txt

I just wondering is there any similar command or script in DOS?

Thanks for your help,


See More: How to Grab the first file in dir

Report •


#1
March 17, 2009 at 21:59:13
Not that I know of, but you could try installing cygwin & bash in windows - that should give you the bash shell on a windows box & you can use your *nix tools...

Report •

#2
March 18, 2009 at 02:37:05
It's easier to grab the last line, so if you wanted the first file by name:
FOR /F "delims=" %%a IN ('DIR /a-d/b/o-n') DO SET file=%%a


Report •

#3
March 18, 2009 at 06:24:03
It is good, But actual requirement is to select ONLY ONE first file and rename it. can you help in this?

Report •

Related Solutions

#4
March 18, 2009 at 06:56:52
Depends on how you define 'first' file.


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#5
March 18, 2009 at 07:04:09
I am fine with sorting on any order. but select only one file and rename it in each run.

Report •

#6
March 18, 2009 at 09:59:52
@echo off & setLocal EnableDelayedExpansion

if exist abc.txt del abc.txt

for /f "tokens=* delims= " %%a in ('dir/b/a-d/o-d *.txt') do (
ren %%a abc.txt
goto :eof
)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#7
March 18, 2009 at 11:42:50
WOW! This is working absolutely fine and as expected… Thanks much…

Can you please explain what exactly is 'dir/b/a-d/o-d *.txt'? Because, it helps in change the selection pattern of files. Actually, I have audit_parameter_*.txt files


Report •

#8
March 18, 2009 at 12:20:24
do a dir/? and study the usage


=====================================
If at first you don't succeed, you're about average.

M2


Report •


Ask Question