Solved

Batch to strip file name prefixes

Posted on 2008-09-29
9
2,367 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 69

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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 69

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
 
LVL 69

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 69

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 69

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

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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