Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

writing a batch file to get the most recent file

Posted on 2003-03-09
6
Medium Priority
?
293 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
  • 2
  • 2
5 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Make the most of your online learning experience.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

564 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