研究了powershell一个星期,想写点东西练练手。工作中经常需要将文件从美术目录拷贝到程序目录,全部复制不合适,因为经常有许多美术资源是你不想要同步的。所以需要一个方便的手动选择+自动拷贝流程,totalcommand的命令+powershell 刚好能很好的实现这个需求
实现步骤:
1.total commander 自定义命令设置
这个可以在网上搜索到很多,比如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‘ $target = ‘D:\test‘ foreach ($fullname in $args) { $item = Get-Item $fullname
##取得相对路径
Set-Location $target $relativePath = $item | Resolve-Path -Relative $sourcefilepath = Join-Path $source $relativePath -resolve
##拷贝 if(Test-Path $sourcefilepath) { Write-Host("COPY " + $sourcefilepath + " -> " + $fullname) Copy-Item $sourcefilepath -Destination $fullname } else { Write-Host("ERROR:can‘t find " + $sourcefilepath) } } Write-Host("DONE ") Start-Sleep -s 1
以上两步都配置好后,在total commander中选择文件后按你设置的快捷键就可以方便的自动替换文件了
totalcommand + powershell 实现自动替换选定文件
原文:http://www.cnblogs.com/redeye821/p/3551680.html