【问题标题】:Move a file based on its filename根据文件名移动文件
【发布时间】:2015-10-09 18:00:39
【问题描述】:

我有一台联网的 PDF 打印机。它具有自动保存功能,但不允许我保存在用户指定的文件夹中(例如,\\server\users\<username>\pdfs\)。

它确实允许在保存后运行程序。所以,我需要一个脚本在保存后运行,并根据文件名将文件移动到该特定用户的保存目录。

目前,自动保存是使用username<date/time>.pdf 生成的,所以我需要一个脚本:

  1. 扫描将自动保存它们的文件夹
  2. 从文件名中提取用户名并将文件移动到\\servername\users\<username>\pdfs\

我的 Googlefu 运行不佳,我的脚本编写能力非常有限。任何帮助表示赞赏。

这是我目前正在使用的:

$autoSaveDir = "c:\autosave"
$userDir = "c:\userdir\%username%\pdfs"
$regexFirstNumber = "^[^\d]*(\d+)"

#iterate through the auto save directory
Get-ChildItem -Path $autoSaveDir -File | ForEach-Object {
  #find the username portion of the file by splitting on the first number in the filename
  $dateInFileName = [regex]::split($_.Name,'^[^\d]*(\d+)')

  $fileNameParts = $_.Name -split $dateInFileName[1]
  $userName = $fileNameParts[0]

  $newFile = $userDir -replace "%username%", $username
  $newFile = $newFile + "\" + $_.Name

  #copy the file over - doesn't check to make sure the folders are there first though
  Copy-Item $_.FullName $newFile
}

【问题讨论】:

    标签: powershell batch-file vbscript


    【解决方案1】:

    这个 VBScript 应该能满足你的需要:

    With CreateObject("Scripting.FileSystemObject")
        For Each File In .GetFolder("c:\autosave").Files
            If StrComp(.GetExtensionName(File.Name), "pdf", vbTextCompare) = 0 Then
    
                strUser     = .GetBaseName(File.Path)
                strUserRoot = .BuildPath("c:\userdir", strUser)
                strUserPdf  = .BuildPath(strUserRoot, "pdfs")
    
                If Not .FolderExists(strUserRoot) Then .CreateFolder strUserRoot
                If Not .FolderExists(strUserPdf)  Then .CreateFolder strUserPdf
    
                File.Move strUserPdf & "\"
    
            End If
        Next
    End With
    

    编辑:

    如果文件以yyyymmddhhnnssUsername.pdf格式命名,只需去掉前14个字符即​​可确定用户名:

    strUser = Mid(.GetBaseName(File.Path), 15)
    

    当文件被移动时,它仍然会包含时间戳。

    【讨论】:

    • 我稍微修改了一下,但是使用 CreateObject("Scripting.FileSystemObject") For Each File In .GetFolder("c:\pdfcreator") 得到“line 4 char 13 Path not Found” .Files If StrComp(.GetExtensionName(File.Name), "pdf", vbTextCompare) = 0 Then File.Move "c:\networkstorage\userstorage\" & .GetBaseName(File.Path) & "\pdfs\" End If下一个结尾
    • 每个用户的目标文件夹需要创建还是已经存在?
    • 此处文件夹已存在的测试用户是脚本运行前输出文件的示例 20150720104631administrator.pdf c:\networkstorage\userstorage\administrator\pdfs 存在
    • 所以文件上有时间戳?你的问题说他们被命名为username.pdf
    • 我删除了时间戳,并运行了 vbs,但仍然出现错误,但如果这使编码更容易,我可以删除时间戳。所以现在位于 c:\pdfcreator 中的 administrator.pdf 仍然没有移动到 c:\networkstorage\userstorage\administrator\pdfs
    【解决方案2】:

    这是我的答案,感谢邦德!

    With CreateObject("Scripting.FileSystemObject")
    For Each File In .GetFolder("c:\pdfcreator").Files
        If StrComp(.GetExtensionName(File.Name), "pdf", vbTextCompare) = 0 Then
    
            strUser = Mid(.GetBaseName(File.Path), 15)
            strUserRoot = .BuildPath("C:\Network Storage\Userstorage", strUser)
            strUserPdf  = .BuildPath(strUserRoot, "pdfs")
    
            If Not .FolderExists(strUserRoot) Then .CreateFolder strUserRoot
            If Not .FolderExists(strUserPdf)  Then .CreateFolder strUserPdf
    
            File.Move strUserPdf & "\"
    
        End If
    Next
    End With
    

    【讨论】:

      猜你喜欢
      • 2013-11-20
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2021-08-23
      • 2016-09-26
      • 1970-01-01
      相关资源
      最近更新 更多