by tommls at 2013-01-09 10:28:31
write-host $dir.fullname “: †$OldestFile.count †Files†| Export-Csv d:\procoff.csv –NoTypeInformationby ArtB0514 at 2013-01-09 10:48:51
I get no errors.
I don’t absolutely need the write-host part, it works correctly, it is there to show me something is happening.
It should write into the csv file two columns per line, an example is:
D:\2012 Foo UPgrade\SQLUpgradeDocs : 3 Files
Thank you, Tom
Fromby tommls at 2013-01-09 11:01:03Get-Help Write-Host -Full
[quote]OUTPUTS
None
Write-Host sends the objects to the host. It does not return any objects. However, the host might display the objects that Write-Host sends
to it.[/quote]
Write-Host doesn’t pass anything to the pipeline, so there’s nothing to export to a csv file. Try something like this:$Data = "$($dir.fullname) : $($OldestFile.count) Files"
$Data
$Data | Export-Csv d:\procoff.csv –NoTypeInformation
That’ll fix your empty export issue, but I’m pretty sure that this isn’t going to give you what you expect. In order to get two columns in your CSV file, you’ll need to create an object with two properties. As it stands, you’re just exporting a simple string.
This might be more like what you want to achieve:$Data = New-Object PSObject -Property @{
'Directory'=$Dir.FullName
'Count'=$OldestFile.count
}
"$($Data.Directory) : $($Data.Count) Files"
$Data | Export-CSV d:\procoff.csv -NoTypeInformation
close but definitely no cigarby tommls at 2013-01-09 11:16:33
all i get is one cell that says ‘Length 60’
the resulting csv file should contain thousands of lines resembling the example line above
thank you, tom
out-file may be equally useful…by DonJ at 2013-01-09 11:25:59
Ugh, Write-Host. "Every time someone uses Write-Host, God kills a puppy."by tommls at 2013-01-09 11:53:22
Tom, can you post two or three lines that you’d expect to see in the CSV file? I know you posted something in your original post, but it didn’t include a comma, so I’m not sure I’m understanding what the output should be.
Sample lines:by DonJ at 2013-01-09 11:57:03
D:\mountainweaversguild.com\guild posted to server with corrections\Weavers\RiesaW 5
D:\mountainweaversguild.com\guild posted to server with corrections\Weavers\RosemaryD 5
D:\mountainweaversguild.com\guild posted to server with corrections\Weavers\Staff
D:\mountainweaversguild.com\guild posted to server with corrections\Weavers\TioS 4
D:\mountainweaversguild.com\guild posted to server with corrections\Weavers\TomB 4
If no number after the directory name, then no files involved.
Request was to find how many millions of files we have on our file server older than 1/1/2007.
This line gives me the result I want in a TEXT file:
$Data | Out-File d:\procoff.txt -append
FULL SCRIPT:cls
# $date = get-date “01/12/2012? # local format here: dd/mm/yyyy
$date = "12/31/2011"
$dirs = get-childitem “D:*.†-recurse | Where-Object {$.psIsContainer -eq $true}
ForEach ($dir in $dirs)
{
$OldestFile = get-childitem $dir.fullname . | where-object {$.LastWriteTime -le $date}
$Data = "$($dir.fullname) $($OldestFile.count)"
$Data
$Data | Export-Csv d:\procoff.csv -NoTypeInformation -force
$Data | Out-File d:\procoff.txt -append
}
HOWEVER – I noticed running the test script on my PC that NOT all the directories are being included, I don’t know why???
Thank you, Tom
Ah. Ok - you’ve got two threads open on this topic. Lets just do this one. Ill get back to you when I’m back in the office.by tommls at 2013-01-09 12:01:07
Yes, thank you…by DonJ at 2013-01-09 13:38:26
I’m using v3 syntax. You may need to adjust if you’re using v2.by tommls at 2013-01-09 13:42:48
function Get-OldFiles {
param($date)
# Assume $date contains the date you want
$folders = Dir D:*.-Recurse -Directory
foreach ($folder in $folders) {
$oldfiles = Dir $folder.fullname . | where LastWriteTime -le $date
$data = @{‘Folder’=$dir.fullname;‘Count’=$oldfiles.count}
New-Object -Type PSObject -Prop $data
}
}
Get-OldFiles ‘1/1/2006’ | Export-CSV filename.csv
Thank you, but I don’t know anything about PS v.3…by DonJ at 2013-01-09 13:45:47
Where may I find something that translates these v.3 lines into v.2??$data = @{‘Folder’=$dir.fullname;‘Count’=$oldfiles.count}
New-Object -Type PSObject -Prop $data
Thank you, Tom
It’s not that hard, dude.by megamorf at 2013-01-09 16:31:25
Instead of…
Dir D:*.* -Recurse -Directory
Do this…
get-childitem “D:*.*†-recurse | Where-Object {$.psIsContainer -eq $true}
Which you already had in your script. And instead of this…
where LastWriteTime -le $date
Do this…
where-object {$.LastWriteTime -le $date}
Which you also already had in your script. You can also learn about PowerShell v3 - it’s out! - and download it. It’ll install on Windows 7, but not Windows XP or Windows Vista. As you can see, some of the v3 syntax is a bit easier. It’ll help you learn this stuff a little more easily.
Hope that helps.
[quote="tommls"]Where may I find something that translates these v.3 lines into v.2??by tommls at 2013-01-09 16:38:45$data = @{‘Folder’=$dir.fullname;‘Count’=$oldfiles.count}
New-Object -Type PSObject -Prop $data
Thank you, Tom[/quote]
What you see here is the creation of a custom object based on a hashtable which works the same in Powershell version 2 and 3[1]NEM-PC {C:\Users\nem}
>$hashtable = @{'Location'="Berlin";'Country'="Germany"}
[2]NEM-PC {C:\Users\nem}
>$hashtable
Name Value
---- -----
Country Germany
Location Berlin
[3]NEM-PC {C:\Users\nem}
>$newobject = New-Object -TypeName PSObject -Property $hashtable
[4]NEM-PC {C:\Users\nem}
>$newobject
Country Location
------- --------
Germany Berlin
[quote="DonJ"]It’s not that hard, dude.by tommls at 2013-01-10 07:05:38
Which you also already had in your script. You can also learn about PowerShell v3 - it’s out! - and download it. It’ll install on Windows 7, but not Windows XP or Windows Vista. As you can see, some of the v3 syntax is a bit easier. It’ll help you learn this stuff a little more easily.
Hope that helps.[/quote]
Helps tremendously – thank you – the ONLY thing I know up to now about PS 3.0 is that it’s out/released…I don’t do PS stuff real often and practically must start over again each time I work with it – after I read this several more times I will know what to do etc.
P’raps when I try again it will actually traverse the whole D:\ drive instead of just a few folders…but first I must get the script working.
Thank you, Tom
Hello,by DonJ at 2013-01-10 08:02:45
Got it working as you suggested.
Code looks like this now, but it only pulls the first 2052 directories, why??
function Get-OldFiles {
param($date)
# Assume $date contains the date you want
$folders = get-childitem “D:*.*†-recurse | Where-Object {$.psIsContainer -eq $true}
foreach ($folder in $folders) {
$oldfiles = Dir $folder.fullname . | where-object {$.LastWriteTime -le $date}
$data = @{‘Folder’=$folder.fullname;‘Count’=$oldfiles.count}
New-Object -Type PSObject -Prop $data
}
}
Get-OldFiles ‘1/1/2011’ | Export-CSV d:\procoff.csv -NoTypeInformation
Thank you, Tom
Couldn’t tell you. I ran it on my machine and it’s listed several thousand.by tommls at 2013-01-10 10:49:06
I found the problem was the . after D:.
Removing . fixed the script.
How can I exclude folders with null or zero count?? – do I use an if statement??
Final script with an if statement to exclude zero/null folders, still work to do, but this is generally what I need now:cls
function Get-OldFiles {
param($date)
# Assume $date contains the date you want
$folders = get-childitem “D:\†-recurse | Where-Object {$.psIsContainer -eq $true}
foreach ($folder in $folders) {
$oldfiles = Dir $folder.fullname . | where-object {$.LastWriteTime -le $date}
if ($oldfiles.count) {
$data = @{‘Folder’=$folder.fullname;‘Count’=$oldfiles.count}
New-Object -Type PSObject -Prop $data
}
}
}
Get-OldFiles ‘1/1/2011’ | Export-CSV d:\procof22f.csv -NoTypeInformation
Thank you, Tom