* Persistence - values are saved before exiting and loaded on the script startup
* Default input pattern changed - now you can add simply prefix and postfix to name and extension without changing Input pattern, e.g.:
. It wasn't possible in previous version.
I encountered the crash of Salamander sometimes what was described before my post and value persistence ddin't work for me. So probably it's caused by Automation bug.
Code: Select all
/* AutoRename v.1.2
* (c) 2010 p4ul
*
* Altap Salamander Automation Script
* Licensed under MIT
*
* History:
* 31.03.2010 - v.1.2 - persistence, index starts from 1 now, default input pattern changed
* 30.03.2010 - v.1.1 - date added, directory support, bugs fixed
* 29.03.2010 - v.1.0 - first script version
*/
var scriptName;
var inputPattern;
var outputPattern;
var modifiersGlobalMatch;
var modifiersCaseInsensitiveMatching;
var inputRegExp;
var fso;
var m = [];
// Script init
function Init()
{
scriptName = 'Auto Rename v.1.2';
inputPattern = '(.*)\\.(.*)';
outputPattern = '$1.$2';
modifiersGlobalMatch = false;
modifiersCaseInsensitiveMatching = true;
fso = new ActiveXObject('Scripting.FileSystemObject');
m['$'] = [1, 1, 1, 1, 1, 1];
m['i'] = [0, 2, 0, 2, 2, 0];
m['d'] = [0, 5, 0, 0, 5, 0];
m['0'] = [0, 4, 0, 3, 0, 0];
m['1'] = [0, 3, 0, 3, 3, 0];
m['2'] = [0, 3, 0, 3, 3, 0];
m['3'] = [0, 3, 0, 3, 3, 0];
m['4'] = [0, 3, 0, 3, 3, 0];
m['5'] = [0, 3, 0, 3, 3, 0];
m['6'] = [0, 3, 0, 3, 3, 0];
m['7'] = [0, 3, 0, 3, 3, 0];
m['8'] = [0, 3, 0, 3, 3, 0];
m['9'] = [0, 3, 0, 3, 3, 0];
}
// Indents text from left
function Indent(chr, totalCount, text)
{
if (totalCount < 2 || totalCount <= text.length) return text;
if (totalCount > 99) totalCount = 99;
var cnt = parseInt(totalCount) - ('' + text).length;
for (var i = 0; i < cnt; i++) text = chr + text;
return text;
}
// Gets date
function GetDateString(chr, totalCount)
{
var dt = new Date();
return Indent(chr, totalCount, dt.getDate()) + '.' + Indent(chr, totalCount, ''+(1+dt.getMonth())) + '.' + dt.getFullYear();
}
// Replaces $i and $d entities
function ProcessString(str, index)
{
var state = 0, output = '', c, zero, num, prevState = 0, cache = '';
for (var i = 0; i < str.length; i++)
{
c = str.charAt(i);
if (m[c] != null) state = m[c][state]; else state = 0;
if (state == 1) { output += cache; cache = ''; }
cache += c;
if (state == 1) { zero = false; num = ''; } // init
if (state == 3) num += c; // number
if (state == 4) zero = true; // zero
if (state == 2) { output += Indent(zero ? '0' : ' ', num, (index + 1)); state = 0; cache = ''; } // index
if (state == 5) { output += GetDateString('0', zero ? 2 : 0); state = 0; cache = ''; } // date
}
if (cache.length > 0) output += cache;
return output;
}
// Renames file name
function RenameFileName(index, originalFileName)
{
var pattern = ProcessString(outputPattern, index);
name = originalFileName.replace(inputRegExp, pattern);
return name;
}
// One file item processing
function ProcessItem(i, item)
{
var newFileName = RenameFileName(i, item.Name);
var f = (fso.FileExists(item.Path)) ? fso.GetFile(item.Path) : fso.GetFolder(item.Path);
if (newFileName != f.name) f.name = newFileName;
}
// Selected file(s) processing
function ProcessSelected()
{
try {
var items = null;
if (Salamander.SourcePanel.SelectedItems.Count == 0)
{
if (Salamander.SourcePanel.FocusedItem.Name != '..')
{
ProcessItem(0, Salamander.SourcePanel.FocusedItem);
} else items = Salamander.SourcePanel.Items;
} else items = Salamander.SourcePanel.SelectedItems;
if (items != null)
{
var i = 0;
for (var itemsE = new Enumerator(items); !itemsE.atEnd(); itemsE.moveNext())
{
var item = itemsE.item();
if (item.Name != '..') ProcessItem(i++, item);
}
}
}
catch(err)
{
Salamander.MsgBox('Unable to process the file(s)!\nError: ' + err.description, 16, scriptName);
}
}
// Loads settings
function LoadSettings()
{
var val;
val = Salamander.GetPersistentVal("AutoRename.InputPattern"); if (val != null) inputPattern = val;
val = Salamander.GetPersistentVal("AutoRename.OutputPattern"); if (val != null) outputPattern = val;
val = Salamander.GetPersistentVal("AutoRename.MGlobalMatch"); if (val != null) modifiersGlobalMatch = val == 1 ? true : false;
val = Salamander.GetPersistentVal("AutoRename.MCaseInsensitiveMatching"); if (val != null) modifiersCaseInsensitiveMatching = val == 1 ? true : false;
}
// Saves settings
function SaveSettings()
{
Salamander.SetPersistentVal("AutoRename.InputPattern", inputPattern);
Salamander.SetPersistentVal("AutoRename.OutputPattern", outputPattern);
Salamander.SetPersistentVal("AutoRename.MGlobalMatch", modifiersGlobalMatch ? 1 : 0);
Salamander.SetPersistentVal("AutoRename.MCaseInsensitiveMatching", modifiersCaseInsensitiveMatching ? 1 : 0);
}
// Shows dialog
function ShowDialog()
{
var fm = Salamander.Forms.Form(scriptName);
fm.inputPatternLbl = Salamander.Forms.Label('Input pattern:');
fm.inputPattern = Salamander.Forms.TextBox(inputPattern);
fm.outputPatternLbl = Salamander.Forms.Label('Output pattern:');
fm.outputPattern = Salamander.Forms.TextBox(outputPattern);
fm.modifiersLbl = Salamander.Forms.Label('Modifiers:');
fm.modifiersGlobalMatch = Salamander.Forms.CheckBox('Global match');
fm.modifiersGlobalMatch.Checked = modifiersGlobalMatch;
fm.modifiersCaseInsensitiveMatching = Salamander.Forms.CheckBox('Case-insensitive matching');
fm.modifiersCaseInsensitiveMatching.Checked = modifiersCaseInsensitiveMatching;
fm.okBtn = Salamander.Forms.Button("Ok", 1);
fm.cancelBtn = Salamander.Forms.Button("Cancel", 2);
while (true)
{
if (fm.Execute() == 1)
{
inputPattern = fm.inputPattern.Text;
outputPattern = fm.outputPattern.Text;
modifiersGlobalMatch = fm.modifiersGlobalMatch.Checked;
modifiersCaseInsensitiveMatching = fm.modifiersCaseInsensitiveMatching.Checked;
var modifiers = '';
if (modifiersGlobalMatch) modifiers += 'g';
if (modifiersCaseInsensitiveMatching) modifiers += 'i';
try {
inputRegExp = new RegExp(inputPattern, modifiers);
}
catch(err)
{
Salamander.MsgBox('Input pattern is wrong (check it for missing brackets for instance)!', 64, scriptName);
continue;
}
SaveSettings();
return true;
}
return false;
}
}
// Entry function of automation script
function Main()
{
Init();
LoadSettings();
if (ShowDialog()) ProcessSelected();
}
Main();