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.
5 Spice ups

There is no out of the box, one size fits all solution for that.

What have you tried? where are you stuck?

If you post code, please use the ‘Insert Code’ button. Please and thank you!

It really is a big fat steaming “it depends”.

If the file names are a consistent format then it’s not too hard. -split ‘_’ the file’s BaseName and then work on the date part and you are well on the way.

@neally I am trying to find the most efficient script. I have tried just making the folders one by one for the files,( new-item -itemtype directory then move-item)but of course that is very time consuming, because there is more than 100 files to organize into folders. So I just need to figure out cmdlets that move/organize mass amount files into folders while creating the appropriate folder for that file.

@M Boyle yes the files are the same format. All of them begin with 59_2018×××× (corresponding to the month and year) Ex. I made the first few folders for the first file. I made folder 2018(which all the files would go to because all the files follow the same year) then made folder 01(corresponding to the month, so 31 files would go here) then finally making a folder for that day (of course that particular file would go into that folder) etc. Then repeat for roughly the next 120 files. (4 months)
Again I appreciate the help.

This attachments shows where I am stuck at now… i maybe tried for each, but i cant get the parameters right

I have no idea what that code of yours is trying to do.

If you’re only dealing with a few hundred files the script doe snot have to be the most efficient, if just has to work.

Get-ChildItem to get all the files. 

foreach file:
    split up the file's basename to get the date part. Parse it out into the constituent parts and construct the dir name where the file will be moved to.

    Test-Path to see if thedir exists, if not New-Item to create it.

    Then move the file into the target folder. 

and that’s the basic structure.

@M Boyle Aha! I think we are on to something! Let me work on that, then post what I get!

@M Boyle which commannds can I use to split up file basename and parse it out?

To start:

  1. Get-ChildItem -Path . should do that for you. But why not define a script parameter (StartPath) that defaults to ‘.’?

  2. what do you mean 'identifies the correct date? the objects returned by step 1 DO have the time/date of last access and last write to that file.

  3. You can use Test-Path to see if the path exists.

  4. Use New-Item to create the new folders as needed

5., Is not needed if 4 done correctly

6 Move-Item moves the file you want.

As to the most efficient, just write the script that does the 6 actions you want.

PLEASE NOTE: this is not a ‘we write your scripts for you for free’ service. Sorry if that is not what you were expecting! ;-),

The -split operator.

1 Spice up