?
Solved

writing a batch file to get the most recent file

Posted on 2003-03-09
6
Medium Priority
?
287 Views
Last Modified: 2008-01-09
Hi
I am trying to write a batch file that would find the most recent file
in a folder and rename it to recent.csv. All the files in the folder are "CSV" files . I tried the following script exactely but no luck. Can someone tell me what is wrong with then following script. I do not have any experience writing scripts --many thanx
cls
@echo off
dir C:\PERFLOGS /o-d /a-d | find "-" | find ":" > enter.bat
fc enter.bat nul /lb1 /n |date|find " 1: " > enter.bat
echo copy /y %%5.%%6 recent.%%6 > enter.bat
call enter.bat
del enter.bat>nul
cls
0
Comment
Question by:ravin1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
6 Comments
 
LVL 7

Accepted Solution

by:
billious earned 160 total points
ID: 8097508
It's OS-dependent, and you've not told us what OS you're using.

for NT/2K/XP, where that technique is broken, try

@echo off
if exist c:\PERFLOGS\recent.csv echo recent.csv already exists
if exist c:\PERFLOGS\recent.csv goto eof
for /f %%k in ('dir/b/o:d C:\PERFLOGS\*.csv') do set changeme=%%k
ren %changeme% recent.csv
set changeme=
cls

...More techniques like this in Operating Systems>MSDOS or
Operating Systems>(Your OS)

...Bill
0
 
LVL 7

Expert Comment

by:billious
ID: 8097543
Ah - in the original, you're COPYing the most recent .csv to recent.csv, whereas your request asks for a 'rename'

If you want to COPY (which seems more reasonable) then
replace the 'ren' with 'copy'

A few notes of caution:

1. The if exist .... lines I inserted to warn of the presence of the destination file. Deltet if this is not required.

2. If you do delete those lines, then if the destination file exists and is already open with some utilities (like EXCEL, WORD...) then the copy will fail BECAUSE EXCEL has the file OPEN.

   With some utilities (like WORDPAD, for instance) I believe that the file is loaded into RAM and closed. If you then do your COPY, that will succeed - but the original can still be re-saved from WORDPAD and will then overwrite your newly-copied version.

...Bill

0
 
LVL 6

Expert Comment

by:holli
ID: 8097989
i think you cannot do this with dos-batch-files.
can you think about using perl?

the following perl script will solve your problem:

#!/usr/bin/perl
use File::Basename;
$f = (sort { (stat($b))[9] <=> (stat($a))[9] } <c:/perlflogs/*.*>)[0];
rename $f, (fileparse ($f))[1]."recent.csv" or die $!;

0
 
LVL 6

Expert Comment

by:holli
ID: 8097999
nice work, billious. the FOR options where new for me :-))
0
 

Expert Comment

by:CleanupPing
ID: 9447255
ravin1:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Introduction to Processes

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question