我对PowerShell脚本没有任何经验,所以我需要一些帮助来创建一个简单的shell脚本。 shell脚本需要执行以下操作:
该脚本将在目录M:\ProgramData\Aspect Software\Unified IP\Advanced List Management\tenant1\xfer\CRM_Upload.upl
中查找文件扩展名为.upl
的文件名。
shell脚本需要复制xfer\uploads
文件并将其放入同一目录中的文件夹中,目标文件夹名为"上传" (hhmmssmmddyyyy
)。
在将脚本(CRM_Upload_hhmmssmmddyyyy
)复制到Uploads文件夹后,shell脚本还需要将当前日期/时间(.txt
)附加到文件名末尾。
最后,shell脚本需要将文件扩展名更改为.upl
而不是原来的CRM_Upload_hhmmssmmddyyyy.txt
(Function RenameMoveFile($locationPath, $fileName, $extension1, $extension2, $archiveFolder)
{
$date = Get-Date -uFormat ""%y%m%d-%H%M%S""
$old = $locationPath + $fileName + $extension1
$new = $locationPath + $fileName + "_" + $date + $extension2
$archiveFolder = $locationPath + $archiveFolder + "\"
Rename-Item $old $new
Move-Item $new $archiveFolder
}
## This is the only part that we'd edit
RenameMoveFile -locationPath "M:\ProgramData\Aspect Software\Unified IP\Advanced List Management\tenant1\xfer" -fileName "CRM_DIALER_UPLOAD" - extension1 ".upl" -extension2 ".txt" -archiveFolder "Uploads"
)。
据我所知:
这是我使用PowerShell脚本进行的另一次尝试...我希望它复制文件并重命名而不是移动它。有人可以帮我这个吗?
from System import Environment
data_dir_base = Environment.GetEnvironmentVariable('LMSRVSHARED')
xfer_dir = data_dir_base + "tenant1\\xfer\\"
source_file = xfer_dir + "CRM_DIALER_UPLOAD.txt"
now = datetime.datetime.now()
date = now.strftime("%Y%m%d_%H%M%S")
new_folder = xfer_dir + "Uploads\\"
dest = new_folder + '_' + str(date) + '.txt'
***这是我尝试使用的Python脚本,但它仍然不正确..
导入shutil 导入日期时间
import shutil
import datetime
### Variable Definitions
from System import Environment
data_dir_base = Environment.GetEnvironmentVariable('LMSRVSHARED')
xfer_dir = data_dir_base + "tenant1\\xfer\\"
source_file = xfer_dir + "CRM_DIALER_UPLOAD.txt"
now = datetime.datetime.now()
date = now.strftime("%Y%m%d_%H%M%S")
new_folder = xfer_dir + "Uploads\\"
dest = new_folder + 'CRM_DIALER_UPLOAD' + '_' + str(date) + '.txt'
### Main Script
shutil.copy(source_file, dest)
shutil.copy(source_file,dest)
***经过一些努力和反复试验,我得到了脚本的工作......
log = True
答案 0 :(得分:0)
对于Windows中的shell脚本,您可以使用批处理(.bat),但我更喜欢(.vbs)
这是你想要的VbScript:
Dim fso, folder, objFolder, file, objFile
Dim strDir, strFileExtension, strNewDir, strNewExtension, strDate
' Parameters
strDir = "M:\ProgramData\Aspect Software\Unified IP\Advanced List Management\tenant1\xfer\"
strFileExtension = ".upl"
strNewDir = "M:\ProgramData\Aspect Software\Unified IP\Advanced List Management\tenant1\xfer\uploads\"
strNewExtension = ".txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(strDir)
' create new dir
If Not fso.FolderExists(strNewDir) Then
Set objFolder = fso.CreateFolder(strNewDir)
End If
' search for file, get date, then copy and rename
For each file In folder.Files
If (InStr(file.name, ".") > 0) Then
If (LCase(Mid(file.name, InStrRev(file.name, "."))) = strFileExtension) Then
' get only the file name
Set objFile = fso.GetFile(file.name)
' get date
strDate = Right("0" & DatePart("h",now), 2) _
& Right("0" & DatePart("n",now), 2) _
& Right("0" & DatePart("s",now), 2) _
& Right("0" & DatePart("m",now), 2) _
& Right("0" & DatePart("d",now), 2) _
& DatePart("yyyy",now) _
' copy file
CreateObject("Scripting.FileSystemObject").CopyFile file.name, strNewDir & fso.GetBaseName(objFile) & "_" & strDate &
strNewExtension
End If
End If
Next
答案 1 :(得分:0)
import shutil
import datetime
### Variable Definitions
from System import Environment
data_dir_base = Environment.GetEnvironmentVariable('LMSRVSHARED')
xfer_dir = data_dir_base + "tenant1\\xfer\\"
source_file = xfer_dir + "CRM_DIALER_UPLOAD.txt"
now = datetime.datetime.now()
date = now.strftime("%Y%m%d_%H%M%S")
new_folder = xfer_dir + "Uploads\\"
dest = new_folder + 'CRM_DIALER_UPLOAD' + '_' + str(date) + '.txt'
### Main Script
shutil.copy(source_file, dest)