Leo Torres
asked on
Powershell creating extra space
When I iterate thru list in file
This path get picked from the file:
\\pcna-02\InformationTechn ologyServi ces\Packag eMigration \SSISPacka ges\PCPRS0 1\RPTPROD\ QI\CCIP_LO AD.dtsx
gets passed to my variable as this path:
\ \ p c n a - 0 2 \ I n f o r m a t i o n T e c h n o l o g y S e r v i c e s \ P a c k a g e M i g r a t i o n \ S S I S P a c k a g e s \ P C P R S 0 1 \ R P T P R O D \ Q I \ C C I P _ L O A D . d t s x
The path with all those space don't exists so nothing gets done. The paths inside the files also change with spaces. I have included a test file
Why does it create spaces and how can I avoid that..
My code listed below..
This path get picked from the file:
\\pcna-02\InformationTechn
gets passed to my variable as this path:
\ \ p c n a - 0 2 \ I n f o r m a t i o n T e c h n o l o g y S e r v i c e s \ P a c k a g e M i g r a t i o n \ S S I S P a c k a g e s \ P C P R S 0 1 \ R P T P R O D \ Q I \ C C I P _ L O A D . d t s x
The path with all those space don't exists so nothing gets done. The paths inside the files also change with spaces. I have included a test file
Why does it create spaces and how can I avoid that..
My code listed below..
$Files = Get-ChildItem "\\pcna-02\InformationTechnologyServices\PackageMigration\ChangeFiles"
Foreach($File in $Files){
$FilePath = $File.FullName
$Packages = Get-Content $FilePath
IF($File = "FBPRODVS__FBPROD.txt" ){
$Search = "FBPRODVS\\FBPROD"
$Replacement = "FAPROD\FAPROD"
}
IF($File = "FBRPTPRODVS__FBRPTPROD.txt" ){
$Search = "FBRPTPRODVS\\FBRPTPROD"
$Replacement = "PCSQ-21\FARPTPROD"
}
IF($File = "PCSQ-03__FBRPTTEST.txt" ){
$Search = "PCSQ-03\\FBRPTTEST"
$Replacement = "PCSQ-14\FARPTTEST"
}
#Write-Host $FileName
Write-Host $FilePath
Write-Host $Search
Write-Host $Replacement
Foreach($package in $Packages){
Write-Host $package
(Get-Content $FilePath) | Foreach-Object { $_ -replace $SearchString, $Replacement } | Set-Content $FilePath
}
}
PCSQ-03--FBRPTTEST.txt
Hi,
this is due to the encoding of the file. The file is encoded "UCS-2 Little Endian" which means two bytes per character. So you perhaps might want to use the -encoding parameter when reading the file content:
HTH
Rainer
this is due to the encoding of the file. The file is encoded "UCS-2 Little Endian" which means two bytes per character. So you perhaps might want to use the -encoding parameter when reading the file content:
$Packages = Get-Content $FilePath -Encoding Unicode
HTH
Rainer
ASKER
Hi,
I tried the script change with your provided sample.
From the above screenshot I can see that at least the first lines are formatted correctly - therefore I assume that in your folder where you loop through you will have files with different encodings hence an ANSI encoded file will be formatted the wrong way.
Could you perhaps "Write-Host" the file where the chinese characters start and upload this file - just to confirm that encoding?
HTH
Rainer
I tried the script change with your provided sample.
From the above screenshot I can see that at least the first lines are formatted correctly - therefore I assume that in your folder where you loop through you will have files with different encodings hence an ANSI encoded file will be formatted the wrong way.
Could you perhaps "Write-Host" the file where the chinese characters start and upload this file - just to confirm that encoding?
HTH
Rainer
From similar issues I've seen in the past I think RainerJ is right about encoding being the issue. But I think we can correct by modifying the line
(Get-Content $FilePath) | Foreach-Object { $_ -replace $SearchString, $Replacement } | Set-Content $FilePath
to be(Get-Content $FilePath) | Foreach-Object { $_ -replace $SearchString, $Replacement } | Out-File $FilePath
The reason is that Set-Content (and Add-Content) uses ANSII as the default encoding while Out-File uses Unicode. With PS 3.0 you could also use the -encoding parameter with Set-Content to adjust this.
ASKER
OK let rethink this I dont have much time to do this
This code has worked before. This one has a \ in the search and replace string.
BUt its not working I did the fancy up above to handle all the scenarios but its not working and i really need this to work today im in a time crunch can anyone tell me whats wrong here
This code has worked before. This one has a \ in the search and replace string.
BUt its not working I did the fancy up above to handle all the scenarios but its not working and i really need this to work today im in a time crunch can anyone tell me whats wrong here
$RootString = "\\pcna-02\InformationTechnologyServices\PackageMigration\SSISPackages\"
$SearchString = "FBPRODVS\\FBPROD"
$Replacement = "FAPROD\FAPROD"
$FileName = Get-ChildItem $RootString -recurse | Select-String -pattern $SearchString | group path #| select name
#"\\pcna-02\mydocs\ltorres\My Documents\SSIS_Packages\PCSQ-02\PRODUCTION\MM_PCP_DMA_USERS.dtsx"
$FileName | Out-GridView
$FileName | Out-file -width 300 -filepath "C:\Users\ltorres\Desktop\FBPRODVS_FBPROD_exce.txt"
(Get-Content $FileName) | Foreach-Object { $_ -replace $SearchString, $Replacement } | Set-Content $FileName
ASKER
Geting this error
PS C:\Users\ltorres\Dropbox\Documents\Lotteries> C:\Users\ltorres\Dropbox\Documents\Powershell\replace_string.ps1
Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo Microsoft.PowerShell.Commands.GroupInfo
Get-Content : Cannot find path 'C:\Users\ltorres\Dropbox\Documents\Lotteries\Microsoft.PowerShell.Commands.GroupInfo' because it does not exist.
At C:\Users\ltorres\Dropbox\Documents\Powershell\replace_string.ps1:19 char:13
+ (Get-Content <<<< $FileName) | Foreach-Object { $_ -replace $SearchString, $Replacement } | Set-Content $FileName
+ CategoryInfo : ObjectNotFound: (C:\Users\ltorre...mands.GroupInfo:String) [Get-Content], ItemNotFoundException
+ FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
The error is because of what you're setting the $filename variable to in line 5.
Can you provide a sample input file that we can replicate the issue with?
Can you provide a sample input file that we can replicate the issue with?
ASKER
The FBRPTPRODVS\FBRPTPROD
should be replaced by
FAPROD\FAPROD
in this file and in all files that are in the directory
CCIP-LOAD.txt
should be replaced by
FAPROD\FAPROD
in this file and in all files that are in the directory
CCIP-LOAD.txt
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
\\pcna-02\InformationTechn
You can remove the whitespace by
$Filepath=$Filepath.Replac