Author Posts

May 22, 2015 at 1:14 am

i have 10 txt files in folder i want copying those files in folders on another location and if statment is name folders must be same like contain in files. then if contain file for example john smith, this file wil copy in folder with name john smith

May 22, 2015 at 4:02 am

i found solution

{$folder = get-childitem V:\ ;
$file = get-childitem c:\test\ -filter *.txt -recurse; 
foreach ($foldername in $folder)
{
foreach ($filename in $file)
{
$filecopy = $filename | get-content -filter $foldername.Name;
if ($filecopy -eq $foldername.Name)
{
copy-item $filecopy.PSPath $foldername.FullName;
}
}
}}

but how read pdf files instead txt files.
i have code for reading pdf's

{[System.Reflection.Assembly]::LoadFrom("C:\Scripts\PdfToText\itextsharp.dll")
Add-Type -Path 'C:\Scripts\PdfToText\itextsharp.dll'

$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList "$pwd\test.pdf"

for ($page = 1; $page -le $reader.NumberOfPages; $page++) {
 $lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"
 foreach ($line in $lines) {
  if ($line -match "^\[") {   
   $line = $line -replace "\\([\S])", $matches[1]
   $line -replace "^\[\(|\)\]TJ$", "" -split "\)\-?\d+\.?\d*\(" -join ""
  }
 }
}}

how connect both codes?

May 22, 2015 at 11:27 am

You could try something like this. This is NOT tested, but should be close to the logic. Keep in mind you will need to download the DLL referenced and should test just opening a PDF before trying a more advanced loop like this:

[System.Reflection.Assembly]::LoadFrom("C:\Scripts\PdfToText\itextsharp.dll")
Add-Type -Path 'C:\Scripts\PdfToText\itextsharp.dll'
 
$destinationFolders = Get-ChildItem V:\ -Directory
$files = Get-ChildItem C:\Test -Filter *.pdf -Recurse -File

foreach ($file in $files) {
    #Reset flag for match for each file
    $foundMatch = $false
    #Open the file
    $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file.FullName
    #Set label for page loop so we can exit when match found
    :PageLoop for ($page = 1; $page -le $reader.NumberOfPages; $page++) {
        $lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"
        foreach ($line in $lines) {
            #Loop through all folder names to look for matches in the file
            foreach ($folder in $destinationFolders) {  
                #Look for a Regex match for the folder name
                if ($line -match $folder.Name) {   
                    #Set flag for match to true
                    $foundMatch = $true
                    #Get folder info
                    $destPath = $folder.FullName
                    $destFolder = $folder.Name
                    #Break page loop, but stay in file loop
                    break :PageLoop
                }
            } #for each folder in destination folders
        } #for each line in lines
    }#for each page in pdf
    #Close the opened file
    $reader.Close()
    if ($foundMatch -eq $true) {
        "Match found for {0} in file {0}" -f $folder.Name, $file
        Copy-Item -Source $file.FullName -Destination $destPath -Whatif
    }
    else {
        "No matches for any folder names in file {0}" -f $file
    }
 } #for each file in files

May 22, 2015 at 1:04 pm

If I understand before applying your script all pdf files should open. If I have for example 250 files this is not useful.

May 22, 2015 at 1:58 pm

No, you do not understand. The script will go through each file, open it and read line by line, page by page to look for the folder name (e.g. John Smith). On each line, it will search for every folder name to see if there is a match. When it finds a match, it will exit the for loop, close the file and copy the file to the destination folder. Otherwise, if you have a 100 page PDF and find a name match on line 1, it still would continue to search the entire document even thought a match was found. The script won't copy or do anything because the Copy-Item currently has the -WhatIf switch so that you can validate the logic is working as expected

June 8, 2015 at 1:18 pm

I tested your code but he isn't working every time I receive message no matches for any folder name in files. I have folder name in files

June 8, 2015 at 1:52 pm

Goran, is it possible for you to reformulate your initial question please? I try to understand what you are asking, but I am confused. I think the solution Rob offered is working, but to be sure it meets your needs, and we are of any help, we need to understand the question crisp and clear.

June 8, 2015 at 9:43 pm

again, i have 10 pdf files that contain each first name and last name. i have 10 folders with folder names first name and last name.i need script that find first name and last name for example John Smith in to file and copy them in folder with folder name John Smith etc. Rob's solution just shows the message that no files that contains folder name every time.
PDF documents have just one page. on this page is first name and last name some person.

June 8, 2015 at 10:02 pm

rob's script give me next message
PS C:\Users\administrator.DRI> .\extpdf-ext.ps1

GAC Version Location
— ——- ——–
False v2.0.50727 C:\Scripts\PdfToText\itextsharp.dll
No matches for any folder names in file 1.pdf
No matches for any folder names in file 2.pdf
No matches for any folder names in file Document1.pdf

June 9, 2015 at 12:41 am

i changed rob's code because content of $line is not equivalent $folder.Name. Changed code is

[System.Reflection.Assembly]::LoadFrom("C:\Scripts\PdfToText\itextsharp.dll") | Out-Null

Add-Type -Path "C:\Scripts\PdfToText\itextsharp.dll"

 

$destinationFolders = Get-ChildItem C:\pdftest\

$files = Get-ChildItem C:\test\ -Filter *.pdf -Recurse -File

 

foreach ($file in $files) {

    #Reset flag for match for each file

    $foundMatch = $false

    #Open the file

    $reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file.FullName

    #Set label for page loop so we can exit when match found

    $page = 1;

        $lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"

       foreach ($folder in $destinationFolders) { 

        $destPath = $folder.FullName

                    $destFolder = $folder.Name

        foreach ($line in $lines) {

        if ($line -match "^\[") {   

   $line = $line -replace "\\([\S])", $matches[1]

 

   $line -replace "^\[\(|\)\]TJ$", "" -split "\)\-?\d+\.?\d*\(" -join ""

   $line = $line.split('[')

 

      #Loop through all folder names to look for matches in the file

             $line[0]

             $folder.Name

 

                #Look for a Regex match for the folder name

                if ($line[0] -eq $folder.Name) {   

 

 

                    Copy-Item -Source $file.FullName -Destination $destPath -WhatIf

 

 

                }

            } 

        } #for each line in lines

   }

    #Close the opened file

    $reader.Close()

 

 } #for each file in files

output changed script is

PS C:\Users\administrator.DRI> .\extpdf-ext.ps1
aleksandar djuric

1
aleksandar djuric

aleksandar babovic
aleksandar djuric

aleksandar djuric
aleksandar djuric

goran ljubic

but the copying is not working why?

June 9, 2015 at 2:57 am

changed code works the copying

[System.Reflection.Assembly]::LoadFrom("C:\Scripts\PdfToText\itextsharp.dll") | Out-Null

Add-Type -Path "C:\Scripts\PdfToText\itextsharp.dll"

$destinationFolders = Get-ChildItem C:\pdftest\

$files = Get-ChildItem C:\test\ -Filter *.pdf -Recurse

foreach ($file in $files) {

#Open the file

$reader = New-Object iTextSharp.text.pdf.pdfreader -ArgumentList $file.FullName

#Set label for page loop so we can exit when match found

$page = 1;

$lines = [char[]]$reader.GetPageContent($page) -join "" -split "`n"

foreach ($folder in $destinationFolders) {

$destPath = $folder.FullName

$destFolder = $folder.Name

foreach ($line in $lines) {

if ($line -match "^\[") {

$line = $line -replace "\\([\S])", $matches[1]

$line = $line -replace "^\[\(|\)\]TJ$", "" -split "\)\-?\d+\.?\d*\(" -join ""

$line = $line.split('[')

if ($line[0].Trim() -eq $folder.Name.Trim()) {

#Set flag for match to true

"the file is copying"

Copy-Item $file.FullName $destPath -force

}

}

} #for each line in lines

}

#Close the opened file

$reader.Close()

} #for each file in files

but i have one more problem. in my file there's serbian cyrilic letters what i do that i can compare text in file and folder name?
Output of $line[0] is

goran ljubi)278]TJ and folder name is goran ljubić

June 10, 2015 at 3:32 am

problem is complicated with uppercase letters. how resolve this problem?