1.

Solve : trim beginning digits,symbols and space off filename?

Answer»

Hi there,

I saw a video tutorial introducing batch file a while back and it kind of piqued my interest, so here I am

I have some mp3 files that BEGIN with 01, 02, dashes and so on. Is there a command to:

1. list the files that are not BEGINNING with an alphabet
2. rename without those characters

otherwise, a line to trim the first digit would be fine.

Thanks in advance.
Quote from: mjncheers on February 09, 2009, 12:10:30 PM

otherwise, a line to trim the first digit would be fine.

Since trimming the 1st character is pretty easy, I'll give you an example on how to do that. The other can be done, but with batch the only ways I can think of to do it would take many more lines of code. Have you considered a file renaming program? My favorite is BRU or Bulk Rename Utility at http://www.bulkrenameutility.co.uk/

Anyway, here is some code. The way it is here, it will work on the current directory and all subdirectories. If you only want only the current directory, take out the /S. If there is already a file with the same name, it will probably give an error for that file and keep going. I have it ECHO the results so you can see what it will do instead of actually renaming. Bulk renaming can be dangerous, so be sure it is doing what you want it to do, and / or have a backup!
Code: [Select]@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in ('dir *.mp3 /a-d /b') do (
set CurrentFile=%%a
echo ren "%%a" "!CurrentFile:~1!"
)

If you only want to trim the 1st character if it is a digit, you will have to add some code to check for a digit ... something like replacing your REN line with
Code: [Select]set FirstChar=!CurrentFile:~1,1!
if !FirstChar! GEQ 0 if !FirstChar! LEQ 9 echo ren "%%a" "!CurrentFile:~1!"
You could do something similar to check for files that don't begin with a-z or A-Z (IF NOT statement)

Hope that helps.
I do not like that bulk rename utility. It's hard to use. It's complicated. A batch file that size could sweep the computer from head to toe. It doesn't have a very well designed interface, but it doesn't take too long to get used to.I was going to use it to rename some of my NES ROMs, but then I realized that there are goodtools for that and NSRT for SNES... so I've yet to find a reason to use it.


Not sure what you mean:
Quote from: BatchFileCommand on February 09, 2009, 07:14:08 PM
A batch file that size could sweep the computer from head to toe.

that's why he's got it set to echo the renaming behaviour rather then actually do it.Quote from: BC_Programmer on February 09, 2009, 08:21:10 PM
Not sure what you mean:
Quote from: BatchFileCommand on February 09, 2009, 07:14:08 PM
A batch file that size could sweep the computer from head to toe.

that's why he's got it set to echo the renaming behaviour rather then actually do it.



That program is like 500 kb. Not saying that it's to big for the computer. But if you had a batch file that was 500KB it could sweep the whole computer and rename tons of files.Hey, thanks guys for your help and replies.

I haven't really got that far yet...As I'm an absolute beginner, I guess it'll take me a great while to figure out and learn from your codes. But great thanks anyways, it makes a good goalpost

P.S. yes, I use a RENAMER. I picked that up from a previous FAQ post here.

Great forum anyways, keep up the good work here!

Quote from: BatchFileCommand on February 10, 2009, 06:14:34 AM
Quote from: BC_Programmer on February 09, 2009, 08:21:10 PM
Not sure what you mean:
Quote from: BatchFileCommand on February 09, 2009, 07:14:08 PM
A batch file that size could sweep the computer from head to toe.

that's why he's got it set to echo the renaming behaviour rather then actually do it.



That program is like 500 kb. Not saying that it's to big for the computer. But if you had a batch file that was 500kb it could sweep the whole computer and rename tons of files.

The rename function in ExplorerXP is excellent, not too complicated but very powerful.

Doesnt work in vista though


Discussion

No Comment Found