PDA

View Full Version : .bat file to remove part of a file name


Antoniohawk
01-03-2005, 02:13 AM
Is it possible to have a .bat file remove part of the name of a file?

For example, to change

3 Doors Down - Away From The Sun - Changes
to
3 Doors Down - Changes

oracleguy
01-03-2005, 06:06 AM
A VBS script could do it with the FileSystemObject and some regular expressions.

Antoniohawk
01-03-2005, 07:11 PM
Where were you when I asked last night Kevin lol? I guess that I'll have to learn a little vb then.

oracleguy
01-04-2005, 05:04 AM
I don't recall you asking me... lol...

But the regex to do it would be: ^(.*) - (.*) - (.*)$

Then just use submatches to get the two parts you want out of it.

Celtboy
01-04-2005, 06:35 AM
or you could probably use a great app like "Tag Scanner." Tis freeware. i have a copy.

that is, of course, if you're editing mp3 files like that.

-Celt.

ps. I love that album.

codegoboom
01-07-2005, 08:59 AM
I guess that I'll have to learn a little vb then.

I feel your pain. :D

Actually, JScript can automate the various Windows automation object models.

The Shell (http://msdn.microsoft.com/library/en-us/shellcc/platform/shell/reference/objects/objects.asp) object may be fastest for this sort of thing, because it can shrink the collection down for you via the Filter() method, so if you have numerous/diverse files in a directory, that would prevent you having to test each and every one.

Here's an example of filtering:

<job>
<object id="IShellDispatch" progid="Shell.Application" />
<object id="WshShell" progid="WScript.Shell" />
<runtime>
<description>Shell Object file renaming example</description>
<usage>Drag &amp; Drop a folder onto the script file, run it with an address parameter, or choose from the dialog to follow...</usage>
</runtime>
<script language="JScript">
var WshArguments = WScript.Arguments;
var Folder;
try
{
Folder = IShellDispatch.NameSpace(WshArguments.Item(0));
}
catch(e)
{
WshArguments.ShowUsage();
Folder = IShellDispatch.BrowseForFolder(0, "Select a folder in which files are to be renamed:", 0x0200);
if(!Folder)
WScript.Quit();
}
var Items = Folder.Items();

// the filter for file enumeration (for all extensions, use: "* - * - *.*")
var bstrFilter = "* - * - *.mp3;* - * - *.wma";

var grfFlags = 0x0040; // SHCONTF_NONFOLDERS: this filters only files.

Items.Filter(grfFlags, bstrFilter);

var n = Items.Count;
if(!n)
{
WScript.Echo("No files matching the filter were found.");
WScript.Quit();
}
if(WshShell.Popup("The script will rename " + n + " files.", 0, Folder.Title, 0x00040131) != 1)
WScript.Quit();

// the regular expression for name changing
var myRegExp = /^(.*? - ).*? - (.*)/;

var i, e = new Enumerator(Items);
for(;!e.atEnd();e.moveNext())
{
i = e.item();

// the test script just alerts what the file would be renamed to:

WScript.Echo(i.Name.replace(myRegExp,"$1$2"));

// change the above line to the following, for actual renaming:

// i.Name = i.Name.replace(myRegExp,"$1$2");
}
WScript.Echo("Operation complete: " + n + " files were renamed. :)");
</script>
</job>

(save as *.wsf)
This only operates on the top level directory (excluding sub-folders); for that, you may want to filter all folders into an array first...

all of the grfFlags constants, btw:

// IShellFolder::EnumObjects grfFlags bits
cpp_quote("typedef enum tagSHCONTF")
cpp_quote("{")
cpp_quote(" SHCONTF_FOLDERS = 0x0020, // only want folders enumerated (SFGAO_FOLDER)")
cpp_quote(" SHCONTF_NONFOLDERS = 0x0040, // include non folders")
cpp_quote(" SHCONTF_INCLUDEHIDDEN = 0x0080, // show items normally hidden")
cpp_quote(" SHCONTF_INIT_ON_FIRST_NEXT = 0x0100, // allow EnumObject() to return before validating enum")
cpp_quote(" SHCONTF_NETPRINTERSRCH = 0x0200, // hint that client is looking for printers")
cpp_quote(" SHCONTF_SHAREABLE = 0x0400, // hint that client is looking sharable resources (remote shares)")
cpp_quote(" SHCONTF_STORAGE = 0x0800, // include all items with accessible storage and their ancestors")
cpp_quote("};")
typedef DWORD SHCONTF;

Roelf
01-07-2005, 09:59 AM
or download Total Commander (from www.ghisler.com ) a great file manager tool with multi file rename option where you can use regular expressions to determine the new name
it is shareware, but the only limitation when you dont have a registered copy is that you have to click away a screen on startup.