Computing.Net > Forums > Programming > Append a number to filename

Append a number to filename

Reply to Message Icon

Original Message
Name: remgu
Date: September 26, 2007 at 07:16:03 Pacific
Subject: Append a number to filename
OS: XP
CPU/Ram: 3.4
Model/Manufacturer: Clone
Comment:

I have a batch file that FTPs a text file from a server to the user's PC. However sometimes the file is needed to be FTPed more than once a day. When we attempt to FTP it again, since there is a file with the same name already in that directory we get the following:

A duplicate file name exists, or the file
cannot be found.
Press any key to continue . . .

How can I tell the batch file to add one digit to the end of the file name so that we don't have this duplicate issue.

For example:

09-20-07file1.txt
09-20-07file2.txt
09-20-07file3.txt
...etc.

That is a simplified example; in reality the file names would look more like this:

audit.dump.%dumpdate%.LK%dumptid%001.txt
audit.dump.%dumpdate%.LK%dumptid%002.txt
audit.dump.%dumpdate%.LK%dumptid%003.txt
...etc.

Here is the part of the code that is relevant:

~

:LOOP1
Echo Enter the TID (xxxxxx) for the Audit Dump.
Echo.
Set /P dumptid= ^>
Echo.
Echo Enter the Date (yy-mm-dd) for the Audit Dump.
Echo.
Set /P dumpdate= ^>
Echo.
Echo.
Echo The Audit Dump's TID and Date that you entered are %dumptid% and %dumpdate%.
Echo Press Y or y and then ENTER if correct or press ENTER to retry.
Echo.
Set /P answ= ^>
Echo.
Echo.
Echo.
If /I not "%answ%."=="Y." GoTo :LOOP1
cd c:\ftp_temp
> latest.ftp.request.txt echo o %qastratusip%
>> latest.ftp.request.txt echo %qastratususername%
>> latest.ftp.request.txt echo %qastratuspassword%
>> latest.ftp.request.txt echo bin
>> latest.ftp.request.txt echo cd audit_dumps
>> latest.ftp.request.txt echo get %dumptid%.%dumpdate%.prt
>> latest.ftp.request.txt echo bye
ftp -s:latest.ftp.request.txt

REM *****************Start of Last FTP Request Time Stamp***************
echo @prompt set date=$d$_set time=$t$h$h$h > {a}.bat
%comspec% /e:2048 /c {a}.bat > {b}.bat
for %%v in ({b}.bat del) do call %%v {?}.bat
echo %date% %time% >> latest.ftp.request.txt
REM *****************End of Last FTP Request Time Stamp*****************

rename %dumptid%.%dumpdate%.prt audit.dump.%dumpdate%.LK%dumptid%.txt
Set dumptid=
Set dumpdate=
Set answ=
Pause
goto start

~

Thanks,
REMGU


Report Offensive Message For Removal


Response Number 1
Name: tonysathre
Date: September 26, 2007 at 16:27:10 Pacific
Reply: (edit)

You could do a GET to download the file first, then check if the filenames are the same. If they are, then append a number with this:

for /l %%n in (1,1,99) do (
rename downloaded_file downloaded_file%%n
)

Then, PUT the new file.

There's probably a better way to do it though , so I'll mess around with it.


"Computer security." — Oxymoron


Report Offensive Follow Up For Removal

Response Number 2
Name: Mechanix2Go
Date: September 27, 2007 at 02:28:00 Pacific
Reply: (edit)

There are several ways of discovering ;ast used suffix. Am correct in thinkiing all the files of interest have the extension txt?


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

M2



Report Offensive Follow Up For Removal

Response Number 3
Name: remgu
Date: September 27, 2007 at 05:22:49 Pacific
Reply: (edit)

Hi M2, you actually helped me out with the original batch file. Yes, the file FTP'ed is converted from a .prt to a .txt.

Thanks,
REMGU


Report Offensive Follow Up For Removal

Response Number 4
Name: Mechanix2Go
Date: September 28, 2007 at 07:57:15 Pacific
Reply: (edit)

I got the zip. II won't get to work on iit until tomorrow.


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

M2



Report Offensive Follow Up For Removal

Response Number 5
Name: remgu
Date: September 28, 2007 at 10:22:39 Pacific
Reply: (edit)

Thanks I appreciate it.

Thanks,
REMGU


Report Offensive Follow Up For Removal


Response Number 6
Name: Mechanix2Go
Date: October 1, 2007 at 01:15:08 Pacific
Reply: (edit)

I'm lost. I think I've mixed two threads.


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

M2



Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Append a number to filename

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge