# Extension problems

**URL:** <https://forums.powershell.org/t/extension-problems/19632>\
**Category:** PowerShell Help\
**Created:** [June 18, 2022, 8:18am UTC](https://forums.powershell.org/t/extension-problems/19632 "2022-06-18T08:18:07Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 18, 2022, 8:18am UTC](https://forums.powershell.org/t/extension-problems/19632/1 "2022-06-18T08:18:07Z")

</div>

```auto
$files = get-childitem 'C:\_KLARA\*'
Foreach ($filename in $files){
$extn = [IO.Path]::GetExtension($filename)
if ($extn -eq ".epub",".pdf",".txt",".mobi"){      
$filename = $filename.name.replace('.',' ').trim()
$NewFileName = $filename + $extn
}
}
Move-Item -Destination 'E:\M01_Backup\Books\$NewFileName' -force

```

A.Canticle.for.Leibowitz.epub  
A Coyote’s in the House.pdf  
CopyData.txt  
Getting Started.with.OpenOffice - Rob.mobi

```auto

```

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2022, 8:27am UTC](https://forums.powershell.org/t/extension-problems/19632/2 "2022-06-18T08:27:24Z")

</div>

What’s your question?

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 18, 2022, 8:31am UTC](https://forums.powershell.org/t/extension-problems/19632/3 "2022-06-18T08:31:09Z")

</div>

What is wrong. I don’t know how to code properly

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2022, 8:33am UTC](https://forums.powershell.org/t/extension-problems/19632/4 "2022-06-18T08:33:11Z")

</div>

So I recommend to do a big step back and start with learning the very basics of PowerShell first. This will save you from a lot of wasted time and frustration. Until that you could start with explaining what your issue is.

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 18, 2022, 8:37am UTC](https://forums.powershell.org/t/extension-problems/19632/5 "2022-06-18T08:37:26Z")

</div>

This is my way trying to learn. Learn by misstakes

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 18, 2022, 8:40am UTC](https://forums.powershell.org/t/extension-problems/19632/6 "2022-06-18T08:40:38Z")

</div>

If you want to get help you will have to elaborate a little more detailed though.

Please keep in mind: people coming here to help other do this in their spare time. So please don’t be a jerk wasting their time and energy. At least do your homework first.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 18, 2022, 8:45am UTC](https://forums.powershell.org/t/extension-problems/19632/7 "2022-06-18T08:45:53Z")

</div>

If I have done my homework I have no need to ask for PowerShell Help

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 18, 2022, 9:17am UTC](https://forums.powershell.org/t/extension-problems/19632/8 "2022-06-18T09:17:40Z")

</div>

Me helping others I see as a gift not a burden

---

<div class="post-metadata">

**Author:** ![DejvDzi](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dejvdzi/32/2460_2.png) [@DejvDzi](https://forums.powershell.org/u/DejvDzi)\
**Post date:** [June 18, 2022, 3:00pm UTC](https://forums.powershell.org/t/extension-problems/19632/9 "2022-06-18T15:00:51Z")

</div>

Hi, try this:

```auto
$files = Get-ChildItem 'C:\_KLARA\*.*'

    Foreach ($filename in $files){

    $extn = [IO.Path]::GetExtension($filename)

        if ($extn -eq ".epub",".pdf",".txt",".mobi"){ 
             
        $filename = $filename.name.replace('.',' ').trim()

        $NewFileName = $filename + $extn

        }

        Move-Item -Path $filename.FullName -Destination "E:\M01_Backup\Books\$NewFileName" -force

    }

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 19, 2022, 7:47am UTC](https://forums.powershell.org/t/extension-problems/19632/10 "2022-06-19T07:47:50Z")

</div>

Hi, sorry but I can’t get it to work.

---

<div class="post-metadata">

**Author:** ![PSCode](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pscode/32/2476_2.png) [@PSCode](https://forums.powershell.org/u/PSCode)\
**Post date:** [June 19, 2022, 8:23am UTC](https://forums.powershell.org/t/extension-problems/19632/11 "2022-06-19T08:23:23Z")

</div>

**file extension if test**  
**================**

- to use a single if construct where the code components is the same process will require the test to use **or** with each of the criteria which can be achieved using different methods

1. manually test each criteria within the **if** construct using **-or**  
you will need to modify the criteria inside the if test for any changes  
code readability can be an issue

_replace_ :

```auto
($extn -eq ".epub",".pdf",".txt",".mobi")

```

_with_ :

```auto
(($extn -eq "epub") -or ($extn -eq "pdf") -or ($extn -eq "txt") -or ($extn -eq "mobi"))

```

1. manually create a **regex -match** construct as the **if** test  
you will need to modify the criteria inside the if test for any changes  
testing values with regex uses **|** as the _or_

_replace_ :

```auto
($extn -eq ".epub",".pdf",".txt",".mobi")

```

_with_ :

```auto
($extn -match "epub|pdf|txt|mobi")

```

1. use a dynamically created **regex -match** from _array_ object  
use an array to contain extensions which are concatenated to create the test  
add or remove elements within the array for any changes  
testing values with regex uses **|** as the _or_

_add at start of script_ :

```auto
$extn_list = [string[]]@("epub","pdf","txt","mobi")

```

_replace_ :

```auto
($extn -eq ".epub",".pdf",".txt",".mobi")

```

_with_ :

```auto
($extn -match ($extn_list -join "|"})

```

- functionality with the code  
‘{0}’ -f (…) : formats the first item with the results that follow  
'($extn\_list -join ‘|’) : concatenates the array elements with | as a separator

- test code and output demonstrating this method

**code**

```auto
$tHost =
@'
==================================
Output - Test Results
==================================

Match Test Created
----- ---- -------
{0}Match : {1}

Match Results
----- -------
{2}
==================================
'@

$tItem =
@'
{0}Test : {1}{0} - Result : {2}
'@

$oNewLine = '{0}' -f ([system.environment]::NewLine)
$oTab = '{0}' -f $([system.char]9)
$rItems = $null

$tExtn = [string[]]@('file','txt','log','pdf')
$lExtn = [string[]]@('epub','pdf','txt','mobi')

$tExtn | 
    ForEach ({
        $isMatch = '{0}' -f ($($_ -match ('{0}' -f ($lExtn -join '|'))) -as [system.string])
        
        If ([system.string]::IsNullOrEmpty($rItems))
        {
            $rItems = '{0}' -f ($tItem -f $oTab,$_,$isMatch)
        }
        Else
        {
            $rItems = '{0}{1}{2}' -f $rItems,$oNewLine,($tItem -f $oTab,$_,$isMatch)
        }})

$oHost = '{0}' -f ($tHost -f $oTab,('{0}' -f ($lExtn -join '|')),$rItems)
Write-Host -Object $oHost

```

**output**

```auto
==================================
Output - Test Results
==================================

Match Test Created
----- ---- -------
	Match : epub|pdf|txt|mobi

Match Results
----- -------
	Test : file - Result : False
	Test : txt - Result : True
	Test : log - Result : False
	Test : pdf - Result : True
==================================

```

**changing the file name**  
**===================**

- I am unsure what you are trying to achieve with the new file name but the **.** needs to be escaped for it to be replaced.
- this can be achieved with a single line using the same functionality in the previous information to format an item

replace :

```auto
$filename = $filename.name.replace('.',' ').trim()
$NewFileName = $filename + $extn

```

_with_ :

```auto
$NewfileName = '{0}.{1}' -f ($fileName.Name -replace '\..*?$'),$extn

```

**code to achieve moving eBooks**  
**==========================**

- the following code will achieve identifying files with the extension used for eBooks in a folder and move it to a different folder.

```auto
# Make Changes as Required
$FileSoure = '{0}' -f 'C:\_KLARA\*.*'
$FileDestination = '{0}' -f 'E:\M01_Backup\Books'

$ListBooksExtn = [string[]]@('epub','pdf','txt','mobi')

# Process Files Testing the Meet Extension before Moving
$ListFiles = Get-ChildItem -Path $FileSource

ForEach ($ItemFile in $ListFiles)
{
    If ($ItemFile.Name -contains ('{0}' -f ($ListBooksExtn -join '|')))
    {
        $SplatMoveItem = [ordered]@{
            Path = '{0}' -f $ItemFile.FullName
            Destination = '{0}\{1}' -f $FileDestination,$ItemFile.Name
            Force = $true
        }

        Move-Item @SplatMoveItem
    }
}

```

---

<div class="post-metadata">

**Author:** ![PSCode](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pscode/32/2476_2.png) [@PSCode](https://forums.powershell.org/u/PSCode)\
**Post date:** [June 19, 2022, 8:44am UTC](https://forums.powershell.org/t/extension-problems/19632/12 "2022-06-19T08:44:47Z")

</div>

convert the code presented to pseudo code and i believe the question is requesting help to identify eBooks currently saved in one location and relocate the eBooks to another folder

yes the code provided is not gong to achieve the outcomes but it has demonstrated what the requirements are

we were all new to coding, coding processes, syntax and applying all those skills with PowerShell

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 19, 2022, 4:40pm UTC](https://forums.powershell.org/t/extension-problems/19632/13 "2022-06-19T16:40:35Z")

</div>

Whoa this will take some time to melt! Thank’s

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 20, 2022, 5:43am UTC](https://forums.powershell.org/t/extension-problems/19632/14 "2022-06-20T05:43:55Z")

</div>

Is the end result like this?

```auto
$FileSource = '{0}' -f 'C:\_KLARA\*.*'

$FileDestination = '{0}' -f 'E:\M01_Backup\Books'

$ListBooksExtn = [string[]]@('epub','pdf','txt','mobi')

$ListFiles = Get-ChildItem -Path $FileSource

    ForEach ($ItemFile in $ListFiles){
        If ($ItemFile.Name -contains ('{0}' -f ($ListBooksExtn -join '|')))
		{
            $SplatMoveItem = [ordered]@{
                Path = '{0}' -f $ItemFile.FullName
                Destination = '{0}\{1}' -f $FileDestination,$ItemFile.Name
                Force = $true
            }
    
            Move-Item @SplatMoveItem
        }
    }

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 20, 2022, 6:07am UTC](https://forums.powershell.org/t/extension-problems/19632/15 "2022-06-20T06:07:20Z")

</div>

> [@PSCode](#):
>
> `($extn -match ($extn_list -join "|"})`

This line I can’t put in place!

```auto
($extn -match ($extn_list -join "|"})

```

---

<div class="post-metadata">

**Author:** ![ingewll](https://avatars.discourse-cdn.com/v4/letter/i/4bbf92/32.png) [@ingewll](https://forums.powershell.org/u/ingewll)\
**Post date:** [June 20, 2022, 6:20am UTC](https://forums.powershell.org/t/extension-problems/19632/16 "2022-06-20T06:20:09Z")

</div>

Thanks for putting my code on the right track!!

---

<div class="post-metadata">

**Author:** ![PSCode](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pscode/32/2476_2.png) [@PSCode](https://forums.powershell.org/u/PSCode)\
**Post date:** [June 20, 2022, 8:12am UTC](https://forums.powershell.org/t/extension-problems/19632/17 "2022-06-20T08:12:53Z")

</div>

Hi ingewell

powershell also provides a default parameter to test before you commit

this can be achieved by the following modifications

line in code to change

```auto
Move-Item @SplatMoveItem

```

additional parameter

```auto
Move-Item @SplatMoveItem -WhatIf

```

this will simulate the process listing what will occur - if the results provide your needs then remove the additional parameter and execute

---

<div class="post-metadata">

**Author:** ![PSCode](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pscode/32/2476_2.png) [@PSCode](https://forums.powershell.org/u/PSCode)\
**Post date:** [June 20, 2022, 8:15am UTC](https://forums.powershell.org/t/extension-problems/19632/18 "2022-06-20T08:15:33Z")

</div>

hi ingewell

no the difference is the script no longer references the .ext of the files but the complete filename and checks if it contains one of the extensions specified

---

<div class="post-metadata">

**Author:** ![PSCode](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/pscode/32/2476_2.png) [@PSCode](https://forums.powershell.org/u/PSCode)\
**Post date:** [June 20, 2022, 8:17am UTC](https://forums.powershell.org/t/extension-problems/19632/19 "2022-06-20T08:17:51Z")

</div>

yes - i posted this with my initial response as i understood what you really were trying to achieve with the script you posted and requested assistance

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/extension-problems/19632/20 "2024-05-16T20:17:50Z")

</div>


