PowerShell script to set "date created" to specific times according to a csv file

440 views Asked by At

I have some thousands of files in a folder that I need to bulk edit their creation time to a specific order. I have prepared a csv file with all file names and the preferred creation times, like this:

filename;filecreationTime;
file1.mp4;10/11/2022 2:50;
file2.mp4;10/11/2022 2:49;
file3.mp4;10/11/2022 2:49;
etc

I have used this suggestion to a similar previous question: https://stackoverflow.com/a/36348448/20467894 and created a code like this:

Set-Location 'path to files'
Import-Csv -Path 'path to csv file' | 
    ForEach-Object { (Get-Item $_.filename).CreationTime = (Get-date $_.filecreationTime) }

The outcome is this error, for each line of the csv:

Get-Item : Cannot bind argument to parameter 'Path' because it is null.
At line:2 char:32
+     ForEach-Object { (Get-Item $_.filename).CreationTime = (Get-date  ...
+                                ~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-Item], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.GetItemComm
   and

What am I doing wrong?

EDIT: After Theos' comment, it run once in a sample subset of my files but never run again. Now it can indeed read the filenames, but it brings a new error:

Get-date$_.filecreationTime : The term 'Get-date$_.filecreationTime' is not recognized as the name of a cmdlet, functio
n, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
 correct and try again.
At line:2 char:57
+ ... (Get-Item $_.filename).CreationTime = (Get-date$_.filecreationTime) }
+                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Get-date$_.filecreationTime:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
1

There are 1 answers

0
Helias Valenti On

Thanks everyone for your suggestions. As pointed out by Theo, the problem was that in my language and regional formats the delimiter of the csv files is set to ";" instead of ",", as the latter is used as decimal...

So I had to insert an additional argument into the import csv (that was not obvious by the error expression), in order to clarify the non default delimiter:

-Delimiter ';'

So, the correct code for me was the following:

Set-Location 'path to files'
Import-Csv -Delimiter ';' -Path 'path to csv file' | 
    ForEach-Object { (Get-Item $_.filename).CreationTime = (Get-date $_.filecreationTime) }