Solved

Rename file - remove trailing numbers

Posted on 2011-09-04
10
367 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 tutorial demonstrates a quick way of adding group price to multiple Magento products.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 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

16 Experts available now in Live!

Get 1:1 Help Now