totalcommand + powershell 实现自动替换选定文件

 

研究了powershell一个星期,想写点东西练练手。工作中经常需要将文件从美术目录拷贝到程序目录,全部复制不合适,因为经常有许多美术资源是你不想要同步的。所以需要一个方便的手动选择+自动拷贝流程,totalcommand的命令+powershell 刚好能很好的实现这个需求

实现步骤:

1.total commander 自定义命令设置

tc怎么设置自定义命令可以在网上搜索到很多,比如 http://fanhongtao.org/2012/12/19/add-user-command-in-tc.html

我就不累述了,直接贴我的usercmd.ini文件:

[em_synfile]
button=
cmd=powershell.exe
param="D:\work\powershell\syn.ps1 %P%S "

该命令是将选定的所有文件路径都传给powershell脚本 syn.ps1(请把D:\work\powershell\替换成你本地的路径)

 

2.syn.ps1脚本

#source是源目录 target是目标目录
$source = ('D:\test2\'|Get-Item).FullName
$target = ('D:\test\'|Get-Item).FullName
$sourceItem = Get-Item $source

foreach ($fullname in $args)
{
    $item = Get-Item $fullname
    $fullname = $item.FullName
    $inSource = $fullname.StartsWith($source)
    $inTarget = $fullname.StartsWith($target)
    #如果选择的是源目录里的文件,将这些文件拷贝到目标目录
    #如果选择的目标目录里的文件,找到源目录里的同名文件,拷贝到目标目录
    if ($inSource -or $inTarget)
    {
        $sourcefilepath = ""
        $targetfilepath = ""
        if ($inSource)
        {
            $sourcefilepath = $fullname
            Set-Location $source
            $relativePath = $item | Resolve-Path  -Relative
            $targetfilepath = [IO.Path]::Combine( $target, $relativePath)
        }
        else
        {
            $targetfilepath = $fullname
            Set-Location $target
            $relativePath = $item | Resolve-Path  -Relative
            $sourcefilepath = [IO.Path]::Combine( $source, $relativePath)
        }
 
        if(Test-Path $sourcefilepath)
        {
             Write-Host("COPY " + $sourcefilepath + " -> " + $targetfilepath)
             Copy-Item $sourcefilepath -Destination $targetfilepath
        }
        else
        {
            Write-Host("ERROR:can't find " + $sourcefilepath)
        }
    }
    
}

Write-Host("DONE ")
Start-Sleep -s 1

 

以上两步都配置好后,在total commander中选择文件后按你设置的快捷键就可以方便的自动替换文件了

 

posted @ 2014-02-16 19:29  月月减清辉  阅读(1019)  评论(0)    收藏  举报