繁体   English   中英

如何将.cmd转换为PowerShell脚本?

[英]how convert a .cmd into a PowerShell script?

我有下一个文件(.cmd):

@echo off
setlocal

for /f %%b in (Branches.txt) do (
    set branches=%%b
)

call :parse "%branches%"

goto :eos

:parse

set branches=%1
set branches=%branches:"=%

for /f "tokens=1* delims=," %%a IN ("%branches%") DO (
  if not "%%a" == "" call :sub %%a
  if not "%%b" == "" call :parse "%%b"
)

goto :eos

:sub

echo Starting compilation for branch :[%1]

goto :eos

:eos
endlocal

其中“ Branches.txt”包含以下内容:

branch1,branche2,branche3,...,branchn

该脚本从Branches.txt中获取最后一行,删除字符串中的所有引号,并以逗号分隔行。 如果存在至少一个元素,它将使用第一个元素调用sub,并且如果该行在拆分时具有两个或多个元素,则将重新分析除去了第一个元素的字符串。

我需要此代码,但在PowerShell .ps1文件中

我认为这是您要寻找的开始。 考虑txt文件branch.txt的以下内容。 我引用了最后一行,以表明它们已被删除,这是该过程的一部分。

branch1,branche2,branche3
branch2,branche2,branche3
"branch3","branche2",branche3"

代码段

$path = "c:\temp\braches.txt"
$lastLine = Get-Content -Path $path | Select-Object -Last 1
$lastLine -replace '"' -split "," | ForEach-Object{
    Write-Host "Do something with: $_"
}

将产生以下控制台输出。

Do something with: branch3
Do something with: branche2
Do something with: branche3 

这将获取文件并将其内容读取为字符串数组。 我们采用最后一行,并使用-replace删除引号。 然后使用-split创建另一个数组。 然后可以分别处理该数组的每个元素。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM