Powershell moving files in order

Powershell scripting.

Dear expert

c:\test\01\test.txt
c:\test\02\test.txt
c:\test\03\test.txt
c:\test\04\test.txt

I need a script that take the first folder 01 file to another folder and then delete the 01 folder. Then it will stop here once removed 01 folder, next day i want the same progress with 02 folder and 03 etc.

Please help. Thanks
LVL 1
WeTiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
It should be something like this.
$root ="c:\test"

#first 2 folders
$folders = Get-ChildItem -Directory -Path $root | select -First 2

$tmp = [char](65 + [Convert]::ToInt32($folders[0].BaseName) - 1) 
$FstFile = Get-ChildItem -File $($folders[0].FullName)

foreach($file in $FstFile){
    $MoveFilename = "$($folders[1].FullName)\$($file.BaseName)$tmp.txt"
    Move-Item $($file.FullName) $MoveFilename
}

$isEmpty = (Get-ChildItem -Path $folders[0].FullName).Count -eq 0
if($isEmpty){
    Remove-item -Path $folders[0].FullName
} 

Open in new window

WeTiAuthor Commented:
Thanks, question tho:
why are you using array for this simple task?
$folders = Get-ChildItem -Directory -Path $root | select -First 1
This will only select the first folder right? Why select First 2? Then you use a Int32 convert why is this operation needed?
WeTiAuthor Commented:
Ahh, now i understand why you did it. Ok I will rewrite my question:

c:\test\01\test.txt
c:\test\02\test.txt
c:\test\03\test.txt
c:\test\04\test.txt

I need a script that take the first folder 01 file to another folder, like c:\temp\  and then delete the 01 folder. Then it will stop here once removed 01 folder, next day i want the same progress with 02 folder and 03 etc.

Not move the 01 folder files to 02.
Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

David Johnson, CD, MVPOwnerCommented:
$root = 'f:\test\'
$destination = 'F:\destination'
$folder = get-childitem -Path $root -Directory | Select-Object -first 1
$files = Get-ChildItem -Path ($root + $folder)
foreach($file in $files){
  Move-Item -Path $file.fullname -Destination $destination -Force
  }
Remove-Item -Path ($root+$folder)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
WeTiAuthor Commented:
David answer was the one im after. $files = Get-ChildItem -Path ($root + $folder) This solved my problem thanks both for the help
WeTiAuthor Commented:
David answer was the one im after. $files = Get-ChildItem -Path ($root + $folder) This solved my problem thanks both for the help
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
Yeah the problem was that you didn't specify what to do with the text.txt file when you copied ( the action was to overwrite, mine preserves procedence).

It's a simple task, but When I did it, in the 1st loop, it works selecting the 1st one.

But to move the file you need the next folder. Then why just select one if I can get the 2 folders and then use one or the other every time?.
Since the txt file is named the same (test.txt) if you take the one from the 01 folder and move it to the 02 folder it will overwrite the content on the second folder. then I just created a way to change the (the 01 of the folder into an A, 02 into a B and so on).
So the renaming logic is testA (for the 1st folder 01), and here's where the things get intense.

*-1
When you move the 1st folder you get in the 02
Test.txt (the one that it was there)
TestA.txt (the one from the 1st folder)
remove 01 folder

*-2
In the second loop you get:
2 folders (02 and 03) and in 02 are 2 files.
Getting the files in 02 contains (test and testA) when you move it to 03 it will change the 02 to (B) and it will append that B into the end of the names of both files:
so at the end in 03 you will get:
test.txt
testB.txt (the one from folder 02)
TestAB,txt (the one from folder 01)
remove 02 folder

*-3
in the 3rd loop:
2 folders = 03 and 04
(in 03 are 3 files), so in the 04 will be:
then change the 03 to a C and append it to the end of the files
test.txt
testC.txt (this folder)
testBC.txt (second folder)
testABC txt (1st folder)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.