Solved

Rename file - remove trailing numbers

Posted on 2011-09-04
10
381 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
[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
  • 3
  • 2
  • 2
  • +2
10 Comments
 
LVL 38

Expert Comment

by:Gerwin Jansen, EE MVE
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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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
 
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

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
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 …
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

688 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