使用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
就可以了。
原文地址:https://www.cnblogs.com/WeyneChen/p/5571184.html