Solved

Rename file - remove trailing numbers

Posted on 2011-09-04
10
366 Views
Last Modified: 2012-05-12
I have a list of say 50 files.  Each start with a user ID followed by an underscore, three 0's and then a nine digit number.  Something like AJones_000123456789.smid;  BWilliams_000234567890.smid;  CAndrews_000345678901.smid

I want to remove the '_' and all the numbers;  rename the files to Ajones.smid; BWilliams.smid; CAndrews.smid; etc

I'd prefer, if possible, to use DOS based commands like 'For'.  Currently I'm generating a list of the files with Dir /B *.smid >>ListOfFiles.txt

BigMacinWV
0
Comment
Question by:BigmacMc
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 37

Expert Comment

by:Gerwin Jansen
ID: 36481431
Hi, how about this:

C:\Temp>for /f "tokens=1,2,3 delims=_." %f in ('dir /b *.smid') do rename %f_%g.%h %f.%h
C:\Temp>rename ajones_00012345.smid ajones.smid
C:\Temp>rename bwiliams_033412345.smid bwiliams.smid
C:\Temp>rename candrews_033564345.smid candrews.smid

Open in new window


Tested on command line, if you put it into a batch/cmd file, you have to write %%f %%g etc.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36481630
Nothing wrong with that... I had typed in this version before being delayed posting:

@echo off
for %%a in ("*.smid") do for /f "delims=_" %%s in ('echo %%a') do rename "%%~fa" "%%~s.smid"

Only real difference is rather than splitting up into the three tokens and putting back together for the "source" rename filename it uses one loop to get the full filename and then one within that to break it at the _

Steve
0
 
LVL 10

Accepted Solution

by:
ReneGe earned 500 total points
ID: 36482098
The following script will create some test files, then rename them as you required.

TEST SCRIPT
 
@ECHO OFF

SET WorkingDirectory=C:\temp\SmidFiles
IF NOT EXIST %WorkingDirectory% MD %WorkingDirectory%

CD %WorkingDirectory%

REM CREATING TEST FILES
	ECHO.>AJones_000123456789.smid
	ECHO.>BWilliams_000234567890.smid
	ECHO.>CAndrews_000345678901.smid

FOR %%A IN (*.smid) DO FOR /F "tokens=1 delims=_" %%B IN ("%%~nA") DO REN "%%~fA" "%%B%%~xA"

Open in new window


PRODUCTION SCRIPT
 
@ECHO OFF

SET WorkingDirectory=C:\temp\SmidFiles

CD %WorkingDirectory%

FOR %%A IN (*.smid) DO FOR /F "tokens=1 delims=_" %%B IN ("%%~nA") DO REN "%%~fA" "%%B%%~xA"

Open in new window


Cheers,
Rene
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36493255
To rename files on your hard drive. For example:

   AJones_000123456789.smid  -->  AJones.smid


Copy and paste the following batch file code into Notepad and save it as say, RENFILES.BAT in the folder where your .SMID files are. Then, fire up a DOS box, navigate to where your files are and run the batch file by entering the following command:

   RENFILES

 
:: -----------------------------------------------------------------

   @echo off

   for /f "tokens=1,* delims=_" %%a in ('dir /b *.smid') do (
      ren %%a_%%b %%a%%~xb
   )

:: -----------------------------------------------------------------

And that's all there is to it !!




CONSIDERATIONS...

(1) As a single line batch file command, the above would translate to the following:

   for /f "tokens=1,* delims=_" %%a in ('dir /b *.smid') do ren %%a_%%b %%a%%~xb


(2) As a single command (not batch command), this would translate to the following:

   for /f "tokens=1,* delims=_" %a in ('dir /b *.smid') do ren %a_%b %a%~xb


(3) To read filenames in from your textfile (ListOfFiles.txt) and rename the files on your hard drive you need this:

:: -----------------------------------------------------------------

   @echo off

   for /f "tokens=1,* delims=_" %%a in (ListOfFiles.txt) do (
      ren %%a_%%b %%a%%~xb
   )

:: -----------------------------------------------------------------


(4) To read in your filenames as: AJones_000123456789.smid from the hard drive and write them to your file as: AJones.smid, without renaming the files on your hard drive, you need this:

:: -----------------------------------------------------------------

   @echo off

   (for /f "tokens=1,* delims=_" %%a in ('dir /b *.smid') do (
      echo %%a%%~xb
   ))>ListOfFiles.txt

:: -----------------------------------------------------------------


Which is the same as, but faster than, this:

:: -----------------------------------------------------------------

   @echo off

   del ListOfFiles.txt 2>nul

   for /f "tokens=1,* delims=_" %%a in ('dir /b *.smid') do (
      echo %%a%%~xb >>ListOfFiles.txt
   )

:: -----------------------------------------------------------------

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 43

Expert Comment

by:Steve Knight
ID: 36527475
BigMacmc  .... you've got four ways of doing what you asked for there.  did this solve your problem or do you need more help - or have we confused you?  Don't forget to select answer(s).   steve
0
 

Author Closing Comment

by:BigmacMc
ID: 36995413
Sorry to be so late in responding, but this worked perfectly.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 36995892
I felt certain I was going to bag some points for my efforts....
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 36995918
Likewise!
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 36996561
I feel bad guys. I wish I could give you some!!

Glad I could help.

Cheers,
Rene
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction How to create multiboot configuration with XP\Vista and Windows 7 on it? And most important question - how to do this correctly so not to have any kind of nightmares we get when system gets screwed? First of all one should realize t…
As the title indicates, I have done this before. It chills me everytime I update the OS on my phone, (http://www.experts-exchange.com/articles/18084/Upgrading-to-Android-5-0-Lollipop.html) because one time I did this and I essentially had a bricked …
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 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