windows - Copy file from one folder to another and then rename using current date/time and new file ext using PowerShell or Python script -
i not have experience powershell scripts need create simple shell script. shell script needs following:
the script file name
.upl
file extension in directorym:\programdata\aspect software\unified ip\advanced list management\tenant1\xfer\crm_upload.upl
.the shell script needs copy
.upl
file , place folder in same directory, destination folder called "uploads" (xfer\uploads
).the shell script needs append current date/time (
hhmmssmmddyyyy
) onto end of file name after has been copied uploads folder underscore (crm_upload_hhmmssmmddyyyy
).finally shell script needs change file extension
.txt
instead of original.upl
(crm_upload_hhmmssmmddyyyy.txt
).
this far got:
this stab took @ using powershell script...i prefer if copied file , renamed instead of moving it. can me below?
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 } ## part 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"
*** here python script trying work on still not right..
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" = datetime.datetime.now() date = now.strftime("%y%m%d_%h%m%s") new_folder = xfer_dir + "uploads\\" dest = new_folder + '_' + str(date) + '.txt'
main script
shutil.copy(source_file, dest)
***with little bit of hard work , trial , error got script working..
import shutil import datetime ### variable definitions system import environment data_dir_base = environment.getenvironmentvariable('lmsrvshared') xfer_dir = data_dir_base + "tenant1\\xfer\\" source_file = xfer_dir + "crm_dialer_upload.txt" = 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)
for shell scripting in windows can use batch (.bat), prefer (.vbs)
here want 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) set objfolder = fso.createfolder(strnewdir) end if ' search file, date, copy , rename each file in folder.files if (instr(file.name, ".") > 0) if (lcase(mid(file.name, instrrev(file.name, "."))) = strfileextension) ' file name set objfile = fso.getfile(file.name) ' 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
Comments
Post a Comment