Computing.Net > Forums > Programming > How to Grab the first file in dir

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to Grab the first file in dir

Reply to Message Icon

Name: raghav525
Date: March 17, 2009 at 12:34:18 Pacific
OS: Windows XP
Subcategory: Batch
Comment:

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,



Sponsored Link
Ads by Google

Response Number 1
Name: Fist (by fmwap)
Date: March 17, 2009 at 21:59:13 Pacific
Reply:

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


0

Response Number 2
Name: Razor2.3
Date: March 18, 2009 at 02:37:05 Pacific
Reply:

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


0

Response Number 3
Name: raghav525
Date: March 18, 2009 at 06:24:03 Pacific
Reply:

It is good, But actual requirement is to select ONLY ONE first file and rename it. can you help in this?


0

Response Number 4
Name: Mechanix2Go
Date: March 18, 2009 at 06:56:52 Pacific
Reply:

Depends on how you define 'first' file.


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

M2


0

Response Number 5
Name: raghav525
Date: March 18, 2009 at 07:04:09 Pacific
Reply:

I am fine with sorting on any order. but select only one file and rename it in each run.


0

Related Posts

See More



Response Number 6
Name: Mechanix2Go
Date: March 18, 2009 at 09:59:52 Pacific
Reply:

@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


0

Response Number 7
Name: raghav525
Date: March 18, 2009 at 11:42:50 Pacific
Reply:

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


0

Response Number 8
Name: Mechanix2Go
Date: March 18, 2009 at 12:20:24 Pacific
Reply:

do a dir/? and study the usage


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

M2


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: How to Grab the first file in dir

how to compile a .Reg file in VC++? www.computing.net/answers/programming/how-to-compile-a-reg-file-in-vc/10251.html

.BAT To use FTP and find first file in folder www.computing.net/answers/programming/bat-to-use-ftp-and-find-first-file-in-folder/19566.html

c++ - find first file in directory? www.computing.net/answers/programming/c-find-first-file-in-directory/16516.html