0

enter image description hereI have a folder which has a bunch of files named: WBF123456, WBF135464, etc. These files need to be moved to the corresponding folder. At the moment I am using the commandline to manually enter the numbers of each file so they get moved, using this code:

$files = $args[0]

mv O:\SCAN\SecSur\*$files.pdf O:\SPG\G*\*\*$files

How can I automate this process? It needs to identify the number in the filename, then move it to the folder containing the same number. Any help would be great. Thanks.

I need to get the files on the left, inside the corresponding folders on the right.

c00per
  • 3
  • 4

2 Answers2

0

Maybe the below solution will help you. You should change $origin_path and $destination_path

    $origin_path= "C:\Users\geralexgr\Desktop\kati\files"
    $destination_path = "C:\Users\geralexgr\Desktop\kati\folders"
    Get-ChildItem $origin_path -Recurse -Include *.txt | ForEach-Object {
        $folder = [regex]::Matches($_.Name, "\d+(?!.*\d+)").value
        Move-Item $_.FullName $destination_path\$folder
    }

The example will place files under the folders that match the numeric regex.

enter image description here

After powershell execution file WBF12 gets inside 12 folder

enter image description here

GeralexGR
  • 2,973
  • 6
  • 24
  • 33
0

Apparently the files to move are .pdf files, so what you can do is get a list of those files in the source folder and then loop over that list to create (if needed) the destination subfolder and move the file there.

Try:

$destinationRoot = 'O:\SPG\G\SomeWhere'  # enter the root folder destination path here

$filesToMove = Get-ChildItem -Path 'O:\SCAN\SecSur' -Filter '*.pdf' -File
foreach ($file in $filesToMove) {
    $numName = $file.BaseName -replace '\D+'  # leaving only the numbers
    # create the target path for the file
    $targetFolder = Join-Path -Path $destinationRoot -ChildPath $numName
    # create that subfolder if it does not already exist
    $null = New-Item -Path $targetFolder -ItemType Directory -Force
    # now, move the file
    $file | Move-Item -Destination $targetFolder
}

Seeing your screenshots, this might be a better approach for you.

$destinationRoot   = 'O:\SPG\G\SomeWhere'  # enter the root folder destination path here
# get a list of target folders for the files to be moved to and create a lookupHashtable from their names
$targets = @{}
Get-ChildItem -Path $destinationRoot -Directory | Where-Object {$_.Name -match '(\d+)'} | ForEach-Object {
    $targets[$matches[1]] = $_.FullName  # key is the number, value is the directory fullname
}

# get a list of files to move
$filesToMove = Get-ChildItem -Path 'O:\SCAN\SecSur' -Filter '*.pdf' -File | Where-Object {$_.Name -match '\d+'}
foreach ($file in $filesToMove) {
    $numName = $file.BaseName -replace '\D+'  # leaving only the numbers
    # see if we have a target folder with that same number in its name
    if ($targets.ContainsKey($numName)) {
        $targetFolder = $targets[$numName]
        Write-Host "Moving file $($file.Name) to $targetFolder"
        $file | Move-Item -Destination $targetFolder
    }
    else {
        Write-Warning "Could not find a destination folder for file $($file.Name).."
    }
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • The target folders already exist and are named "name_name_123456". Is it possible to put them in those existing folders? – c00per Sep 20 '22 at 14:07
  • @c00per Only is somehow the part `name_name` can be deduced from...? – Theo Sep 20 '22 at 14:23
  • I added a link at the top with a screenshot of what I mean. I need the files on the left to go into the corresponding folders on the right. Thanks for your time, I hfind it hard to explain what I mean. – c00per Sep 20 '22 at 14:35