-2

My script works fine and does changes as required. Only issue it does is my input file size remains same and output file keeps doubling. How can i stop that? Input is my orginal file!! when i compare i can see changes but my output ctg files gets doubled!! please help

$data = @(
    @{
        pattern = "LACTION 'SQL\(''logility_prod_scp_logility'',"
        replacement = "LACTION 'SQL(''LOGILITY_PROD_SCP_LOGILITY'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outputFile = "C:\files\DW_FEI_output.ctg"
    },
    @{
        pattern = "LACTION 'SQL\(''dwfei'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outputFile = "C:\files\DW_FEI_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''DWFEI'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\DW_FEI_input.ctg"
        outputFile = "C:\files\DW_FEI_output.ctg"
    }

)

$data | 
    ForEach-Object  { 
        (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile
    }
user9766188
  • 47
  • 2
  • 9
  • Save your commands in a `.ps1` file and then run the script? – BenH May 29 '18 at 15:25
  • 1
    Possible duplicate of [how can we automate below commands. Is there a efficient way to do it?](https://stackoverflow.com/questions/50471182/how-can-we-automate-below-commands-is-there-a-efficient-way-to-do-it) – Bill_Stewart May 29 '18 at 15:26
  • @Bill_Stewart It is a duplicate. – pfx May 29 '18 at 15:41
  • 1
    Correct. The "possible" wording is added automatically by the system when you flag the question as a duplicate. – Bill_Stewart May 29 '18 at 15:46

1 Answers1

0

Today, you're running this process to swap a series of tokens out in your source .ctg file. I would propose moving each of the steps into lines in a .CSV file like so:

#This
$pattern = "LACTION 'SQL\(''logility_prod_scp_logility'',"
$replacement = "LACTION 'SQL(''LOGILITY_PROD_SCP_LOGILITY'',"

We would move these two strings into columns within a .CSV file, like below. A note, since there are a lot of commas in your fields, I would recommend using a semicolon instead:

Source;Target
"LACTION 'SQL\(''logility_prod_scp_logility'',";"LACTION 'SQL(''LOGILITY_PROD_SCP_LOGILITY'',"

We can retrieve these values like so:

$strings | convertfrom-csv -Delimiter ';'

Integration

First, you should manually move all of your tokens from those nine steps into a semicolon separated value list.

Secondly, once you've done this, you can iterate through each line in the .CSV to replace all of the tokens with just six lines.

$string = Import-Csv -Delimiter ';' -Path c:\PathTo\YourFile.csv
ForEach($string in $strings){

   (Get-Content "C:\files\DW_FEI_DEV_input.ctg") -replace $string.Source,$string.Target |
      Out-File "C:\files\DW_FEI_DEV_output.ctg"

}
FoxDeploy
  • 12,569
  • 2
  • 33
  • 48
  • Can you provide me an example! This looks confusing I can only update in CTg file i beileve not in csv file that is my requirement – user9766188 May 29 '18 at 15:43