0

I am trying to compare 2 files in different folders but i need to use the first 8 Characters of the filename. below is what i have tried but i am not having any joy

$Source = "Path To PDF Files"
$Compare = "Path To PDF Files i Want To Comapare" 

GCI $Source | ForEach-Object{

if(Test-Path "$Compare\$($_.Name.Substring(0,8))") {

Move-Item $Compare\$($_.Name) "\\Destination\" 

    }
 }
8
  • Is the value of $Compare\$($_.Name.Substring(0,8)) what you expect ? Does that path exist ? Commented May 27, 2020 at 8:34
  • the path works when using $Compare\$($_.Name) but when i add the .Substring it does nothing. i thought by adding .Substring it would look at the first 8 Characters on both files. Commented May 27, 2020 at 8:39
  • 2
    But adding substring means you're truncating the path so the path is invalid, which is why Test-Path fails. Why do you you need the first 8 characters ? Commented May 27, 2020 at 8:43
  • 1
    write some code to check for that trailing (#) and remove it if found. THEN use that for your test. [grin] Commented May 27, 2020 at 9:08
  • 1
    @RedecIndustrial - sure ... plus, you can get rep points by working thru the Tour page for this site. [grin] Commented May 27, 2020 at 17:02

1 Answer 1

1

after @Lee_Dailey pointed me in the right direction i changed the way i went about it below is the code snippet i wrote to get around the problem.

this will look for the number added to the end of a file and remove it for example

file(1).pdf it will remove the (1)

$SignedNt = GCI "PATH TO FILES" -Filter *.pdf 
$Pattern = "[(#)]"
ForEach($Sign in $SignedNt){
    if($Sign -match $Pattern){
    Rename-Item -Path $Sign.FullName -NewName (($Sign.Name)-replace '\([0-9]\)' , '') 

    }
}

once this cleans the file it then runs the code below compare and move it which in turn answers my question.

$Source = "Path To PDF Files"
$Compare = "Path To PDF Files i Want To Compare" 

GCI $Source | ForEach-Object{

if(Test-Path "$Compare\$($_.Name)") {

Move-Item $Compare\$($_.Name) "\\Destination\" 

    }
 }

hope this helps the next person

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.