使用powershell批量添加Qt的文件(生成pro)

Posted WeyneChen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用powershell批量添加Qt的文件(生成pro)相关的知识,希望对你有一定的参考价值。

想使用QtCreator作为编辑器编辑keil或者IAR的工程,需要生成.pro文件,于是使用powershell批量处理。

 

 源码如下:

$incPath = dir -filter "*.h" -Recurse
$headList="",""
$pathList = "",""


foreach($fpath in $incPath)
{
    $headList+=$fpath.DirectoryName + "/" + $fpath.name
    $headList = $headList -replace "\\","/" 
}

foreach($fpath in $incPath)
{
    $pathList += $fpath.DirectoryName
    $pathList = $pathList -replace "\\","/" 
}

$txtTemp=$headList[0]

$QtProHeadFileOut="HEADERS  +="
$QtIncludePath = "INCLUDEPATH +="

$workDir=get-location
$proName = gi $workDir
$workDir =$workDir -replace "\\","/"
$workDir += "/"

$flag = 0

foreach($txt in $pathList)
{
    if($txt -ne $pathList[0])
    {
        if($txt -ne $txtTemp)
        { 
            $txtTemp=$txt    
            
            if($flag -eq 0)
            {
                $flag = 1
                $txt = $txt -replace $workDir , " "
            }
            else
            {
                $txt = $txt -replace $workDir , "               "
            }
            
            $t = $pathList[-1] -replace $workDir , "               "
            if($txt -ne $t)
            {
                $QtIncludePath += $txt + "\`n"
            }
            else
            {
                $QtIncludePath += $txt + "`n`n"
            }
        }    
    }    
}

$flag = 0
foreach($txt in $headList)
{
    if($txt -ne $headList[0])
    {
        if($flag -eq 0)
        {
            $flag = 1
            $QtProHeadFileOut += $txt -replace $workDir , " "
        }
        else
        {
            $QtProHeadFileOut += $txt -replace $workDir , "            "
        }
        
        if($txt -ne $headList[-1])
        {
            $QtProHeadFileOut += "\`n"
        }
        else
        {
            $QtProHeadFileOut +="`n`n"
        }
        
    }
}


$sourcePath = dir -filter "*.c" -Recurse
$sourceList="",""

foreach($fpath in $sourcePath)
{
    $sourceList+=$fpath.DirectoryName + "/" + $fpath.name
    $sourceList = $sourceList -replace "\\","/" 
}

$sourceTemp=$sourceList[0]

$QtProSourceFileOut="SOURCES  +="

$flag = 0

foreach($txt in $sourceList)
{
    if($txt -ne $sourceTemp)
    {
        if($flag -eq 0)
        {
            $flag = 1
            $QtProSourceFileOut += $txt -replace $workDir , " "
        }
        else
        {
            $QtProSourceFileOut += $txt -replace $workDir , "            "
        }
        
        if($txt -ne $sourceList[-1])
        {
            $QtProSourceFileOut += "\`n"
        }
        else
        {
            $QtProSourceFileOut +="`n`n"
        }
    }
}

$proString ="TARGET = " + $proName.name +"`nTEMPLATE = app`n`n"
$outfile = $proString + $QtProSourceFileOut + $QtProHeadFileOut + $QtIncludePath
$outfile | Out-File -Encoding ascii qt.pro

最后会在工作目录生成一个qt.pro的文件。

使用方法:

1,打开powershell(win + R,输入powershell)

2,粘贴上面代码

3,回车,回车

 

想要添加.cpp文件,只需要将 

$sourcePath = dir -filter "*.c" -Recurse
改成
$sourcePath = dir -filter "*.cpp" -Recurse
就可以了。

以上是关于使用powershell批量添加Qt的文件(生成pro)的主要内容,如果未能解决你的问题,请参考以下文章

Powershell-批量重命名替换文件名

使用powershell批量修改文本为utf8

简单实现批量ping并生成html报告 Powershell Code

PowerShell 批量添加域用户(Windows 2012 R2)

office 365通过PowerShell批量添加共享邮箱成员

powershell小脚本--批量添加用户属性----导出登录时间