0

I am trying to

  1. Create a CD_TMP file in each WE*.MS directory

  2. Set content by processing the AHD*.TPL and ADT*.TPL files

  3. Rename the AHD*.TPL to AHD*.TPL.Done and ADT*.TPL to AHD*.TPL.Done.

When there is only one WE.20150408.MS directory, the scripts works fine but when there are more than one directories (i.e. WE.20150408.MS, WE.20151416.MS,WE.20140902.MS), it does not work and gives error message:

Get-Content: An object at specified path AHD*TPL does not exist of has been filtered by the -Include or -Exclude parameter.
At C:\Temp\Script\Script.ps1:24 Char:14
+ $content = Get=Content -path $AHD
+ CatagoryInfo  :ObjectNotFound: (System.String[]:Strint[1) [Get-Content], Exception
+ FullyQualifiedErrorID:    ItemNotFound,Micorsoft.Powershell.Commands.GetContentCommand

SCRIPT:

$SOURCE_DIR = "C:\Work"
$Work_DIR = "WE*MS"
$WE_DIR = "$SOURCE_DIR\$Work_DIR"
$AHD = "AHD*TPL"
$ADT = "ADT*TPL"
$AHD_FILES = $SOURCE_DIR
$CD_TMP = "CD_TMP"
$Str1 = "TEMP"
##############
Set-Location $WE_DIR
New-Item -Path "CD_TMP" -type file  -force 
#############           
foreach ( $File in ( get-childitem -name $WE_DIR))
                {

        $content = Get-Content -path $AHD
           $content | foreach {

            If ($_.substring(0,4) -NotLike $Str1)
            {
            '0011' + '|' + 'HD' + '|' + 'AHD' + $_
             }
        } | Set-Content $CD_TMP
}

Get-ChildItem AHD*.TPL| ForEach {Move-Item $_ ($_.Name -replace ".TPL$",
".TPL.Done")}
##############
foreach ( $File in ( get-childitem -name $WE_DIR))
                {
        $content = Get-Content -path $ADT
           $content | foreach {

            If ($_.substring(0,4) -NotLike $Str1)
            {
            '0022' + '|' + 'DT' + '|' + 'ADT' + $_
             }
        } | Set-Content $CD_TMP
}

Get-ChildItem ADT*TPL| ForEach {Move-Item $_ ($_.Name -replace ".TPL$",
".TPL.Done")}

PAUSE
Matt
  • 45,022
  • 8
  • 78
  • 119
High_Tech
  • 31
  • 5

1 Answers1

0

Is it first giving the error Set-Location : Cannot set the location because path 'C:\Work\WE*MS' resolved to multiple containers. ? That's what I expect it to say when it fails.

Then, because it can't change into the folder, it can't find any AHD files.

Does it work properly for one folder? It writes the CD_TMP file for AHD files, then overwrites it for ADT files. That doesn't seem right.

Also you can make it a bit more direct by changing:

  • putting lots of things in $CAPITAL variables at the start, then using them once, or never.
  • The .substring() -notlike test to use .startswith()
  • The string building with ++++ into a single string
  • The renaming into a Rename-Item with -NewName scriptblock

I'm thinking this:

$folders = Get-ChildItem "C:\Work\WE*MS" -Directory

foreach ($folder in $folders) {

    # AHD files    
    $content = Get-Content "$folder\AHD*.TPL"
    $content = $content | where { -not $_.StartsWith('TEMP') } 
    $content | foreach {"0011|HD|AHD$_"} | Set-Content "$folder\CD_TMP" -Force

    Get-ChildItem "$folder\AHD*.TPL" | Rename-Item -NewName {$_.Name + '.Done'}

    # ADT files
    $content = Get-Content "$folder\ADT*.TPL"
    $content = $content | where { -not $_.StartsWith('TEMP') } 
    $content | foreach {"0011|HD|ADT$_"} | Add-Content "$folder\CD_TMP"

    Get-ChildItem "$folder\ADT*.TPL" | Rename-Item -NewName {$_.Name + '.Done'}

}

Although I don't know what the input or output should be, so I can't test it. NB. it now does Add-Content to append to the CD_TMP file, instead of overwriting it.

There's still alot of redundancy with $content, but the lines mostly stand alone like this.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87