Reputation: 49
I writing a script in Powershell to gather information about how many files are in my archive folder, the script is to count the files in the folder then it deletes file older than give date and then it counts the number of files again to report how many files it deleted.
I can get this to work however I am running into a little problem with the syntax. When I have it like this it works fine.
Clear-Host
$ErrorFiles = "c:\data\ErrorFiles"
$Archivefiles = "c:\data\Archivefiles"
$PreCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PreCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
Get-ChildItem $ErrorFiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
Get-ChildItem $Archivefiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
$PostCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PostCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n"
$str2 = "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
write-host $str1
write-host $str2
However if I try to change the #str1 assignment to have it in one line and take both the strings I get this error (see code below)
Cannot convert value "Number of files in" to type "System.Int32". Error: "Input string was not in a correct format." At line:13 char:1 + $str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErro ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [], RuntimeException + FullyQualifiedErrorId : InvalidCastFromStringToInteger
Clear-Host
$ErrorFiles = "c:\data\ErrorFiles"
$Archivefiles = "c:\data\Archivefiles"
$PreCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PreCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
Get-ChildItem $ErrorFiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
Get-ChildItem $Archivefiles | Where {$_.LastWriteTime –lt ((Get-Date).AddDays(-10) )} | ForEach { Remove-Item $_.FullName }
$PostCountErrorFiles = ((Get-ChildItem $ErrorFiles | Measure-Object).Count)
$PostCountArchivefiles = ((Get-ChildItem $Archivefiles | Measure-Object ).Count)
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n" +
- "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
write-host $str1
Thanks for you help!
Upvotes: 0
Views: 6982
Reputation: 200373
Remove the hyphen from the beginning of the second line:
$str1 = "Number of files in " + $ErrorFiles.ToString() + " was " + $PreCountErrorFiles.ToString() + " number of files deleted " + ($PreCountErrorFiles - $PostCountErrorFiles).ToString() + " and " + $PostCountErrorFiles.ToString() + " are left." + "`n" +
- "Number of files in " + $Archivefiles.ToString() + " was " + $PreCountArchivefiles.ToString() + " number of files deleted " + ($PreCountArchivefiles - $PostCountArchivefiles).ToString() + " and " + $PostCountArchivefiles.ToString() + " are left." + "`n"
^
In genereal, the format operator (-f
) is a better approach to building strings like this:
$str1 = "Number of files in {0} was {1} number of files deleted {2} and {3} " +
"are left.`nNumber of files in {4} was {5} number of files deleted " +
"{6} and {7} are left.`n"
$str1 -f $ErrorFiles, $PreCountErrorFiles,
($PreCountErrorFiles - $PostCountErrorFiles),
$PostCountErrorFiles, $Archivefiles, $PreCountArchivefiles,
($PreCountArchivefiles - $PostCountArchivefiles),
$PostCountArchivefiles
Upvotes: 2