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
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?
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
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.
Summary: I would like to create a .bat file that finds the first file in a folder on an FTP server. FTP Server RandomFolder <- find the first file in this folder index.html ^Stuff on server I know I have to...
Summary: can someone post a link or code for c++ on linux for assigning the first file in a directory to a variable? i'm not a programmer but have cobbled together a number of things (with lots of help from fo...