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

Append a number to filename

Reply to Message Icon

Original Message
Name: remgu
Date: October 2, 2007 at 13:06:28 Pacific
Subject: Append a number to filename
OS: Windows Vista Ultimate
CPU/Ram: P4 3.4Ghz 3GB Ram
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: Mechanix2Go
Date: October 3, 2007 at 06:38:41 Pacific
Subject: Append a number to filename
Reply: (edit)

:: discover last used ONE digit numeric suffix

@echo off > numlist
setLocal EnableDelayedExpansion

dir /b 09-20-07file?.txt > namelist

for /f "tokens=* delims= " %%a in (namelist) do (
set FN=%%~Na
echo !FN:~-1! >> numlist
)

sort < numlist > sorted

for /f "tokens=* delims= " %%a in (sorted) do (
set /a N=%%a
)
set /a N+=1
echo the next num is !N!



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

M2



Report Offensive Follow Up For Removal

Response Number 2
Name: remgu
Date: October 4, 2007 at 08:07:27 Pacific
Subject: Append a number to filename
Reply: (edit)

I decided to use the code below to rename my files with the date and time instead of a sequence number. Do you know how I can have it show only that last two digits of the year ("07" instead of "2007")?

@ECHO OFF
FOR /F "TOKENS=2-4 DELIMS=/ " %%a IN ("%date%") DO SET mm=%%a&SET dd=%%b&SET yy=%%c
FOR /F "TOKENS=1-3 DELIMS=:." %%a IN ("%time%") DO SET hh=%%a&SET tt=%%b&SET ss=%%c
echo "%yy%-%mm%-%dd% %hh%h.%tt%m.%ss%s"

Thanks,
REMGU


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