1

I would like to create a PowerShell script that can import a CSV file (details.csv) with two headers (FileName and FileCreationTime). Ideally, the script would look for details.csv in the current location the script is saved.

It would create folders in the script's current location with the same name as FileName, and the creation date of said folder would then be changed to match FileCreationTime.

Example chunk of my CSV [made in A & B columns of Excel then saved as CSV (comma delimited)(*.csv)]:

FileName    FileCreationTime
Alpha       5/17/2017
Bravo       12/23/2013
Charlie     11/8/2015

I have been searching for a solution, but nothing I do seems to be quite right. I currently have this:

Import-Csv -Path 'K:\Users\eschlitz\Thesis\details.csv' -Delimiter "," |
    ForEach-Object { 
        $path = 'K:\Users\eschlitz\Thesis'
        # Again, didn't want a definite path here, but I was trying different
        # tweaks to see if I could get at least one instance to work correctly.
        New-Item -Path $path -Name $$_.Filename -Type Directory
        (Get-Item $_.Filename).CreationTime = (Get-Date $_.FileCreationTime) 
    }

My current error message:

Get-Item : Cannot find path 'K:\Users\eschlitz\Thesis\Alpha' because it does not exist.

I do not care about whether or not the hh:mm:ss part of the creation time is edited for the new folders, but it would be a bonus if I could standardize them all to 12:00:00 AM.

~~~~~~~~~~~~~~~~~~~~~~~Question Duplication Edit~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suggested edit to show how my question is different from PowerShell: Change the timestamp (Date created) of a folder or file

Everything that I was able to find related to this did either only A)create folders from a CSV, or was B)script to edit the creation date of a single folder / or batch edit the creation date of multiple folders but only with a single new creation time. I wanted the script to hopefully fail if it would be unable to correctly find the new creation time unique to each new folder, thereby eliminating the need for me to manually delete wrong folders or edit the creation time manually.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Edit~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Just wanted to post the complete, working versions in case anyone needs them in the future.

#Adds folders to specified directory and modifies their creation date
Import-Csv -Path 'K:\Users\eschlitz\Thesis\details.csv' -Delimiter "," |
    ForEach-Object {
        $path = ' K:\Users\eschlitz\Thesis'
        $dir = New-Item -Path $path -Name $_.Filename -Type Directory
        $dir.CreationTime = [DateTime]::ParseExact($_.FileCreationTime, 
        'M\/d\/yyyy', [Globalization.CultureInfo]::InvariantCulture)
} 

And a slightly different version depending on needs:

#Adds folders and then modifies their creation date where script+csv 
#currently are 
Set-Location -Path "$PSScriptRoot"
Import-Csv -Path ".\details.csv" -Delimiter ',' |
    ForEach-Object {
        New-Item -Path "$PSScriptRoot" -Name $_.FileName -Type Directory
        (Get-Item $_.Filename).CreationTime = 
        ([DateTime]::ParseExact($_.FileCreationTime, 'M\/d\/yyyy', 
        [Globalization.CultureInfo]::InvariantCulture))
} 
  • see https://stackoverflow.com/questions/38959250/powershell-change-the-timestamp-date-created-of-a-folder-or-file – No Refunds No Returns Oct 05 '18 at 04:19
  • 1
    Possible duplicate of [PowerShell: Change the timestamp (Date created) of a folder or file](https://stackoverflow.com/questions/38959250/powershell-change-the-timestamp-date-created-of-a-folder-or-file) – No Refunds No Returns Oct 05 '18 at 04:19

1 Answers1

0

The folder is not created b/c you have a typo in the New-Item statement ($$_.Filename → $_.Filename), and even if it were created Get-Item most likely wouldn't be able to find it, because it's looking in the current working directory, whereas you create the folder in $path. You can avoid the latter issue by capturing the DirectoryInfo object that New-Item returns in a variable:

$dir = New-Item -Path $path -Name $_.Filename -Type Directory
$dir.CreationTime = (Get-Date $_.FileCreationTime)

You may also need to actually parse the date string into a DateTime value (depending on your locale):

[DateTime]::ParseExact($_.FileCreationTime, 'M\/d\/yyyy', [Globalization.CultureInfo]::InvariantCulture)

If you defined the date in ISO format (yyyy-MM-dd) Get-Date should be able to digest it regardless of the system's locale.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Hey, this newbie really appreciates the advice @Ansgar Wiechers. Your script worked like a charm. I think I found something that said $$_ was used for the second item coming from a set (which should have been my FileCreationTime -> but where I had it placed in my script doesn't even make sense for that). Took me a while to find out about ‘$PSScriptRoot’, but that helps even more for the way I wanted to use it. I'm unable yet to mark answers as useful, but I'm marking it in spirit. – Emil Schlitzohr Oct 05 '18 at 17:48