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
- 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 :
($extn -eq ".epub",".pdf",".txt",".mobi")
with :
(($extn -eq "epub") -or ($extn -eq "pdf") -or ($extn -eq "txt") -or ($extn -eq "mobi"))
- 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 :
($extn -eq ".epub",".pdf",".txt",".mobi")
with :
($extn -match "epub|pdf|txt|mobi")
- 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 :
$extn_list = [string[]]@("epub","pdf","txt","mobi")
replace :
($extn -eq ".epub",".pdf",".txt",".mobi")
with :
($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
$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
==================================
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 :
$filename = $filename.name.replace('.',' ').trim()
$NewFileName = $filename + $extn
with :
$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.
# 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
}
}