Link to home
Start Free TrialLog in
Avatar of boridom2006
boridom2006

asked on

REPLACE FILE EXTENSION WITH CURRENT DATETIME

I have 10 files that needs to be renamed every night with the extension datetime.  Currently file name is
ERRORLOG.1  to  ERRORLOG.10.
I need to rename only those 10 files on the ERRORLOG directory to:
ERRORLOG.DATETIME  where DATETIME needs to be the system datetime in which ERRORLOG is been renamed.

Your prompt assistance is greatly appreciated.

Thanks,
Amber
Avatar of GuruGary
GuruGary
Flag of United States of America image

Hi, Amber. I am going to make the following assumptions:
- You are running this from a command prompt under Windows 2000/XP/2003
- By DATETIME, you mean a format of YYYYMMDDhhmm[AM/PM]
- The file ERRORLOG.DATETIME does not already exist

Try the following from within a batch file:

@echo off
for /L %%a in (1 1 10) do if exist ERRORLOG.%%a for /f "Tokens=1-6 Delims=:/ " %%b in ('dir ERRORLOG.%%a ^| findstr /i ERRORLOG.%%a') do echo Processing ERRORLOG.%%a & ren ERRORLOG.%%a *.%%d%%b%%c%%e%%f%%g

If you want the DATETIME in a different format, or have any problems, let me know.
Avatar of boridom2006
boridom2006

ASKER

Hi GuruGary,
When I executed the command, received the following message:
%%a was unexpected at this time.

Do I have to change anything on the code to adapt it to the location of my ERRORLOG directory?  
My ERRORLOG directory is on the following path:
C:\ERRORLOG

Under that directory I have the following files to rename with extension .YYYYMMDDhhmmss:
ERRORLOG.1
ERRORLOG.2
ERRORLOG.3
ERRORLOG.4
UNTIL ERRORLOG.10

In reference to the DATETIME format, I need  YYYYMMDDhhmmss


Thanks,
Amber
You have to execute Gary's code from a batch file on operating system's Windows 2000 or greater. Put the code he posted in a file using notepad and name it something like FixFiles.bat. You might consider putting it in the same directory as the files or in a directory that's accessible via the path.
Gary,
I executed the batch file, and it worked beautifully, but the DATETIME extension, is the file's timestamp, instead of the current DATETIME in which the file got renamed.  In addition, the DATETIME extension is needed as YYYYMMDDhhmmss, to avoid having files with the same name extension.  Please advise, and thanks for your help.

Amber
Hi, Amber.  I'm not sure I understand your question, so let me clarify.  I originally understood that you wanted the extension to be the timestamp of the file.  Do you actually want the extension to be the timestamp of the current date and time of when the batch file is run?

If this is the case, all 10 files will be prossed within 1-2 seconds and they will have the same filename and extansion.  Or will it typically only be processing one file at a time?
Hi, Amber.  After reading the title of your post it appears that you do indeed want the current date and time of when the batch file is run.  Sorry I missed that the first time.  Please note that if there is more than 1 file named ERRORLOG.#, then this batch file will only process the first one - since they would otherwise try to have the same filename.  Please try this instead:

@echo off
setlocal
for /f "Tokens=2-4 Delims=/ " %%a in ('date /t') do for /f "Tokens=5-7 Delims=:. " %%d in ('echo. ^| time ^| findstr /i /v enter') do set datetime=%%c%%a%%b%%d%%e%%f
for /L %%g in (1 1 10) do if exist ERRORLOG.%%g echo Processing ERRORLOG.%%g &ren ERRORLOG.%%g *.%datetime% &goto :EOF
echo No valid files, or they have allready been renamed.
Hi Gary,
Thanks a lot for all of your help.  You are correct.  The last script you provided only renames one file, and ignores the rest.

I will be accepting your first answer, but first I would like to ask if you could please let me know how to change the datetime format to MMDDYYYYhhmmss.  

Thanks,
Amber
Hi, Amber.  To change the order of the Month, Day and Year, it will depend on which batch file example you are using.  For the last example, the new code would be:

@echo off
setlocal
for /f "Tokens=2-4 Delims=/ " %%a in ('date /t') do for /f "Tokens=5-7 Delims=:. " %%d in ('echo. ^| time ^| findstr /i /v enter') do set datetime=%%a%%b%%c%%d%%e%%f
for /L %%g in (1 1 10) do if exist ERRORLOG.%%g echo Processing ERRORLOG.%%g &ren ERRORLOG.%%g *.%datetime% &goto :EOF
echo No valid files, or they have allready been renamed.

This renames the first ERRORLOG.* file with the extension of the current date/time the batch file is run in the format of MMDDYYYYhhmmss.

If this is not what you want, just let me know.
Hi Gary,
I will be using your first solution, not the last one.  The last solution only renames one file.
So please give me the code to get the datetime format as MMDDYYYYhhmmss for your first solution.

Thanks,
Amber
Hi, Amber.  I don't know any easy way to get the "seconds" out of a file timestamp.  If any other experts read this and know of a way, please let me know or feel free to jump in to solve the problem.  I can get seconds out of the CURRENT date and time, but I don't know how for the FILE date and time.

As for the rest, please let me know what you want.  Since you like the first solution better, I am guessing that you want:
to look for filenames that begin with "ERRORLOG" and have extensions of .1 through .10, and any that exist should be renamed with the extension that has the date and time of the file being renamed (and NOT current date/time) in the format of MMDDYYYYhhmmss

Another idea ... If the reason you are going with my first solution is because it processes all of the files and not just the first one, I can have the script wait 1 second between each process so the files won't have the same filename, and that way it can process all 10 files and give them all unique filenames including the seconds.

Please let me know.
Hi Gary,
You are correct, the reason why I am going with your first solution is because it processes all of the files and not just the first one. Please give me that script that wait 1 second between each process so the file won't have the same filename.  If that can be done, than I will accept your second solution.  

Thank you very much for all of your assistance,
Amber
ASKER CERTIFIED SOLUTION
Avatar of GuruGary
GuruGary
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Gary,
I tested the script, and it give message "The syntax of the command is incorrect.

Thanks,
Amber
Hi, Amber. What version of Windows are you running this under?  Can you tell how far the script got before it gave you that message?  Did you copy and paste the entier script to a new file?
Hi Gary,
I am running Microsoft Server 2003.
The following is the screen shot of the result.

Processing ERRORLOG.1
The syntax of the command is incorrect.
Processing ERRORLOG.2
The syntax of the command is incorrect.
Processing ERRORLOG.3
The syntax of the command is incorrect.
Processing ERRORLOG.4
The syntax of the command is incorrect.
Processing ERRORLOG.5
The syntax of the command is incorrect.
Processing ERRORLOG.6
The syntax of the command is incorrect.

Done - renamed 6 files.


Thanks,
Amber
Hi Gary,
I do not know what was happening this morning, but I ran your code again, and it is working fine.  It does not give the previous error anylonger.  The script that work is the following:

@echo off
setlocal
set /a renamed=0
for /L %%a in (1 1 10) do call :Process %%a
goto :End

:Process
if exist ERRORLOG.%1 echo Processing ERRORLOG.%1&ren ERRORLOG.%1 *.%date:~4,2%%date:~7,2%%date:~-4%%time:~0,2%%time:~3,2%%time:~6,2%&ping -n 2 localhost >NUL&set /a renamed+=1
goto :EOF

:End
echo.
echo Done - renamed %renamed% files.

Thank you very much for your assistance Gary,
Amber