Solved

Batch to strip file name prefixes

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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

772 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