1

The purpose of this script is to find the Name, directory and last write time of files and output it to a csv file.

get-childitem D:\testing -recurse -filter *.txt | select-object Name,DirectoryName,LastWriteTime, @{Name="New_colimn";Expression={"copy-item \`"DirectoryName\`" To_Compile_directory"}} | where { $_.DirectoryName -ne $NULL } | Export-CSV D:\testing\rdf.csv

My problem is that there is 1 cell I want to fill with another script that takes values from the generated csv file. Is there a way to pull the value of each DirectoryName and paste it into the Expression of the same row? I only get an error that says DirectoryName is an invalid key.

when I try to pull using $.DirectoryName the script only reads the $ and the value it has is the Name.

Thank for helping.

1 Answers1

1

Did you mean to collect the data from the files like this:

Get-ChildItem -Path 'D:\testing' -Filter '*.txt' -File -Recurse | 
Select-Object Name,DirectoryName,LastWriteTime | Export-Csv -Path 'D:\testing\rdf.csv' -NoTypeInformation

and then have your other script read the DirectoryName's from it like this?

$directories = (Import-Csv -Path 'D:\testing\rdf.csv').DirectoryName | Select-Object -Unique
# maybe do something with these directories here?
foreach ($folderPath in $directories) {
    # copy the directories including the files to an existing root destination folder
    Copy-Item -Path $folderPath -Destination 'D:\SomeExistingDestinationPath' -Recurse -Force
}
Theo
  • 57,719
  • 8
  • 24
  • 41
  • yep that is correct, first collect the data from the files like that... and then I would need my other script to read the data from prior script to execute a copy.. – John Paul Ranoa Apr 06 '22 at 12:47
  • `$directories = (Import-Csv -Path 'D:\testing\rdf.csv').DirectoryName | Select-Object -Unique Name,DirectoryName,LastWriteTime, @{Name="New_colimn";Expression={"copy-item \`"$directories\`" To_Compile_directory"}} | where { $_.DirectoryName -ne $NULL } | Export-CSV D:\testing\rdf1.csv` I assume my next script would look like this? I'm exporting another csv file for the second script.. apologies, I am very new to powershell. – John Paul Ranoa Apr 06 '22 at 12:50
  • @JohnPaulRanoa Nope, by ` $directories = (Import-Csv -Path 'D:\testing\rdf.csv').DirectoryName | Select-Object -Unique` you get an array of DirectoryNames (all strings). If you want to getg those directories in the second script and then copy them to somewhere else, please see my edit – Theo Apr 06 '22 at 12:57