powershell - 监控无效文件名并设置警报

标签 powershell batch-file vbscript

我想监视人们为某些事件放置 Excel 工作表的特定文件夹。

我想确保人们在放置 Excel 工作表之前应注意以下规则。

  1. Excel 工作表应为 xls 格式(Excel 97-2003 Workboomk 格式)
  2. Excel 文件名不应包含任何空格
  3. 文件名长度不得超过 15 个字符。

如果用户在未遵循上述 3 条规则的情况下放置了 Excel 工作表,我想设置一封电子邮件提醒。

我需要 VB 或 Powershell 脚本或批处理来监视文件夹。请帮助我解决这个问题。

最佳答案

这听起来像是两个步骤。第 1 步,编写一段可运行来检查文件条件是否正确的代码。第 2 步,通过电子邮件向用户发送错误命名文件的列表。第 1 步相当简单。

Dim fso, scanFolder, files, n, fileList, fileArray
Set fso = CreateObject("Scripting.FileSystemObject")
Set scanFolder = fso.GetFolder("C:\Your\Folder\Here")
Set files = scanFolder.Files
fileList = ""

for each n in files
    if len(n.name) > 15 or right(n.name, 3) <> "xls" or InStr(n.name, " ") > 0 then
        fileList = fileList & n.name & "|"
    end if
next

fileList = left(fileList, len(fileList) - 1)
fileArray = split(fileList, "|")

这将获取所有命名不当的文件的列表。

第 2 步。不幸的是,据我所知,FSO 无法从文件中获取所有者名称。您能做的最好的事情就是通过电子邮件发送一份包含错误命名文件列表的通讯组列表。我希望这会有所帮助。

编辑:这是我在脚本中使用的电子邮件代码。

Dim objEmail, strFileList, x
strFileList = ""

for each x in fileArray
    strFileList = strFileList & x & vbNewLine
next

Set objEmail = CreateObject("CDO.Message")
with objEmail
    .From = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98deeaf7f5d8fdf5f9f1f4b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>"
    .To = ""
    .CC = ""

    .Subject = "List of wrong files"
    .Body = strFileList

    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = 'you will need to get the code for this from IT'
    .Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    .Configuration.Fields.Update
end with
objEmail.Send

关于powershell - 监控无效文件名并设置警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801422/

相关文章:

java - 从 Java 程序执行批处理文件时出错

Python 多处理 - 是否可以在各个进程之间引入固定时间延迟?

windows - 批量编码文本文件

windows - 用于删除某些文件的 VB 脚本,如果找到文件,则将其他文件复制到目录

VBScript:如何利用从函数返回的字典对象?

powershell - 写主机-丢失新行格式

arrays - Powershell foreach(列出引用的数组项命令)

azure - 如何在 Azure SSAS 上部署多维数据集(XMLA 格式)

windows - 捕获 VBScript 中的任何错误?

powershell - Invoke-Command 中的 ArgumentList 参数不发送所有数组