Foreach multiple -replace script

I have a script I use to fix SVG file output from SolidWorks Composer. In short I have developed a pattern of trigger strings that the script will locate and replace which works as expected if there is only one file in the input folder. If there is more than one file in the input folder SVG file contents as a whole get multiplied. I’ve been staring at this script this morning trying to make it compatible with multiple input files at once and I’m not seeing how to get that out of it without breaking it. Here is the script I have to do the string replacements…

$InFolder = 'C:\Temp\SVG_IN\'
$OutFolder = 'C:\Temp\SVG_OUT\'
$Source = Get-ChildItem -Path $InFolder -file
Foreach ($file in $Source) {
echo $file.fullname
$JPEG = $file.basename +'_files'
(Get-Content $InFolder*.svg) | Foreach-Object {
$_ -replace 'ImagesFolder' , $JPEG `
-replace 'file://C:Back.svg' , 'javascript:history.go(-1)' `
-replace 'file://C:Forward.svg' , 'javascript:history.go(1)' `
-replace ':clickdown:' , '")" onmousedown="ShowToolTip(evt, ''12'',"' `
-replace ':clickup:' , '")" onmouseup="ShowToolTip(evt,''12'',"' `
-replace 'file://C:SB202KIT.pdf' , './SB-Docs\SB202-N_BLADE_ReplaceBlade_LOW.pdf' `
-replace 'file://C:SB204KIT.pdf' , './SB-Docs\SB204revA_BladeMotorCableLOW.pdf' `
-replace 'file://C:SB207KIT.pdf' , './SB-Docs\SB207_BLADE_ReplaceHub.pdf' `
-replace 'file://C:SB211KIT.pdf' , './SB-Docs\SB211_RegulatorLOW.pdf' `
-replace 'file://C:SB213KIT.pdf' , './SB-Docs\SB213_Blade_ConveyorTensionKit.pdf' `
-replace 'file://C:SB222KIT.pdf' , './SB-Docs\SB222_Blade_ShockAbsorbersLOW.pdf' `
-replace 'file://C:SB223KIT.pdf' , './SB-Docs\SB223_Blade_LASM_LoRes.pdf' `
-replace 'file://C:SB225KIT.pdf' , './SB-Docs\SB225_Blade_Waste Deflector.pdf' `
-replace 'file://C:SB228KIT.pdf' , './SB-Docs\SB228 rev A_Blade_VFD_LoRes.pdf' `
-replace 'file://C:SB230KIT.pdf' , './SB-Docs\SB230_CLS_LOW.pdf' `
-replace 'file://C:' , './'} |
Set-Content $OutFolder$file}

Hi Pj

In Line 7 you get the content of all files in the $InFolder. You most likely only want to read the content of the current file, the one you mention in line 6. This is why it seems ok when there is only one file in the input folder.

If you change line 7 from

(Get-Content $InFolder*.svg) | Foreach-Object {
to
(Get-Content $file.fullname) | Foreach-Object {
it might work for multiple files in the $InFolder

:stein

that did it… someday ill catch the small things lol.

That irks me a little that get-childitem only returns the basename when converted to a string.

mkdir foo\foo2\foo3
ls -r foo | foreach { "$_"; test-path $_ }
foo2
False
foo3
False

But all filesystem stuff is already a string.
Why would need to convert something to a string that is a string?

Lastly, I’ve not found any reason to do such a conversion, but when I use you last snippet, I get the full file name.

    ls -r d:\temp | foreach { "$_"; test-path $_ }
    Duplicates
    False
    EmptyFolder
    False
    10 passwordchangelog.txt
    False
    BusinessTest.pptx
    False
    ...

The ones with an extension are of course folders.

What PS version and what OS are you running? Just curious

I use both Windows 10 and Osx.

By fullnames I mean:

d:\temp\duplicates
d:\temp\emptyfolder
'd:\temp\10 passwordchangelog.txt'
d:\temp\BusinessTest.pptx

That’s why the results of all your test-path’s are false. get-childitem actually returns directoryinfo and fileinfo objects that get cast to string when used with test-path.

ls -r foo | foreach { $_.gettype() }

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     DirectoryInfo                            System.IO.FileSystemInfo
True     True     DirectoryInfo                            System.IO.FileSystemInfo
True     True     FileInfo                                 System.IO.FileSystemInfo

If I pipe it to get-item, the strings have the full path, even though they’re originally same types:

 ls -r foo | get-item | foreach { "$_"; test-path $_}
C:\Users\js\foo\foo2
True
C:\Users\js\foo\foo2\foo3
True
C:\Users\js\foo\foo2\foo3\hi.txt
True