Solved

Batch to strip file name prefixes

Posted on 2008-09-29
9
2,357 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
Comment Utility
@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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
@~#`!  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
Comment Utility
Brilliant work Olemo, you got it perfect.  Thanks !!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

743 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

18 Experts available now in Live!

Get 1:1 Help Now