如何在pwershell中使用txt文件的原始名称保存xml文件? [英] How can i save the xml file with the originional name of the txt file in pwershell ?

查看:71
本文介绍了如何在pwershell中使用txt文件的原始名称保存xml文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$DirVan = "C:\BestandVerplaatsen\van\*"
$DirNa = "C:\BestandVerplaatsen\naar" 
$File = Get-ChildItem $DirVan #| Where-Object {$_.Extension -eq ".txt"}

#*******************************************************************
# if files in directory copy to specific place
#*******************************************************************
foreach ($File in $DirVan)
{
#If txt files found...
    if (gci $DirVan *.txt)
    {
    #copy files with extention txt
    copy-item $DirVan $DirNa -Filter "*.txt"

    # check if file transfer is succesfull otherwise give error
    $x = Copy-Item C:\BestandVerplaatsen\van\* -filter '*.txt' -Destination 'C:\BestandVerplaatsen\naar' -PassThru -ErrorAction silentlyContinue
    if(-not $?) 
        {
        write-warning "Copy Failed"
        }
        else
        {
        write-host "Succes"

        # Create a new XML File with config root node 
        [System.XML.XMLDocument]$oXMLDocument=New-Object System.XML.XMLDocument 
        # New Node 
        [System.XML.XMLElement]$oXMLRoot=$oXMLDocument.CreateElement("config") 
        # Append as child to an existing node 
        $oXMLDocument.appendChild($oXMLRoot) 
        # Add a Attribute 
        $oXMLRoot.SetAttribute("description","Config file for testing") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Year")) 
        $oXMLSystem.SetAttribute("description","year") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Season")) 
        $oXMLSystem.SetAttribute("description","Season") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Product")) 
        $oXMLSystem.SetAttribute("description","Product") 


        # Save File as originional filename from the .txt file with extention .xml --> HELP HOW DO I DO THAT ???
        
        } 
    }  
  }


$DirVan = "C:\BestandVerplaatsen\van\*"
$DirNa = "C:\BestandVerplaatsen\naar" 
$File = Get-ChildItem $DirVan #| Where-Object {$_.Extension -eq ".txt"}

#*******************************************************************
# if files in directory copy to specific place
#*******************************************************************
foreach ($File in $DirVan)
{
#If txt files found...
    if (gci $DirVan *.txt)
    {
    #copy files with extention txt
    copy-item $DirVan $DirNa -Filter "*.txt"

    # check if file transfer is succesfull otherwise give error
    $x = Copy-Item C:\BestandVerplaatsen\van\* -filter '*.txt' -Destination 'C:\BestandVerplaatsen\naar' -PassThru -ErrorAction silentlyContinue
    if(-not $?) 
        {
        write-warning "Copy Failed"
        }
        else
        {
        write-host "Succes"

        # Create a new XML File with config root node 
        [System.XML.XMLDocument]$oXMLDocument=New-Object System.XML.XMLDocument 
        # New Node 
        [System.XML.XMLElement]$oXMLRoot=$oXMLDocument.CreateElement("config") 
        # Append as child to an existing node 
        $oXMLDocument.appendChild($oXMLRoot) 
        # Add a Attribute 
        $oXMLRoot.SetAttribute("description","Config file for testing") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Year")) 
        $oXMLSystem.SetAttribute("description","year") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Season")) 
        $oXMLSystem.SetAttribute("description","Season") 
        [System.XML.XMLElement]$oXMLSystem=$oXMLRoot.appendChild($oXMLDocument.CreateElement("Product")) 
        $oXMLSystem.SetAttribute("description","Product") 


        # Save File as originional filename from the .txt file with extention .xml --> HELP HOW DO I DO THAT ???
        
        } 
    }  
  }







推荐答案


file.BaseName +'。xml'
file.BaseName+'.xml'


这篇关于如何在pwershell中使用txt文件的原始名称保存xml文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆