-1

I have a complex folder structure to move

I have a folder containing 2000 files that must be moved in new file structure, I do some simple task with powershell but not so complex so I'm completely lost... Didn't found any solution on other questions...

all folders containing 23 files (some .dds some .xml and so ones) folder must be moved completely

here is the actual situation:

files\128891\
files\128986\
files\129362\
files...\

that must be moved to:

files\128891\aaa\bbb\ccc\ddd\eee\real\128891\
files\128986\aaa\bbb\ccc\ddd\eee\real\128986\
files\129362\aaa\bbb\ccc\ddd\eee\real\129362\
files...\aaa\bbb\ccc\ddd\eee\real...\

have around 2000 folders in files that must be moved with their files in

Thanks a lot for helping

Kantonin
  • 3
  • 3
  • 1
    could you please clarify and show a more clear example of what folder and what files it contains. As well as where you want to remake them and by what logic. – Vad Mar 10 '20 at 09:52
  • all folder conataining 23 files (some .dds some .xml and so ones) folder must be moved completely – Kantonin Mar 10 '20 at 10:01
  • What logic in that named?? **128891** **128986** **129362** but all this in folder **128891** – Vad Mar 10 '20 at 10:07
  • no logic want to move all folder that are in "files" directory.... would be "get name"\aaa\bbb\ccc\ddd\eee\real\"this name" – Kantonin Mar 10 '20 at 10:13
  • sorry made a mistake @Vlad not all in this folder but keeping the main folder name – Kantonin Mar 10 '20 at 10:16
  • you want move or copy? . I understood correctly that you want move all files from folder named "files\128891" to folder files "\128891\aaa\bbb\ccc\ddd\eee\real\128891"?? – Vad Mar 10 '20 at 10:25
  • I want to Move completely – Kantonin Mar 10 '20 at 10:26
  • final path must be "files\129362\aaa\bbb\ccc\ddd\eee\real\129362\" or "files\aaa\bbb\ccc\ddd\eee\real\129362\" ? – Vad Mar 10 '20 at 10:29
  • files\129362\aaa\bbb\ccc\ddd\eee\real\129362\" must have id\path\id – Kantonin Mar 10 '20 at 10:30
  • 1
    So what did you tred? I don't think this is difficult. You have to make the new folder in that path Having the name of the first folder in a variable that you will use later for creating the other folder. Then next stem move items from one path to the other. you can get-child items to have the names on the root dir and then run it in a loop. Show us what you tried first and we will help you finish your task. – StefTheo Mar 10 '20 at 10:33
  • Yes @StefTheo I understand the general principle... but have difficulties to make the rules to create folder based on a variable looped on data... get-content list.txt |%{ $Destination = $_ -replace 'h:\\files\\$var_id', 'h:\files\$var_id\aaa\bbb\ccc\ddd\eee\real\$var_id\' "Moving $_ to $Destination" move-item -path "$_" -destination "$Destination" } Don't know how I can fill the var_id – Kantonin Mar 10 '20 at 10:49
  • He is not Dracula, call him Vad not Vlad ;) "made a mistake @Vlad not all in this folder" – digitalway Mar 10 '20 at 14:15

1 Answers1

1

You can modify my code (not better way but works)

 $foldernames=Get-ChildItem -Recurse "D:\testdir" | ?{ $_.PSIsContainer } #get all folder in start folder, for you must be "...files\"
    foreach($foldername in $foldernames){
    $files=Get-ChildItem -Path $foldername.FullName|Where-Object {! $_.PSIsContainer} #get all files in current folder, no recurse,no subfolders.
    $files|Move-Item -Destination (New-Item -ItemType Directory -Path (Join-Path -path $foldername.FullName  -ChildPath ("aaa\bbb\ccc\ddd\eee\real\"+$foldername.Name)) -Force)
    #move all files and create directories
    }

Additional:

If you have in folder , subfolders you must fix code like that

$foldernames=Get-ChildItem "D:\testdir"  | ?{ $_.PSIsContainer } 
foreach($foldername in $foldernames){
$files=Get-ChildItem -Path $foldername.FullName
$files|Move-Item -Destination (New-Item -ItemType Directory -Path (Join-Path -path $foldername.FullName  -ChildPath ("aaa\bbb\ccc\ddd\eee\real\"+$foldername.Name)) -Force)
}

It's move all from folder 128891(an exapmle) including subfolders

If you have in folder subfolders but you want move only files from it you must filetring like this:

$files=Get-ChildItem -Path $foldername.FullName -recurse|Where-Object {! $_.PSIsContainer}

But remember in this case errors may occur due to duplicate file names

Vad
  • 693
  • 3
  • 13
  • Yes perfect sounds to work well on tested data. just forget one thing sometimes I got a subfolder in the folder to be i only can add -recurse before -force ? – Kantonin Mar 10 '20 at 11:07
  • You should pay @vad, he has done all the work for you :) – digitalway Mar 10 '20 at 14:07