• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 402
  • Last Modified:

Rename file - remove trailing numbers

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
BigmacMc
Asked:
BigmacMc
  • 3
  • 2
  • 2
  • +2
1 Solution
 
Gerwin Jansen, EE MVETopic Advisor Commented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
ReneGeCommented:
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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Paul TomasiCommented:
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
 
Steve KnightIT ConsultancyCommented:
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
 
BigmacMcAuthor Commented:
Sorry to be so late in responding, but this worked perfectly.
0
 
Paul TomasiCommented:
I felt certain I was going to bag some points for my efforts....
0
 
Steve KnightIT ConsultancyCommented:
Likewise!
0
 
ReneGeCommented:
I feel bad guys. I wish I could give you some!!

Glad I could help.

Cheers,
Rene
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now