Solved

Quick Batch File Rename Question

Posted on 2006-10-20
6
905 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

690 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