Solved

Batch to strip file name prefixes

Posted on 2008-09-29
9
2,363 Views
Last Modified: 2012-05-05
I have about 1500 files that are prefixed with --  "1 - "  (that is one, space, hyphen, space) up to -- "1500 - "

I need a batch to get the file name and strip out this prefix of -- number, space, hyphen, space -- essentially to rename all the files in a single directory.  I can put the batch in the same directory to make it easier.

Second part of question -- after the number is "The" or "the"  in some cases --   748 - The ... rest of name ...
So let's also strip out  "the" or "The" if it occurs just after hyphen-space (but not elsewhere in the name).

Third,  I'd like the renamed files copied to a different directory if possible -- either rename them all in place, or copy the newly named files to a different folder -- don't want 2 copies of each file in the one folder.

MANY Thanks for your help.
0
Comment
Question by:scrathcyboy
  • 5
  • 4
9 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 22599192
@echo off
setlocal enabledelayedexpansion
for /F "delims=- tokens=1*" %%F in ('dir /b "* - *") do ren "%%F - %%G" "%%G"
for %%F in ("The *") do (set n=%%F& ren "%%F" "!n:~4!"

0
 
LVL 44

Author Comment

by:scrathcyboy
ID: 22599245
wow, I would have never figured that out.  Is it safe to do * - * though?  Say there was one file without the number prefix, space, hyphen, space -- would that strip the entire file name out?  Might it be best to do a number check instead of the first asterisk ?
0
 
LVL 44

Author Comment

by:scrathcyboy
ID: 22599288
get the error -- system cannot find the file dir /b * - *

adjust it to work on all files in the current directory only, I will put the batch in where I want to strip
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 22599332
It is not 100% safe. But there is no direct way to use wildcard for numbers only, so that check could by tricky. Let's do it this way:

Since digits are "Less" then letters, this will work

@echo off

setlocal enabledelayedexpansion

for /F "delims=- tokens=1*" %%F in ('dir /b "* - *") do if "%%F" LSS "A" ren "%%F - %%G" "%%G"

for %%F in ("The *") do (set n=%%F& ren "%%F" "!n:~4!"

Open in new window

0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 68

Expert Comment

by:Qlemo
ID: 22599346
Ooops, missing quote ...




@echo off

setlocal enabledelayedexpansion

for /F "delims=- tokens=1*" %%F in ('dir /b "* - *"') do if "%%F" LSS "A" ren "%%F - %%G" "%%G"

for %%F in ("The *") do (set n=%%F& ren "%%F" "!n:~4!"

Open in new window

0
 
LVL 68

Expert Comment

by:Qlemo
ID: 22599365
I use dir /b to suppress unneeded dir info. But you will have to start it in correct directory, else rename will throw errors. But you can avoid this if you use move instead, so paths are not doing any harm.

0
 
LVL 44

Author Comment

by:scrathcyboy
ID: 22599406
I used your last one above, and get repeating message -- system cannot find the file specified.

you are getting closer though.  
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 500 total points
ID: 22600950
@~#`!  spaces ... Next try. But this one is not save, as it could not handle "10 20 - file.wav", since spaces are delimiters, too. But I suppose it will do for your requirements.


@echo off

setlocal enabledelayedexpansion

for /F "tokens=1* delims=- " %%F in ('dir /b "* - *"') do if "%%F" LSS "A" ren "%%F - %%G" "%%G"

for %%F in ("The *") do (set n=%%F& ren "%%F" "!n:~4!")

Open in new window

0
 
LVL 44

Author Closing Comment

by:scrathcyboy
ID: 31501287
Brilliant work Olemo, you got it perfect.  Thanks !!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

895 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

11 Experts available now in Live!

Get 1:1 Help Now