【发布时间】:2015-10-09 18:00:39
【问题描述】:
我有一台联网的 PDF 打印机。它具有自动保存功能,但不允许我保存在用户指定的文件夹中(例如,\\server\users\<username>\pdfs\)。
它确实允许在保存后运行程序。所以,我需要一个脚本在保存后运行,并根据文件名将文件移动到该特定用户的保存目录。
目前,自动保存是使用username<date/time>.pdf 生成的,所以我需要一个脚本:
- 扫描将自动保存它们的文件夹
- 从文件名中提取用户名并将文件移动到
\\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