Solved

Quick Batch File Rename Question

Posted on 2006-10-20
6
900 Views
Last Modified: 2012-08-13
I need to rename this

1234567890123456.com-combined.log.10.19.2006

to this

ex10.19.2006.log

Via a batch file.  I am going to have a log file per day dumped into the directory, so really I just need to trim down to that date and throw ex in front and .log at the end.  The files will always be the same length.

Of course the url has been masked.  It will not be numbers.

Thanks!
0
Comment
Question by:TheShaner
  • 4
  • 2
6 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17773929
@echo off

setlocal

set fileName=1234567890123456.com-combined.log.10.19.2006

set newFileName=ex%fileName:~-10%.log

echo ren "%fileName%" "%newFileName%"
0
 
LVL 1

Author Comment

by:TheShaner
ID: 17774110
the problem with this is the file will always have a different date, so I cannot explicitly call its name.  Can the first field be made into a wildcard?  Like

set fileName=1234567890123456.com-combined.log.* or something?

0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 250 total points
ID: 17774262
Sure, no problem:

@echo off

setlocal

set fileMask=1234567890123456.com-combined.log.*

for /f "delims=" %%a in ('dir /b "%fileMask%"') do call :PROCESS "%%a"

goto :EOF

:PROCESS

set fileName=%~1
set newFileName=ex%fileName:~-10%.log

echo ren "%fileName%" "%newFileName%"
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 
LVL 1

Author Comment

by:TheShaner
ID: 17774817
Actually 2 things on this now that I tried to run it, if you dont mind answering even though I have already given out the points:

If I have multiple files in the directory, is there a way to spin through all of them?  This will just hit the first file and quit.

Secondly ... is the 10 that I see in the set newfilename command specific to the 10 in the date?  If so, that would mean this batch file would only work this month, then bomb out!

Thanks!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17775949
The 10 I have in the set statement just picks off the last 10 digits of the file name. It has nothing to do with the month. To process subdirectory's add the /s switch to the dir command like so:

for /f "delims=" %%a in ('dir /b /s "%fileMask%"') do call :PROCESS "%%a"

You might have a problem with the directory name being specified as the new name of the ren statement. I've got to go, but will address this in a little bit.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 17776351
Here you go:

@echo off

setlocal

set fileMask=1234567890123456.com-combined.log.*

for /f "delims=" %%a in ('dir /s /b "%fileMask%"') do call :PROCESS "%%a" "%%~nxa"

goto :EOF

:PROCESS

set fileName=%~2
set newFileName=ex%fileName:~-10%.log

echo ren %1 "%newFileName%"
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Suggested Solutions

Title # Comments Views Activity
robocopy for today files 14 223
Rename file if it exist (batch file) 8 94
CMD shell create dir, copy file and spawn text window 4 61
Windows Bulk File Rename 3 128
One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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