Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Rename file - remove trailing numbers

Posted on 2011-09-04
10
Medium Priority
?
387 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 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
#Citrix #POC #XenDesktop #vCenter #VMware #ESX
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.

609 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