alpap, я бы охарактеризовал твоё вот это вот всё как скриптоложество.
Если предположить, что желанием
kristian307 было поддержка версионности в имени файла,
то сделать это можно так.
Во-первых, устоявшейся практикой является след. синтаксис:
Код
my_file.exe ==> my_file.v1.0.exe
где
v1
- это мажорная часть версии,
а
0
- это минорная часть версии
и, соответственно, инкрементироваться может как мажорная так и минорная часть.
Во-вторых, разумнее всего написать функцию, которая принимает на вход имя файла и указание на то, какую часть версии инкрементировать и возвращает новое имя с инкрементом мажорной или минорной части.
ожидаемый функционал функции
make-next
make-next | аргумент_1 | аргумент_2 | возвращаемое значение |
---|
make-next | "my_file.exe" | "major" | "my_file.v1.0.exe" |
make-next | "file.v1.0.exe" | "major" | "file.v2.0.exe" |
make-next | "file.v1.0.exe | "minor" | "file.v1.1.exe" |
make-next | c:\folder2.0\setup-1.rus.bin | major | c:\folder2.0\setup-1.rus.v1.0.bin |
make-next | просто_имя_без_расширения | major | просто_имя_без_расширения.v1.0 |
Реализация:
makenext.ps1
PowerShell |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
| function global:make-next( [string] $name, [string] $ver = "major" )
{
$dirname = [IO.Path]::GetDirectoryName($name)
$filename = [IO.Path]::GetFileName($name)
$basename = [IO.Path]::GetFileNameWithoutExtension($name)
$ext = [IO.Path]::GetExtension($name)
if ( $filename -match '(.+?)(?:\.(v\d+)(?:\.(\d+)))$' ) {
$base = $matches. Item(1)
$major = $matches. Item(2)
$minor = $matches. Item(3)
$ext = $null
}
else {
if ( $basename -match '(.+?)(?:\.(v\d+)(?:\.(\d+)))?$' ) {
$base = $matches. Item(1)
$major = $matches. Item(2)
$minor = $matches. Item(3)
}
}
if ( $major -eq $null ) {
$major = 0
}
if ( $minor -eq $null ) {
$minor = 0
}
$major = $major -replace 'v', ''
if ( $ver -ceq 'major' ) {
$major = [int]$major +1
}
if ( $ver -ceq "minor" ) {
$minor = [int]$minor +1
}
return [IO.Path]::Combine( $dirname, "$base.v$major.$minor$ext" )
} |
|
проверка:
запускается сессия powershell,
затем загрузка функции
и вызов функции
на выходе должно быть:
то есть, второй аргумент можно и опустить, по умолчанию это "major"
PowerShell |
1
| gci c:\windows\assembly\*.exe -file -recurse -depth 2 |% { (make-next $_ ) } |
|
на выходе:
Кликните здесь для просмотра всего текста
Код
C:\windows\assembly\GAC_32\ehexthost32\6.1.0.0__31bf3856ad364e35\ehexthost32.v1.0.exe
C:\windows\assembly\GAC_32\MSBuild\3.5.0.0__b03f5f7f11d50a3a\MSBuild.v1.0.exe
C:\windows\assembly\GAC_64\mcupdate\6.1.0.0__31bf3856ad364e35\mcupdate.v1.0.exe
C:\windows\assembly\GAC_64\MSBuild\3.5.0.0__b03f5f7f11d50a3a\MSBuild.v1.0.exe
C:\windows\assembly\GAC_MSIL\ComSvcConfig\3.0.0.0__b03f5f7f11d50a3a\ComSvcConfig.v1.0.exe
C:\windows\assembly\GAC_MSIL\dfsvc\2.0.0.0__b03f5f7f11d50a3a\dfsvc.v1.0.exe
C:\windows\assembly\GAC_MSIL\ehexthost\6.1.0.0__31bf3856ad364e35\ehexthost.v1.0.exe
C:\windows\assembly\GAC_MSIL\loadmxf\6.1.0.0__31bf3856ad364e35\loadmxf.v1.0.exe
C:\windows\assembly\GAC_MSIL\Narrator\6.1.0.0__31bf3856ad364e35\Narrator.v1.0.exe
C:\windows\assembly\GAC_MSIL\PresentationFontCache\3.0.0.0__31bf3856ad364e35\PresentationFontCache.v1.0.exe
C:\windows\assembly\GAC_MSIL\SMSvcHost\3.0.0.0__b03f5f7f11d50a3a\SMSvcHost.v1.0.exe
C:\windows\assembly\GAC_MSIL\WsatConfig\3.0.0.0__b03f5f7f11d50a3a\WsatConfig.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\ComSvcConfig\2f78312a89b89c6f15c73b65a78097bd\ComSvcConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\dfsvc\21a388b621904e62db92701327d74243\dfsvc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\ehExtHost32\5a06c2f9ad7b4af6d04df8f6fa780255\ehExtHost32.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\MSBuild\949b04a063ccaa0e32a32e78aef36057\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\Narrator\a29393aaeef9fdb1a98dcecfb492610a\Narrator.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\PresentationFontCac#\852417c5cb79e583437293ac4e42f076\PresentationFontCache.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\SMSvcHost\cb9c4eaa9eeb1895c3becf6700f5e39b\SMSvcHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_32\WsatConfig\e2b6b850a335ed14bd1c7aa3d10421f8\WsatConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\ComSvcConfig\3c464bbbfd3af13253ca1641b4af51ea\ComSvcConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\dfsvc\901959204b0c9720533484edfbe24673\dfsvc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\ehExtHost\10cd8a4a3a67976b62901975937f4133\ehExtHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\LoadMxf\fd5354af9140e4c8b80dc3f866a5280c\LoadMxf.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\mcupdate\386d9e87cdb393366538bd5d519ca78d\mcupdate.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\MSBuild\856055716758d16851fa6cf588b5cc1e\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\Narrator\3e7e123763372706e68e56b9d4f4c018\Narrator.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\PresentationFontCac#\a5c13775ea87d8f49401b8546a764708\PresentationFontCache.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\SMSvcHost\eaf3373ab8d2f9db12d4a5db84a54527\SMSvcHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v2.0.50727_64\WsatConfig\3a948f6e4ec4c0e6717b7df08d475fd3\WsatConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\aspnet_intern\f8714cf20b693f5f78b196ab47ff4a13\aspnet_intern.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\aspnet_merge\f19bd94d9b4f97b636473a2c9e55dbcc\aspnet_merge.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\AxImp\7bdfedc0c7b78eaf8ea00e2180555cd7\AxImp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\ComSvcConfig\2bd538d545e15452202ef3b41080e2ce\ComSvcConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\csc\1092ede6789886aa47958ba3815e98f1\csc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\csi\3ca22e3552ce8c62c9deeda70faff3db\csi.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\dfsvc\261c09179eae03d67c9b6f3e70b603bd\dfsvc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\fsc\0b0074fc4327c8c3c4515f0bf3bb4796\fsc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\fsi\5dfd4be05d921bc91a16730188606866\fsi.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\IntelliTrace\aa5726660660ccc176072193eda16362\IntelliTrace.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\InteractiveHost32\5841906226b8e16a68bb26b51b72d037\InteractiveHost32.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\InteractiveHost64\be27384bc998890fcae11b128f1bca77\InteractiveHost64.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\lc\c2cfeff844a1cc08cae0b847973b40bc\lc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\Microsoft.A75fd271a#\15b138416fe4d828d0c1061160d39503\Microsoft.Alm.Shared.Remoting.RemoteContainer.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\Microsoft.S441824ef#\c43370324b39f911c13d4380333e8b34\Microsoft.ServiceHub.Controller.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\Microsoft.W71daf281#\df459c0a2762c33e0699703f186b1751\Microsoft.Workflow.Compiler.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\MSBuild\adbd651d04b0d6f4f020f1bec8dd346c\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\MSBuild\b93c627ec2e15c2675bcc81edafb10be\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\MSBuildTaskHost\d117bbf35df3c841206e3f98b293172f\MSBuildTaskHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\MSTest\3b3bba9e55c5662216414992555b3adc\MSTest.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\PSHOST\06fc328e6f41d3c3458e489b1ddb1c38\PSHOST.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent\8fece128ea59216385ed7e79c544294c\QTAgent.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent32\77449e8726c8810c53a1a5cb14c7dd87\QTAgent32.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent32_35\015beea8d793dde06977db065f7fd01d\QTAgent32_35.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent32_40\a78369a08bb73cc6ed38675752a620ba\QTAgent32_40.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent_35\a0cfaf1d3fd78168ee11ec0541ea0a13\QTAgent_35.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTAgent_40\d562b07cffb0bb715b3aaa4f1e6d7bc0\QTAgent_40.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTDCAgent\d42539e960d24e055a064eb0a0f0fcf5\QTDCAgent.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\QTDCAgent32\4238d5fc176e1e4bdfd573f8d5abd08d\QTDCAgent32.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\ResGen\4ef3e2575ea5c739236ad865619fe96c\ResGen.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\SecAnnotate\319ad4b225eca94cc9becbb235474a3f\SecAnnotate.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\ServiceHub.d6c1bc22#\7af8fa8741f2bfa3f62345e6fdd9ef3e\ServiceHub.Host.CLR.x86.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\sgen\d5765cacc5185b174268867aa2a5a6ef\sgen.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\SMSvcHost\e88db1688b08fbb889b0b9d4b1a51493\SMSvcHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\SqlMetal\02c84380f2418e33329e860a4b1cffbd\SqlMetal.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\svcutil\357ab334b15716c08dde62789179cc7b\svcutil.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\TF\aafeda77e4d2390332e3f208a9bb9011\TF.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\TlbExp\ba8f2e42330f70fdb714c3cc255a3719\TlbExp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\TlbImp\00c3b122838f795408ee260d081cfd2d\TlbImp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\vbc\62026b11ec9a87b938b0d9ea682fe07d\vbc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\VBCSCompiler\d259d00c21ffc84aec4112faffb940f7\VBCSCompiler.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\VcxprojReader\5ac546e51a41ef726555c55c1c303fec\VcxprojReader.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\VSWebHandler\9b45e0b09ed7e28a427dfd62f40169a8\VSWebHandler.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\WinMDExp\b6210ef4cce95500970381b4fbb719c8\WinMDExp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\WsatConfig\537950d9c71af966e1d8c9deb550f842\WsatConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\wsdl\e7a9e0c4ca611c1e98af386d4d55709e\wsdl.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\XDesProc\f705352525af8e51ef8ac51aa63d855f\XDesProc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\xsd\a61ae6e115ac098d1f876e62c8aa066d\xsd.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_32\xsltc\1ca8968ecf81b92c1c879b609a0e9922\xsltc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\AxImp\ef6e7eee43bea9e5896bac8826b5a5d9\AxImp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\ComSvcConfig\9a69a26417a09c2d9d7f67bf7592bd74\ComSvcConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\csc\e58f67931c060b4d00657ad8c8bed779\csc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\csi\38d323e096f5eca85ea5e0991403aa21\csi.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\dfsvc\bb4a1994db088e84b9d383271b082250\dfsvc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\fsiAnyCpu\9167f9684f7ec8040b241b412ce07d20\fsiAnyCpu.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\InteractiveHost32\4668e44e5497ce12de25eeaa6b3e9d6d\InteractiveHost32.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\InteractiveHost64\1e546b312c2b3ff28558b2e2c73ed8d6\InteractiveHost64.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\lc\61236ca2f06ff9f1123536cbc9cd6951\lc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\Microsoft.S441824ef#\1217c6ecd4220326bdd7def907a87f5a\Microsoft.ServiceHub.Controller.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\Microsoft.W71daf281#\5ada68cfa2258a2d4e3c3779106faf9b\Microsoft.Workflow.Compiler.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\MSBuild\ead39cbb60f175010d6b3c69ad42cfaa\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\MSBuild\f4a88265ac4ad47978daef8c5482fd30\MSBuild.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\MSBuildTaskHost\e83c8f72769b5424ec395e8cc7d2390e\MSBuildTaskHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\PaintDotNet\354cef8e7ebd86b5ff5a9f0acdee93ef\PaintDotNet.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\ServiceHub.d6c1bcee#\7064305bc9656e0f8690d829e6d089a9\ServiceHub.Host.CLR.x64.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\sgen\f565672bb3a700f8af81fc34aa84559d\sgen.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\SMSvcHost\0b4d4e172e8054cb61d27f5ab9e0e445\SMSvcHost.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\TlbExp\d843192238280cc3d2f517e768dfd905\TlbExp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\TlbImp\cd00ac7293cf92156dc9c0856411bcc3\TlbImp.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\vbc\eb4087ef18c1701bf0a8df6d6b64b02e\vbc.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\VBCSCompiler\f3369cbeadd0dd5a50f491623f9fcf68\VBCSCompiler.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\WsatConfig\9683999d889dc0b8782c782e2fc1aee5\WsatConfig.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\wsdl\8133c8bdd9cd1b0c32ca016635f26d7c\wsdl.ni.v1.0.exe
C:\windows\assembly\NativeImages_v4.0.30319_64\xsd\f6cbb7a744e4b283b67864606e1d6b23\xsd.ni.v1.0.exe
Добавлено через 31 минуту
что ещё стоит упомянуть:
1) данный код кросс-платформенный, будет работать и под линуксом.
2) переделать данный код под
VisualBasic.NET - это дело 3 минут; на выходе будет быстрый make-next.exe без накладных расходов и powershell ( и, кстати, он тоже будет работать под линуксом после пересборки ).
Вернуться к обсуждению:
Скрипт для переименования файлов с сохранением расширения CMD/BAT