1
\$\begingroup\$

I need to recreate project folders containing all article numbers acquired from a list. It's searching for article names in the foldernames and inside the folders.

If the directory is found, it will copy the whole dir. If a file matching the article is found, it will copy the parent dir.

Output:

Input the project name: iFLEX
Directory already exists.
File list already exists.
Folder: H:\Sheetmetal\DMR\CUSTOMER.
Searching for files
Found [4011-511-28126]
Found in H:\Sheetmetal\DMR\CUSTOMER\H4011-511-281261 FRAME WELDED
Found in H:\Sheetmetal\DMR\CUSTOMER\H4011-511-281262 FRAME WELDED
Found in H:\Sheetmetal\DMR\CUSTOMER\H4011-511-281261 FRAME WELDED - Snelkoppeling.lnk
Failed to find dir [4011-511-18567]
Failed to find dir [4011-511-27747]
Found [4011-511-18803]
Found in H:\Sheetmetal\DMR\CUSTOMER\M4011-511-B-PARTS-2
Found in H:\Sheetmetal\DMR\CUSTOMER\M4011-511-B-PARTS-3
Found [4011-511-18812]
Found in H:\Sheetmetal\DMR\CUSTOMER\M4011-511-B-PARTS-2
Found in H:\Sheetmetal\DMR\CUSTOMER\M4011-511-B-PARTS-3

I came up with the following code::

# Define the folder and files where we look for the files
$FilesFolder = 'H:\Sheetmetal\DMR\CUSTOMER'
$DestDir = 'K:\dxf'
$FileLocation = '.\files.txt'
$FolderDate = (Get-Date).tostring("dd-MM-yyyy")
$Depth = 5
$Project = Read-Host -Prompt 'Input the project name'

# Test if dir exist or else create one
if (!(Test-Path "$DestDir\$Project-($FolderDate)"))
{
   New-Item -ItemType Directory -Path "$DestDir" -Name "$Project-($FolderDate)" > $null
   Write-Host "Created new file and text content added"
}
else
{
  Write-Host "Directory already exists."
}

# Test if file exist or else create one
if (!(Test-Path "$FileLocation"))
{
   New-Item -name $FileLocation -type "file" -value "Input article to search for"
   Write-Host "Created new file and text content added"
}
else
{
  Write-Host "File list already exists."
}

# Start Notepad for generating list
$Proc = Start-Process Notepad.exe $FileLocation -NoNewWindow -PassThru
$Proc.WaitForExit()

# Define the list of files to check for
$FileNameList = Get-Content -Path $FileLocation 

# Get all the files in the FilesFolder
$ContainingFiles = Get-ChildItem $FilesFolder -Depth 1

Write-Host "Folder: $FilesFolder."
Write-Host "Searching for files"

# Go through each file in the FilesFodler
foreach ($FileName in $FileNameList) {

    # Define basename
    $BaseName = $FileName.Split("\\s+")[0]

    # Define name
    $Name = $FileName.Split("\\s+")[1]

    # If the folder contains a matching basename, write output GREEN
    If ($ContainingFiles.Name -like "*$BaseName*"){

        Write-Host "Found [$BaseName]" -ForegroundColor Green

        $FullName = Get-ChildItem "$FilesFolder" -Depth 1 | where {$_.name -match "$BaseName"} | %{$_.FullName}

        # Create another loop and copy files
        foreach ($FoundItem in $FullName) {

            # Split the directory structure to $depth
            $result = $FoundItem
            While (($result -split '\\' | ? { $_ }).Count -gt $Depth) {
                $result = Split-Path $result -Parent
            }

            # Split string for article name
            $founddir = $result.split('\')[-1]
        
            Write-Host "Found in $result"

            # Check if diretory already exists before copying
            if (!(Test-Path "$DestDir\$Project-($FolderDate)\$founddir"))
            {
                Copy-Item -Path "$result" -Destination "$DestDir\$Project-($FolderDate)\$founddir" -recurse -Force
            }
            
        }

    # If no matching basename was found, write output RED

    }Else{

        Write-Host "Failed to find dir [$BaseName]" -ForegroundColor Red

        }

    }

pause

I think (since I've no know-how of coding) it could be done way simpler or neater?

\$\endgroup\$

0

Your Answer

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

Browse other questions tagged or ask your own question.