Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy and change multiple files

Status
Not open for further replies.

GarAng

IS-IT--Management
Jun 14, 2002
23
GB
New to scripting & having a steep learning curve in scripting this week!
The script below allows me to copy named files in a folder using a txt file, to a backup folder

Then it looks at the creation date, renames the file and puts the file into the folder of the same month, or creates it if the folder doesn't exist

It only does it for the ext of the file in the strExt = "csv"

I'd like to add other ext options to the same script.
is this possible? or do i have to have a separate script for each file ext

many thanks

Public fso
set fso = CreateObject("Scripting.FileSystemObject")
set Cfg = fso_OpenTextFile("C:\Documents and Settings\uidb3\Desktop\ref_list.txt")
do while not Cfg.AtEndOfStream
str = RTrim(Cfg.ReadLine)
fso.CopyFile str, fso.BuildPath("C:\ga\backups",fso.GetFileName(str)), True
loop
' Rename & Move to folder or create folder if doesn't exist
strSource = "C:\ga\backups"
strExt = "csv"
strDest = "C:\ga\backups"


Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSource = objFSO.GetFolder(strSource)

For Each objFile in objSource.Files
strFileExt = objFSO.GetExtensionName(objFile.Path)

if LCase(strFileExt) = LCase(strExt) Then
If strFileExt <> "" Then
strFileExt = "." & strFileExt
End If

strMM = Right("0" & Month(objFile.DateCreated), 2)
strDD = Right("0" & Day(objFile.DateCreated), 2)
strYY = Right(Year(objFile.DateCreated), 2)
strYYYY = Year(objFile.DateCreated)

strNewName = Left(objFile.Name, 4) & "_" & strYYYY & strMM & strDD & strFileExt
strNewFolder = strDest & "\" & strYYYY & strMM
strNewPath = strNewFolder & "\" & strNewName

If Not objFSO.FolderExists(strNewFolder) Then
objFSO.CreateFolder(strNewFolder)
End If

'WScript.Echo "Move " & objFile.Path & " to " & strNewPath
objFSO.MoveFile objFile.Path, strNewPath
End If
Next




 
I would use a dictionary object. That way you can check if the key exists. It will be efficient and easy to modify.

Replace strExt = "csv" with

Code:
Dim oDic
Set oDic = CreateObject("scripting.dictionary")
oDic.Add "csv", 1
oDic.Add "txt", 1
oDic.Add "doc", 1

Then for your check, instead of if LCase(strFileExt) = LCase(strExt) Then use

Code:
If oDic.Exists(objFSO.GetExtensionName(objFile.Path)) Then

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Many thanks
Just what I was after
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top