helb
|
Posted: Thu Oct 16, 2014 13:48 Post subject: Скрипт меняющий имена двух объектов местами |
|
|
Часто возникает необходимость поменять местами имена пары файлов/папок. Автоматизировал, сведя до одного нажатия на клавиатуре (у меня Ctrl+Shift+F11). Переименовывает либо ровно два выделенных объекта в текущей панели, либо то что под курсором в обеих.
Параметры: <имя1> <имя2> /<альтернативное имя>
Параметры в TC: %P%S /%T%M %L (последний опционален, для обновления списка в TC)
Code: | '==========================================================
' Swap names (by helb)
' Swaps two filenames, if only <file1> specified (in case of generated list) uses alternative
' Parameters: <file1> <file2> [/<alternative filename>]
' Total Commander parameters: %P%S /%T%M [%L] (two selected in current panel or under cursor in both panels, %L to refresh)
'Переименовывает либо ровно два выделенных объекта в текущей панели, либо то что под курсором в обеих.
'Параметры: <имя1> <имя2> /<альтернативное имя>
'Параметры в TC: %P%S /%T%M %L (последний опционален, для обновления списка в TC)
'==========================================================
unq1 = "swap1-e78d4204-b110-42b6-882e-c93cbab51cd7"
unq2 = "swap2-f8ea00d3-9b76-4039-89c6-8feab352c079"
with WScript.Arguments
if .length < 2 then
WScript.Echo("Insufficient parameters")
WScript.Quit
end if
orig1 = .Item(0)
if .length >= 3 and Left(.Item(1), 1) <> "/" and Left(.Item(2), 1) <> "/" or Left(.Item(0), 1) = "/" then
WScript.Echo("More than two files specified")
WScript.Quit
else
orig2 = .Item(1)
end if
end with
set FSO = CreateObject("Scripting.FileSystemObject")
if Left(orig2, 1) = "/" then orig2 = Right(orig2, Len(orig2)-1)
if objExists(orig1) = 0 or objExists(orig2) = 0 then
WScript.Echo("File not found")
WScript.Quit
end if
set f1 = getObj(orig1)
set f2 = getObj(orig2)
orig1 = f1.Name
orig2 = f2.Name
if not renameObj(f1, unq1) then WScript.Quit
if not renameObj(f2, unq2) then
f1.Name = orig1
WScript.Quit
end if
f1.Name = orig2
f2.Name = orig1
function renameObj(f, newname)
renameObj = true
on error resume next
f.Name = newname
if err.Number <> 0 then
WScript.Echo("Unable to rename: " & f.Name & vbNewLine & "Error #" & CStr(err.Number) & " " & err.Description)
renameObj = false
err.clear
end if
end function
function getObj(fl)
if FSO.FileExists(fl) then
set getObj = FSO.GetFile(fl)
elseif FSO.FolderExists(fl) then
set getObj = FSO.GetFolder(fl)
else
set getObj = nothing
end if
end function
'0=not exists, 1=is file, 2=is folder
function objExists(name)
if FSO.FileExists(name) then
objExists = 1
elseif FSO.FolderExists(name) then
objExists = 2
else
objExists = 0
end if
end function
|
|
|