Powershell Array

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Dudebro Dudebro 3 months, 1 week ago.

  • Author
    Posts
  • #63973
    Profile photo of ceyon67
    ceyon67
    Participant

    I'm trying to create a script to move files and index them in sql. I've run into a roadblock. What I want to do is load an xml file for portability into an array or hash table not sure which on to use. After that I want it to parse through a file list and have it match the first 6 characters(This may vary). Then I want to be able to grab the group of values for that category and move it to that destination while still being able to access the other variables for further manipulation. This is pretty much where i'm stuck at. I've posted my code so far and the xml. Any assistance is greatly appreciated.

    #########################Hospital Configuration############################
    $cPath = "C:\move\outpatient.xml"
    [xml]$xml = New-Object -TypeName Xml
    $xml.Load($cPath)
    $hospitalList = $xml.hospitalstore.hospital
    $src = $xml.hospitalstore.src.path
    $fileList = get-childitem -path $src -File -Force -Recurse
    
    #########################End Hospital Configuration#########################
    
    
    ######################## File Move And Index ##########################
    
    foreach($hospital in $hospitalList ){
    
    $key= $file.basename.Substring(0,6)
    
    Where-Object ($hospital.category -match $key)|write-host $hospital
    
    
    
    
    
    }
    

    XML

    https://gist.githubusercontent.com/anonymous/e17cd871d55b56b24d93f143be82d58e/raw/80dcaa6f0edbadee1087324c4aafb78684554776/gistfile1.txt

  • #64029
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    I'm pretty unsure if I really got what you try to achive but at least in the code snippet you showed here you're using the variable $file but you don't declaire or fill it in. And your where-object inside your foreach loop does not seem to have any input.

    And an additional hint for the future: Do not post needless comments in your code, try to avoid empty lines if they don't seperate logical code regions and think about indentation of your code. That will make your life and ours easier.

  • #64126
    Profile photo of ceyon67
    ceyon67
    Participant

    I have an xml file with parent node hospital store and a few childnodes. The chilnodes contain Name , criteria , hospcode , category, destination . So far I have a source where it retrieves the files in that directory and then match the basename first 6 characters to in the xml childnode category. My problem is I'm unsure to associate the file with that group of nodes. I want to be able to point the file that matches the value in xml category to xml destination etc and then test destination with a few other functions. I'm not sure how to bind file.Hope this makes sense. Thanks

    $cPath="C:\move\outpatient.xml"
    $xml = New-Object -TypeName XML
    $xml.Load($cPath)
    $hName = $xml.hospitalstore.hospital
    $src = $xml.hospitalstore.src.path
    $fileList= get-childitem -path $src -File
    
    ForEach ($file in $fileList ){
    
    $category = ($file.BaseName.Substring(0,6))
    $fileName = $fileList.Name
    
    
    if ($category -in $hName.category){
    
    
    $file.Name
    $hName.category
    
    }
    
    }
    
    
    
    
    
    
  • #64149
    Profile photo of Dudebro
    Dudebro
    Participant

    Provided I have understood this correctly, you could do a nested ForEach.

    [XML] $Xml  = Get-Content -Path "C:\move\outpatient.xml"
    $SourcePath = $Xml.hospitalStore.src.path
    $Files      = Get-ChildItem -Path $SourcePath -File -Recurse
    
    ForEach ( $Hospital in $Xml.hospitalStore.hospital )
    {
    	ForEach ( $File in $Files )
    	{
    		If ( $File.BaseName -Match $Hospital.category.Substring(0,6) )
    		{
    			Write-Verbose "Moving file $($File.Name) to $($Hospital.Destination)"
    			$File | Move-Item -Destination $Hospital.destination
    		}
    	}
    }
    

You must be logged in to reply to this topic.