There is more to this question.
We have Inventor files and all other kinds of files in EngDesignLibrary.
There are .idw that also exist in .pdf format.
The .pdf file can have exactly the same name as the .idw file name or not.
This type of file will definitely have the FileName of the .idw but it can have characters added to the left or the right of it. And characters could be literally any type, alphanumeric, parenthesis, hyphen, comma, special characters, there isn't any pattern.
The end result needs to have the following kind of files while keeping the directory structure:
- FileName.idw is not exactly like FileName.pdf. Basically, when
FileName.idw -like "*FileName*.pdf"I want these. - All the other kind of files but excluding the following: (2.1) Where extensions are ('.ipt', '.iam', '.ipn', '.idw') (2.2) Where
.idwfilname is exactly the same as the.pdf. Basically, whereFileName.idw -like FileName.pdfshould be excluded.
To the above result I get the files in two parts.
Part1 is where .idw file name is not exactly the same as .pdf' and .idw is exactly same as .pdf
Part2 is where I exclude the files that I don't need plus the files from Part1
The issue is that in Part2 I am also getting the .pdf equivalents of .idw
Script
$sourceFolder = "F:\Departments\EngDesignLib\"
$fileExtensions = @('.ipt', '.iam', '.ipn', '.idw')
$destination = "\\SharedFolder\Eng_3\All_Pdfs"
$csvFilePath = "C:\Users\Desktop\CSVOutput\MoveTo03\MoveTo03_1.csv"
Get-ChildItem -Path $sourceFolder -File -Recurse | ForEach-Object{
$file = $_
$extension = $_.Extension
$baseName = $file.Basename
$filename = $file. Name
#Part 1
If ($_.Extension -in '.idw'){
#change the extension of the file in the path itself
$pdfFileName = "*$basename*.pdf"
$pdfFileNameEaxctMatch = [System.IO.Path]::ChangeExtension($file.BaseName, "pdf")
$pdfFilePath = $file.fullname -Replace ($file.Name, $pdfFileName)
$pdfFilePathExactMatch = $file.fullname -Replace ($file.Name, $pdfFileNameEaxctMatch)
$csvData = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
IsInventorFile = 1
FileNamePdfOFIDW = If (Test-Path $pdfFilePath){ $pdfFileName} Else{'PDF Not Found'}
PdfFilePathOfPDFIDW = If (Test-Path $pdfFilePath){ $pdfFilePath} Else{'PDF Not Found'}
}
$csvData| Export-Csv -Path $csvFilePath -NoTypeInformation -Force -Append
}
#Part 2
If ( !( $_.Extension -in $fileExtensions ) -and ($fileName -notLike ($pdfFileName -or $pdfFileNameEaxctMatch))){
$csvData = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
IsInventorFile = 0
FileNamePdf = 'Not an Iventor File'
PdfFilePath = 'Not an Iventor File'
}
$csvData| Export-Csv -Path $csvFilePath -NoTypeInformation -Force -Append
}
}
I have tried most of he conditional operators -like, -notLike, -contains, -notcontains but it doesn't work. Also, not entirely sure how this line ` $pdfFileName = "$basename.pdf"' works. Because the output shows the file name with astrix but Test-path is true when there are no files with astrix. Also it returns files where .idw file name is exactly the same as .pdf which messes up the files I get in Part1
Output example for this particular case:
F:\Departments\Engineering\EngDesignLib\089\089705.000\089705-01.idw F:\Departments\Engineering\EngDesignLib\089\089705.000*089705-01*.pdf
How do I get files where .idw file name is not exactly same as the .pdf name? And make the filtering-out condition in Part2 work.
Considering your 2 criteria:
.idwand the exact filename with extension.pdfdoes not exist.Try this:
Explanation:
Tests that the extension is not one of the listed extensions.
I'll break this down into the 4 things it's doing:
Tests if the extension equals
.idw.Gets the full path of the file, replacing the file extension
.idwwith.pdf.Tests whether or not the replaced filepath exists (e.g.: if the same file ending in
.pdfexists).Inverses whatever the
$true/$falseresult of the tests inside the parentheses. So, if the file does have the extension.idwand the same file ending in.pdfexists, this would normally return$trueso the-notflips that to a$false, causing the wholeWhere-Objectfilter to be$false, so it will not return this file.