2015-07-20 121 views
1

我有一臺網絡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 
} 

回答

0

這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) 

當文件移動時,它仍將包含時間戳。

+0

我修改了那麼一點點,但我得到 「4號線未找到焦炭13路」 用的CreateObject( 「Scripting.FileSystemObject的」) 每個文件在.GetFolder( 「C:\的PDFCreator」 ).Files 如果StrComp(.GetExtensionName(File.Name),「pdf」,vbTextCompare)= 0然後 File.Move「c:\ networkstorage \ userstorage \」&.GetBaseName(File.Path)&「\ pdfs \ 「 End If Next End With – du3ly

+0

是否需要創建每個用戶的目標文件夾還是已經存在? – Bond

+0

測試用戶這個文件夾已經存在這裏是腳本運行之前的輸出文件的一個例子20150720104631administrator.pdf c:\ networkstorage \ userstorage \ administrator \ pdfs存在 – du3ly

0

這是我的答案謝謝邦德!

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 
相關問題