-2

It keeps looking at your source file, then adding those changes to your output, since the source file doesn't change, it keeps adding those to the output

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

    )

    $data | 
        ForEach-Object  { 
            (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile
        }
    I used above code..trying many other way's as well..how can i make sure my file sizes are not doubled?

I don't want my file to change size..all the above changes need's to be done on one output file only

user9766188
  • 47
  • 2
  • 9

2 Answers2

1

Does this produce the expected results?

$pattern = "LACTION 'SQL\(''DWFEI'',"
$replacement = "LACTION 'SQL(''DW_FEI'',"
(Get-Content "inputfile.txt") -replace $pattern,$replacement |
  Out-File "newfile.txt"
Bill_Stewart
  • 22,916
  • 4
  • 51
  • 62
0

Put the script below in a PowerShell .ps1 file.

$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"
    },
    @{
        pattern = "LACTION 'SQL\(''DWFEI'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"

    },
    @{
        pattern = "LACTION 'SQL\(''dwfei'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''user_shared'',"
        replacement = "LACTION 'SQL(''USER_SHARED'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"

    },
    @{
        pattern = "LACTION 'SQL\(''DW_FEI_PROD'',"
        replacement = "LACTION 'SQL(''DW_FEI'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },
    @{
        pattern = "LACTION'SQL\(''SALES_MART_PROD'',"
        replacement = "LACTION 'SQL(''SALES_MART'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    },    
    @{
        pattern = "LACTION 'SQL\(''sales_mart'',"
        replacement = "LACTION 'SQL(''SALES_MART'',"
        inputFile = "C:\files\SALES_MART_input.ctg"
        outPutFile = "C:\files\SALES_MART_output.ctg"
    }
)

$data | 
    ForEach-Object  { 
        (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile
    }

Edit

The --Append flag should be present on the Out-File.

$data | 
    ForEach-Object  { 
        (Get-Content $_.inputFile) -replace $_.pattern, $_.replacement | Out-File $_.outputFile --append
    }

Edit

If you are not familiar with PowerShell, the easiest way to run this script is via the steps below.

  1. Copy/paste the PowerShell script in a file with name script.ps1.
  2. Copy/paste the content below in a file with name runscript.cmd.

    @echo off
    SET psfile="%~dp0script.ps1"
    PowerShell.exe -ExecutionPolicy ByPass -File %psfile%
    pause
    
  3. Execute runscript.cmd from the command prompt.

pfx
  • 20,323
  • 43
  • 37
  • 57
  • @user9766188 When feedback is being given on your question, you are supposed to update your question, not an answer. But no problem, we all here to learn. If the answer gives is a solution to your question you can accept otherwise you give feedback via a comment. – pfx May 29 '18 at 19:22
  • @user9766188 Please explain. – pfx May 30 '18 at 17:52
  • The code is fine, but PowerShell commands can't be executed directly from a DOS/Windows command prompt. You either save the script in a file with extension .ps1 and execute that one, or the easiest way is to execute it via a GUI tool like PowerShell ISE. Search on this site on [how to execute a PowerShell script](https://stackoverflow.com/questions/2035193/how-to-run-a-powershell-script). – pfx May 30 '18 at 18:12
  • @user9766188 Updated answer with how to run a PowerShell script, but please do some research. – pfx May 30 '18 at 18:43
  • That is as expected as you configured the outputFile '"C:\files\SALES_MART_output.ctg" to be used for each inputFile. – pfx May 30 '18 at 20:34
  • Use a different output file for each item in the array, like outPutFile = "C:\files\SALES_MART_output1.ctg", outPutFile = "C:\files\SALES_MART_output2.ctg", ect. – pfx May 30 '18 at 21:53
  • This keeps failing Get-Content : Cannot find path 'C:\files\DW_FEI_input.ctg' because it does not exist. – user9766188 May 31 '18 at 13:22
  • added image to this post – user9766188 May 31 '18 at 13:39