我在Powershell中编写一个脚本来收集有关我的存档文件夹中有多少文件的信息,该脚本是计算文件夹中的文件然后它删除的文件比提供日期更早,然后再次计算文件数报告删除的文件数量。
我可以让它工作但是我遇到了一个语法问题。当我这样的时候它工作正常。
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
但是,如果我尝试更改#str1赋值以使其在一行中并且同时取两个字符串,则会出现此错误(请参阅下面的代码)
无法将值“文件数”转换为“System.Int32”类型。错误:“输入字符串的格式不正确。” 在行:13 char:1 + $ str1 =“文件数”+ $ ErrorFiles.ToString()+“是”+ $ 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
谢谢你的帮助!
答案 0 :(得分:2)
从第二行的开头删除连字符:
$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"
^
在genereal中,格式运算符(-f
)是构建字符串的更好方法:
$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