Home > OS >  Compiling CSV's dependent on Date
Compiling CSV's dependent on Date

Time:07-15

this is my first post so sorry about any mistakes.

I'm currently trying to use Powershell to combine folders of csv files based on date. I'm trying to go a week back, compile them, and export to another folder. I've only been using Powershell a few days and have an ok knowledge on coding in general.

I'm trying to use this fuction:

(Get-ChildItem C:\Folder | Group-Object -AsHashTable {$_.CreationTime -ge (Get-Date).AddDays(-2)})

That outputs a name(true or false) and a value(folder title). What I want to do is use another function to then export those CSV files in several folders all to one folder.

Is this possible? Am I going in the right direction? I have very little experience with this.

Thanks. Luke

CodePudding user response:

You're looking to filter by instead of group by, hence you would be using Where-Object instead of Group-Object. To copy files you can use the built-in cmdlet Copy-Item.

Do note, the path\to\destinationfolder in the example below must be an existing folder, you should create it before running the code.

# NOTE: If you want to filter only for files with .CSV Extension,
#       `-Filter *.csv` should be included
Get-ChildItem C:\Folder -Recurse |
    Where-Object { $_.CreationTime -ge (Get-Date).AddDays(-2) } |
        Copy-Item -Destination path\to\destinationfolder
  • Related