Solved

Quick Batch File Rename Question

Posted on 2006-10-20
6
903 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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