$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles(“\cd1001-c100\public\isantillan”, “. ”, $searchOption);
foreach($fileName in $fileEntries)
{
[Console]::WriteLine($fileName);
}
How do i change the code above to make it output to a text file?
donj
April 2, 2014, 3:37am
2
You’re writing directly to the console. It isn’t intended to be redirected.
Use Write-Output instead. That writes to the pipeline and can be redirected.
Or, if you only want to write to a file, use Out-File.
[quote=14267]You’re writing directly to the console. It isn’t intended to be redirected.
Use Write-Output instead. That writes to the pipeline and can be redirected.
Or, if you only want to write to a file, use Out-File.
[/quote]
Thanks for the quick reply Don. I will give it a try and let you know how it goes.
[quote=14267]You’re writing directly to the console. It isn’t intended to be redirected.
Use Write-Output instead. That writes to the pipeline and can be redirected.
Or, if you only want to write to a file, use Out-File.
[/quote]
$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles(“\cd1001-c100\public\isantillan”, “. ”, $searchOption);
foreach($fileName in $fileEntries)
{
$fileName | Out-File “D:\Backups\Pdrive cleanup\files_to_delete.txt”
}
I was able to get it to out to file with your suggestion. The only issue is it only outputs 1 line instead of listing all the files in that given directory.
I was able to get it to work by adding the -append switch. Thanks Don Jones!
$searchOption =[IO.SearchOption]::AllDirectories
$fileEntries = [IO.Directory]::GetFiles(“\cd1001-c100\public\isantillan”, “. ”, $searchOption);
foreach($fileName in $fileEntries)
{
$fileName | Out-File “D:\Backups\Pdrive cleanup\files_to_delete.txt” -Append
}
/endthread
system
April 2, 2014, 12:54pm
6
On a side note, you can just pipe $fileEntries straight to Out-File, with no need for your own foreach loop:
$fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
$fileEntries | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt"
[quote=14282]On a side note, you can just pipe $fileEntries straight to Out-File, with no need for your own foreach loop:
$fileEntries = [IO.Directory]::GetFiles("\\cd1001-c100\public\isantillan", "*.*", $searchOption);
$fileEntries | Out-File "D:\Backups\Pdrive cleanup\files_to_delete.txt"
[/quote]
Hello Dave, i tried it your way and that works as well! Thanks for providing an alternate solution!