Powershell Script, which compare Name in List ans Files in Directory

I need a powershell script which compare the name in a txt file. In the txt file have only digits. For example:
345
789
3256718
5629
etc.
I have a directory in which have a different type a file (.jpg, psd, gif usw) and the name begin always with digit. For example:
543_Nature…jpg
5629_Mountain_Valley.tif
etc.
I need a powershell script which compare the name (only digit). When found match ignore the file, when the digits not exist in the name, then copy file in other directory.

Thank you

ivcho_pifcho70

You are member of this forum for long enough to know that we will not write ready to use ocde on request. So what’s your actual question here? Please share your code. :wink:
.
If you don’t know how to start you may (re-)read the help for the following cmdlets to get some ideas how to approach this task:

1 Like

Hi Olaf,

thank you for the reminder. I wrote something but not work. I have no time to test other command. I post what i wrote. Can you or someone help me?

$strReference = Get-Content "C:\User\Administrator\Desktop\list\list.txt"
$strNameFile = Get-ChildItem -Path C:\User\Administrator\Desktop\*.* -Recurse -Force  | Where{$_.Name -match '\d+'} | ForEach{$Matches[0]}

if ($strNameFile.Name -match $strReference.Name) {
	Write host "File Exist"
	}
	else {
	Move-Item -Path  C:\User\Administrator\Desktop\File
	}

When I execute

 Get-Content "C:\User\Administrator\Desktop\list\list.txt

this list a list with number:
50955
730424
1999028
918573
3236932
836163
709501
669564
1749134
320937
793455
409383
646232
2337
When I execute this:

Get-ChildItem -Path C:\Users\Ivcho_Pifcho\Desktop\Unimark\*.* -Recurse -Force  | Where{$_.Name -match '\d+'} | ForEach{$Matches[0]}

123
23267
23911
411
456
646232
673574
673574
This are the digit in the name the fie
123.txt
23267.docx
23911.pptx
411_ivo.xlsx
456.pub
646232.bmp
673574.docx
673574_nature.docx
I want to move all file that not match the digit in the name with my list in other directory.
I hope that i am clear.

Thank you.

Hi,

I wrote new, but this not pass 100 % of my need. And so:

$aryfiles = Get-Content "C:\User\Administrator\Desktop\list\list.txtt"
$destinationDir = "C:\User\Administrator\Desktop\File"
$sourceFiles = Get-ChildItem -Path  C:\User\Administrator\Desktop\*.* -Recurse -Force  

Select-Object -ExpandProperty FullName
foreach ($sourceFile in $SourceFiles)
{
Move-Item $sourcefile -Destination $destinationDir
}

This move all file. I need to compare the name of file in C:\User\Administrator\Desktop. The begin always with digit. If the name contains a number in my list to be ignored, otherwise it will be moved to a new directory.
Thank you again.

If I got it right you could approach this task like this:

$SourceFileList = 
    Get-ChildItem -Path 'C:\User\Administrator\Desktop' -Filter *.* -Recurse -Force | 
    Select-Object -Property *, @{Name = 'Digits'; Expression = { ($_.BaseName -split '_')[0] } } 
$aryfiles = 
    Import-Csv -Path 'C:\User\Administrator\Desktop\list\list.txtt' -Header 'Digits'  #btw: the file exrension is "txtt" with a double "t". Is that on purpose? ;-) 
$ResultFileList = 
    Compare-Object -ReferenceObject $SourceFileList -DifferenceObject $aryfiles -Property 'Digits' -PassThru | 
    Where-Object -Property SideINdicator -EQ -Value '<='

Now you have all files NOT referenced by a digit from your list inside the variable $ResultFileList and can do whatever you need to do. :wink:

It appears you could have a typo with your input list in your later code compared to your earlier code. So then nothing would match and everything gets moved? But there would be an error message, so you probably know that.