Solved

Quick Batch File Rename Question

Posted on 2006-10-20
6
869 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now