?
Solved

Quick Batch File Rename Question

Posted on 2006-10-20
6
Medium Priority
?
907 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 1000 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 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