将文件从一个文件夹复制到另一个文件夹,然后使用PowerShell或Python脚本使用当前日期/时间和新文件ext重命名

时间:2015-06-04 00:56:09

标签: python windows shell powershell scripting

我对PowerShell脚本没有任何经验,所以我需要一些帮助来创建一个简单的shell脚本。 shell脚本需要执行以下操作:

  1. 该脚本将在目录M:\ProgramData\Aspect Software\Unified IP\Advanced List Management\tenant1\xfer\CRM_Upload.upl中查找文件扩展名为.upl的文件名。

  2. shell脚本需要复制xfer\uploads文件并将其放入同一目录中的文件夹中,目标文件夹名为"上传" (hhmmssmmddyyyy)。

  3. 在将脚本(CRM_Upload_hhmmssmmddyyyy)复制到Uploads文件夹后,shell脚本还需要将当前日期/时间(.txt)附加到文件名末尾。

  4. 最后,shell脚本需要将文件扩展名更改为.upl而不是原来的CRM_Upload_hhmmssmmddyyyy.txtFunction 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" )。

  5. 据我所知:

    这是我使用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

2 个答案:

答案 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)