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.
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.
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.
- Type the following command and press enter.
- It will launch PowerShell on the same path.
- Copy and paste the following script in the path and press enter twice.
- After the script is completed executing, we can see an output as shown in the image.
- 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.
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.
By executing dir in PowerShell we get the following output.
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.
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.
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.
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.
We can see the value stored in $ct using the below script.
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.
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.
We finally have to move the file into the newly created folder.
Note that, mv is the alias for move-item cmdlet. The first parameter is the path to the file.
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.
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.
- And we set the value of $ln if it is not the same as the current day.
We have completely achieved what we wanted.
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.