Author Posts

August 27, 2018 at 8:53 pm

Basically moving/sorting multiple files and creating folders corresponding to that file. for example 20180102.txt file has to be moved to corresponding folder 2018\01\02
We need a Powershell script that
1. reads thru the current directory (where the script resides)
 2. identifies the correct date that the file represents
3. Checks the existing folder structure, if it exists CURRENT FOLDER\year\xx monthname\xx(day).
     Example for file 59_20180102.txt it should go to folder  \2018\01 January\02\
4. Creates the folder structure, as needed, for the files CURRENT FOLDER\year\xx monthname\xx(day)
5. it should only create folders as needed, meaning for existing files. If I have only one file, it should only create that day folder
6. moves each txt file to the appropriate folder
What is the simplest/ most efficient cmdlet/script to do this. Greatly appreciated.

 

August 27, 2018 at 9:45 pm

I doupt that there will be a ready to use script for exactly that requirement you have. You will probably have to create one by yourself. You could start with Get-ChildItem, Where-Object, Foreach-Object, New-Item, Move-Item and some conditions/comparisons.

August 28, 2018 at 10:43 am

@olaf I do see those cmdlets. But can those move multiple files at once? To make it more efficient? There are about rough 120 files, and I have to move each of them into a separate corresponding file. So that could take some time.

August 28, 2018 at 11:09 am

... But can those move multiple files at once? ...

If you mean parallel – with a normal script – NO, not by default. They are usually copied sequencially. But it would be a script. You don't have to copy them one by one. That does the script for you.

... To make it more efficient?

It doesn't have to be more efficient just because it's parallel. You could use jobs if you need to do it parallel anyway.

... So that could take some time. ...

So what? It would be a script. It runs without interaction. You don't have to watch it running!! 😉

Why don't you just start trying it? It will not hurt you.

August 28, 2018 at 11:19 am

@olaf, ok I will try something, then post what I get. Thank you for your assistance.

August 28, 2018 at 12:55 pm

PS C:\test\goto> new-item -ItemType directory -name[03] -path C:\test\goto\2018  | ForEach-Object -InformationVariable[59_201803xx.txt]

cmdlet ForEach-Object at command pipeline position 2

Supply values for the following parameters:

Process[0]: get-childitem -path C:\test\goto

Process[1]: -membername 59_20180301.txt

Process[2]:

PS C:\test\goto>

 

And these are the examples of the files

-a—-         5/3/2018   5:15 PM              0 59_20180103.txt

-a—-         5/3/2018   5:15 PM              0 59_20180104.txt

-a—-         5/3/2018   5:15 PM              0 59_20180105.txt

-a—-         5/3/2018   5:15 PM              0 59_20180106.txt

-a—-         5/3/2018   5:15 PM              0 59_20180107.txt

-a—-         5/3/2018   5:15 PM              0 59_20180108.txt

-a—-         5/3/2018   5:15 PM              0 59_20180109.txt

-a—-         5/3/2018   5:15 PM              0 59_20180110.txt

-a—-         5/3/2018   5:15 PM              0 59_20180111.txt

-a—-         5/3/2018   5:15 PM              0 59_20180112.txt

-a—-         5/3/2018   5:15 PM              0 59_20180113.txt

-a—-         5/3/2018   5:15 PM              0 59_20180114.txt

-a—-         5/3/2018   5:15 PM              0 59_20180115.txt

-a—-         5/3/2018   5:15 PM              0 59_20180116.txt

-a—-         5/3/2018   5:15 PM              0 59_20180117.txt

-a—-         5/3/2018   5:15 PM              0 59_20180118.txt

-a—-         5/3/2018   5:15 PM              0 59_20180119.txt

-a—-         5/3/2018   5:15 PM              0 59_20180120.txt

-a—-         5/3/2018   5:15 PM              0 59_20180121.txt

-a—-         5/3/2018   5:15 PM              0 59_20180122.txt

-a—-         5/3/2018   5:15 PM              0 59_20180123.txt

-a—-         5/3/2018   5:15 PM              0 59_20180124.txt

-a—-         5/3/2018   5:15 PM              0 59_20180125.txt

-a—-         5/3/2018   5:15 PM              0 59_20180126.txt

-a—-         5/3/2018   5:15 PM              0 59_20180127.txt

-a—-         5/3/2018   5:15 PM              0 59_20180128.txt

-a—-         5/3/2018   5:15 PM              0 59_20180129.txt

-a—-         5/3/2018   5:15 PM              0 59_20180130.txt

-a—-         5/3/2018   5:15 PM              0 59_20180131.txt

-a—-         5/3/2018   5:15 PM              0 59_20180201.txt

-a—-         5/3/2018   5:15 PM              0 59_20180202.txt

-a—-         5/3/2018   5:15 PM              0 59_20180203.txt