none
How do i create a file extention if the extention is empty and there is olso an xml file with the same basename in powershell RRS feed

  • Question

  • #******************************************************************* #Auteur: Casper Limburg #******************************************************************* #******************************************************************* #cleans screen #******************************************************************* cls #******************************************************************* # Declare variable & constrains #******************************************************************* $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){ $exists = $DirVan | Where-Object {$_.BaseName -eq $DirVan.BaseName} if($exists) { #If no xml files found... if (!(gci $DirVan *.xml)) { write-host "No xml files!" } #If xml files found... if (gci $DirVan *.xml) { #copy files with extention xml #copy-item $DirVan $DirNa -Filter "*.xml" | Where-Object {$_.BaseName -eq $DirVan.BaseName} # check if file transfer is succesfull otherwise give error $x = Copy-Item C:\BestandVerplaatsen\van\* -filter '*.xml' -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 $oXMLDocument.Save("C:\BestandVerplaatsen\naar\" + $x.Basename +'.trg') #$file.BaseName+'.xml' } } # Get Path Extension (hint: there is none!) $extension = [System.IO.Path]::GetExtension($File) "GetExtension('{0}') returns '{1}'" -f $DirVan, $extension if($extension.Length -le 0) { #echo file has no extention this will be created now write-host "The File has no extention, a .tif extention will be made" #echo originional file will be stored on a different location write-host "The origional File will be stored on a differend location" ## create file extention if the extention is empty and there is olso an xml file with the same name ## Help --> how do i do that

    gci C:\BestandVerplaatsen\van\* -file | where Extension -eq ""| foreach-object {Rename-Item -path "$($_.fullname)" -newname "$($_.Basename).tif"} # WRONG

    HELP #copy file to specific location write-host "The file is being copied" copy-item $DirVan $DirNa -filter "*.tif" # check if file transfer is succesfull otherwise give error $x = Copy-Item C:\BestandVerplaatsen\van\* -filter '*.tif' -Destination 'C:\BestandVerplaatsen\naar' -PassThru -ErrorAction silentlyContinue if ($x) { $x write-host "Copy succeded" # 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","Prouct") # Save File # $oXMLDocument.Save("C:\BestandVerplaatsen\naar\Gegenereerde_XML_File.xml") $oXMLDocument.Save("C:\BestandVerplaatsen\naar\" + $x.Basename +'.trg') } else { "Copy failure" } } else { #copy file to specific location write-host "The file is being copied" copy-item $DirVan $DirNa -filter "*.tif" # check if file transfer is succesfull otherwise give error $x = Copy-Item C:\BestandVerplaatsen\van\* -filter '*.tif' -Destination 'C:\BestandVerplaatsen\naar' -PassThru -ErrorAction silentlyContinue if ($x) { $x write-host "Copy succeded" # 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("Season")) $oXMLSystem.SetAttribute("description","Season") # Save File # $oXMLDocument.Save("C:\BestandVerplaatsen\naar\Gegenereerde_XML_File.xml") $oXMLDocument.Save("C:\BestandVerplaatsen\naar\" + $x.Basename +'.trg') } else { "Copy failure" } } } }


    Wednesday, June 17, 2015 12:08 PM

All replies

  • Could you simplify your problem to an example so that we don't have to read a wall of code?

    If my post was helpful, please mark it as so!

    Tuesday, July 14, 2015 12:13 PM