0

I want to compare files with same name but with different extensions present in the same directory.

Example: There are 6 files at a location.

1234.pdf

1234.xml

abcd.pdf

abcd.xml

5678.pdf

efgh.xml

I want to compare all the .pdf/.xml files with the files with .xml/.pdf extension which have a same name and find out if any of the .pdf or .xml file is missing like here in the above example the 5678.xml file is missing and efgh.pdf file is missing.

I tried to copy all the .pdf files in a text file and all the .xml files in another text file and tried comparing the strings within them but it isn't working.

Can anyone please let me know how can i compare the file names with different extensions?

1 Answer 1

1
Push-Location "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"
Get-childitem *.xml | ForEach { 
  if (!(test-path "$($_.BaseName).pdf")){ 
    "$($_.BaseName).pdf missing"
  }
} 
Get-childitem *.pdf | ForEach { 
  if (!(test-path "$($_.BaseName).xml")){ 
    "$($_.BaseName).xml missing"
  }
} 
Pop-Location
Sign up to request clarification or add additional context in comments.

5 Comments

I tried the code provided by you but it is just listing out all the files names with suffix "missing". Code: Get-childitem -Path "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test"*.xml | ForEach { if (!(test-path "$($_.BaseName).pdf")){ "$($_.BaseName).pdf missing" } } Get-childitem -Path "\\Cifintgfsfs001\gfs\MXPDFXML\Data\Test" *.pdf | ForEach { if (!(test-path "$($_.BaseName).xml")){ "$($_.BaseName).xml missing" } } Output: abcd.pdf missing 1234.pdf missing abcd.xml missing 1234.xml missing 6789.xml missing
In the above example only "6789.pdf" missing should have been the output because rest all the files have a corresponding .pdf or .xml file. Can you please guide me on this?
The code assumes the files in the current folder. I'll take a look into your code.
@ShaikShahabuddin It's easier to temporarily change the location - otherwise test-path has to get a complete path with Join-Path. See above
Thanks a Lot for your help. The code provided by you is working.

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.