in

WindowsWindows Loving ItLoving It

Files created on the same day be in the same folder.

Move files created on same day to same folder using PowerShell.

powershell files

If you are someone like us, you will probably have lots of files being created every day.  Managing these files can become very difficult if you let these files pile up for a long time.  But in this post, we will provide you with a simple solution to this problem.

It will be easier if we can just move these files into meaning full folders.  What if we move the files created on a particular day in one folder and so on.  We will see just how to do that.

Jump to:

We have a few sections up ahead.  In the first section, we will see how to move the files created on the same day into the same folder.  This is done using a script given here.  You can easily copy and paste the script, get things done, and move on with life.  The subsequent sections break down the script for anyone who is curious about what is going on.

PowerShell move Files created on the same day in the same folder:

Consider a folder as shown in the image.

folder full of files
folder full of files

Clearly there are too many files to go through and sort them.  By following the given instructions, we can fix this problem.

  • Click on the address bar as shown in the image.
click on the address bar
click on the address bar
  • Type the following command and press enter.
powershell
PowerShell command
PowerShell command
  • It will launch PowerShell on the same path.
PowerShell in the same path
PowerShell in the same path
  • Copy and paste the following script in the path and press enter twice.

$ln = “”
dir | sort creationtime | %{
$ct = $_.creationtime
$fn = “{0}_{1}_{2}” -f $ct.month, $ct.day, $ct.year
If($ln -ne $fn)
{
md -force $fn
$ln = $fn
}
mv $_.fullname -destination $ln
$fn
}

enter the script
enter the script
  • After the script is completed executing, we can see an output as shown in the image.
output
output
  • We can go to the location and see that the files are neatly moved into the respective folder that is created on a date basis.
created folders
created folders

There we go, we have managed to move files created on the same day to the same folder.

Break down the script:

We need to get the list of all the files in the directory and we can do that by dir cmdlet.

dir

By executing dir in PowerShell we get the following output.

dir command
dir command

We need to get to all the files that are created on the same day.  We can do that by sorting the output by date.  We will pipe the output of the dir command to sort followed by the creationdate parameter name.

sort by date
sort by date

We need to loop through every element and see its date value and decide which folder it must be moved to.  So we will pipe the output a foreach-object (%)loop.  Please note that we use the % alias for the foreach-object loop.

dir | sort creationtime | %{

$_
}

Executing the above script will produce the same result but we need to note the $_ that we use to identify an item that comes as the output of dir | sort creationtime cmdlet.

dir creation time
dir creation time

We have to obtain the date when the file was created and use it as the folder name.  Firstly, we will take the creation time property value in a variable as shown below.

$ct = $_.creationtime

We can see the value stored in $ct using the below script.

dir | sort creationtime | %{
$ct = $_.creationtime
$ct
}

creation time
creation time

Secondly, we will create a folder name using string-format feature available in PowerShell as given below.  Note that executing the below script will also show the created folder name in the console.

dir | sort creationtime | %{
$ct = $_.creationtime
$fn = “{0}_{1}_{2}” -f $ct.month, $ct.day, $ct.year
$fn
}

folder names
folder names

Now we just need to create a folder in the name of each date.  We need to ensure that the folder is created and only if the folder does not already exist.

md -force $fn

We finally have to move the file into the newly created folder.

mv $_.fullname -destination $ln

Note that, mv is the alias for move-item cmdlet.   The first parameter is the path to the file.

mv $_.fullname -destination $ln

The second parameter is the path of the new folder.  In our case, it contains only the folder name as it is created inside the same directory.

mv $_.fullname -destination $ln

Now that we have broken down how each component of the script works, we need to tackle the last problem.

We have to create a folder when the next file on the list is created on the next day and move it to the folder created for that particular day.

  • We achieve that by storing the current day in the $ln
  • And check if the date created for the current file is the same.
  • If it is not the same, we understand that the file is created on the next day and we create a new folder for the new day.
  • We save the new day in $ln and so on.
  • And thus we have $ln declared outside the loop.
$ln = “”
  • And we set the value of $ln if it is not the same as the current day.

If($ln -ne $fn)
{
md -force $fn
$ln = $fn
}

We have completely achieved what we wanted.

Verdict:

We have learned to move the files created on the same day to the same folders.  This is very useful in an organization where we plenty of files generated on a day.

If you want more such awesome posts, please stay tuned to TecKangaroo.

Leave a Reply
  1. Hi Team,

    I want a script that combines today dated file and next day created file to be copied into a folder.

    I have backup running that runs for 2days . so i need to move all files that were created today and tomorrow files to a single folder

    • Files created tomorrow will not be there in the hard disk. You will have the files created today and the days that are gone. Can you please clarify your requirements a little more so that we can try and help you?

Leave a Reply

Your email address will not be published. Required fields are marked *