Link to home
Start Free TrialLog in
Avatar of WeTi
WeTi

asked on

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
Avatar of J0rtIT
J0rtIT
Flag of Venezuela, Bolivarian Republic of image

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

Avatar of WeTi
WeTi

ASKER

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?
Avatar of WeTi

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WeTi

ASKER

David answer was the one im after. $files = Get-ChildItem -Path ($root + $folder) This solved my problem thanks both for the help
Avatar of WeTi

ASKER

David answer was the one im after. $files = Get-ChildItem -Path ($root + $folder) This solved my problem thanks both for the help
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)