Solved

Batch to strip file name prefixes

Posted on 2008-09-29
9
2,375 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
[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
  • 5
  • 4
9 Comments
 
LVL 70

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
Increase Agility with Enabled Toolchains

Connect your existing build, deployment, management, monitoring, and collaboration platforms. From Puppet to Chef, HipChat to Slack, ServiceNow to JIRA, Splunk to New Relic and beyond, hand off data between systems to engage the right people.

Connect with xMatters.

 
LVL 70

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 70

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 70

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 70

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

Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

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…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

691 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