2

i need help with a task

I need to copy an exported file from sub-directories into a new folder location and have them renamed. The issue is that all the exported files have the same name and it goes up to 500

--Old Folder
 ---Export1
    ---Scan.TIF
 ---Export2
    ---Scan.TIF
 ---Export3
    ---Scan.TIF

I have tried the following link which worked fine but the data would be all over the place. For example, the copied and renamed file in the new folder location called eg SCAN13.TIF would actualy be the document from sub-directory Export24

https://sumtips.com/snippets/powershell/copy-files-rename-duplicates-subfolder/

I need help with a script that could copy the file from all the sub-directories to a new location, renamed and have the data matched

Is that doable via scripting or would there be a tool for that?

Thanks for your time

Tommy0824
  • 25
  • 5
  • Just to confirm, the folder structure is `Old Folder\Export#\Scan.TIF` where there is a new `Export#` folder for each and every file? – Owain Esau Dec 07 '18 at 00:35
  • It's unclear to me what mean with `renamed and have the data matched` Do you want to rename the file to include the source folder name? –  Dec 07 '18 at 01:33
  • @LotPings Thanks for editing the folder structure. i was trying to but i just didn't know how. What i mean renamed and have the data matched is for example, if via a script the Scan.TIF from Export3 is copied to a new location, and renamed to Scan3.Tif, when i open the file, it needs to be the same .TIF file from the Export 3 folder. The link i provided did not achieve that – Tommy0824 Dec 07 '18 at 02:50
  • @OwainEsau Hi, sorry for not being to illustrate the Folder structure clearer. LotPings seems to have resolved that. And yes there is new Export# folder for each and every file. There is essential 1 file per Export folder and they are all named Scan.TIF – Tommy0824 Dec 07 '18 at 02:52
  • If the Export folder numbers are continuous, use a counter `for ($i=1;$i -le $max;$i++){..}` –  Dec 07 '18 at 03:10
  • @LotPings where do i place that in Owen's script below? sorry i am new to coding – Tommy0824 Dec 07 '18 at 03:12

4 Answers4

1

This script uses a Regular Expression to get the trailing number of the scan.tif files directory name.
The -match operator stores the RegEx capture group () in the $Matches collection which is used to build the destination name with the -f(ormat) operator.

## Q:\Test\2018\12\07\SO_53661264.ps1
$Src = "A:\Old Folder"
$Dst = "A:\New Folder"

Set-Location $Src
Get-ChildItem Scan.tif -Recurse -File |
  Where {$_.Directory.Name -match 'Export(\d+)$'} |
    Copy-Item -Destination {Join-Path $Dst ("Scan{0}.TIF" -f $Matches[1])} -WhatIf

If the output looks OK remove/comment out the trailing -WhatIf parameter.

Sample tree /f of my ramdisk after running the script:

> tree /F \
Auflistung der Ordnerpfade für Volume RamDisk
├───New Folder
│       Scan1.TIF
│       Scan2.TIF
│       Scan3.TIF
│
└───Old Folder
    ├───Export1
    │       Scan.TIF
    │
    ├───Export2
    │       Scan.TIF
    │
    └───Export3
            Scan.TIF
0

If i understood you correctly the following should work:

$source = "C:\test"
$Destination = "C:\Users\user\Documents\Tests"

$i = 1

foreach ($file in (Get-ChildItem -Recurse $source -Filter *.TIF)) {
    Copy-Item $file.FullName -Destination "$destination\Scan$i.TIF"
    $i++
}

You will need to change the $source and $destination variables. Source being the root directory of the folder you are copying from.

The end result will be any .TIF file in the source directory down being copied to the destination folder with the filenames Scan[$i].TIF where the number iterates by 1 on each file copied.

Owain Esau
  • 1,876
  • 2
  • 21
  • 34
  • Thanks for the prompt response. I just tested you script, even though it managed to copy and rename the files from the old to new location, the data is wrong. For example, in the original file structure, the Scan.TIF file in folder Export 25 refers to a Mr.Anderson. With your script, in theory, if i go and open Scan25.TIF it should be the same file for Mr.Anderson but instead its a file for some other guy. This happens to alot of them – Tommy0824 Dec 07 '18 at 03:00
0

You could try this first

Puts the name of the folder at the beginning of the file name

Get-ChildItem <Directory of File> -Filter *.ext -Recurse | Rename-Item -NewName {$_.Directory.Name+'_'+$_.Name}

Its not the whole answer you are looking for but hopefully a piece of what you need.

Community
  • 1
  • 1
0

I share this script here hopping it will be useful to other people. I would personally have been happy to find it here earlier.

## Extract and rename.ps1
$src = split-path -parent $MyInvocation.MyCommand.Definition;
$root = split-path -parent $src;
$folderName = (get-item $src ).Name;
$suffix = '-extract';
$newFolderPath = "$($root)\$($folderName)$($suffix)\";
Write-Host "Extracting files from $($folderName) to $($folderName)$($suffix)";
New-Item -ItemType directory -Force -Path $newFolderPath
Set-Location $src
Get-ChildItem -Recurse -File -Exclude *.ps1 |
    ForEach-Object {
        $currentFolderName = Split-Path -Path $_.Directory.FullName -Leaf;
        $currentFileName = Split-Path -Path $_.FullName -Leaf;
        $newFileName = "$($currentFolderName)_$($currentFileName)";
        [System.IO.Path]::GetInvalidFileNameChars() |
            ForEach-Object {
                $newFileName = $newFileName.replace($_,'.');
            }
        $destination = Join-Path $newFolderPath $newFileName;
        Copy-Item -literalpath $_ -Destination $destination -Force;
        Write-Host $_ => $destination;
    }

Result:

├───Old Folder-extract         <========== The resulting folder
│       Export1_Scan.TIF
│       Export2_Scan.TIF
|       Sub Export 2.2_Scan.TIFF
│       Export3.TIF
│
└───Old Folder                 <========== The chaotic folder
    │   Extract and rename.ps1 <========== The script must be placed here
    │
    ├───Export1
    │       Scan.TIF
    │
    ├───Export2
    │   │   Scan.TIFF
    │   └───Sub Export 2.2
    │          Scan.TIF
    │       
    └───Export3
            Scan.TIF

I'm a newbie in powershell so I don't think my script is optimized but it works.

bZx
  • 111
  • 1
  • 4