Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Create folder in vbs

Posted on 2009-07-13
4
Medium Priority
?
689 Views
Last Modified: 2012-05-07
Hi Experts
I need to create a vbs script that allows me to rename and copies files to a folder on the same server

I have managed to find the code for renaming the file based on the creation date
But now I also need for this file to be copied to a folder that potentially doesnt exist

i.e.

report is created called sample.csv

script is run nightly to pick up the new file and rename it to the creation date sam_120709.csv

how do I then move the file into a folder that doesnt exist

am looking to put files into monthly folders

folder name is simply yyyymm


Below is what I have so far

Thanks in advance

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C \Report'} Where " _
        & "ResultClass = CIM_DataFile")
 
For Each objFile In FileList
    strDate = Left(objFile.CreationDate, 8)
    strNewName = objFile.Drive & objFile.Path & _
       "X143" & "_" & strDate & "." & "csv"
    strNameCheck = Replace(strNewName, "\", "\\")
 
    i = 1
    Do While True
        Set colFiles = objWMIService.ExecQuery _
            ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'")
        If colFiles.Count = 0 Then
            errResult = objFile.Rename(strNewName)
            Exit Do
        Else
            i = i + 1
            strNewName = objFile.Drive & objFile.Path & _
                "X143" & "_" & strDate & "_" & i & "." & "csv"
            strNameCheck = Replace(strNewName, "\", "\\")
        End If
    Loop
Next

Open in new window

0
Comment
Question by:Ninjaguy900
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 9

Expert Comment

by:skipper68
ID: 24841587
This should work for you.
'Set New Folder name for date
yy = DatePart("yyyy",DateAdd("d",-1,Date))
mon = DatePart("m",DateAdd("d",-1,Date))
If len(mon) = 1 Then
     mon = "0" & mon
End If
 
'Set New Folder, Create new folder
NewFolder = "C:\test\" & yy & mon
objFSO.CreateFolder (NewFolder)
wScript.Echo "Folder Created: " & newfolder
 
'Creates Folder Obj
Set objFolder = objFSO.GetFolder(foldera)
Set colFiles = objFolder.Files
 
'Start Files Move
For Each objFile In colFiles
     WScript.Echo "Moving " & objFile.Path & " to " & newfolder & "..." & objFSO.GetFileName(objFile.Path)
     objFSO.MoveFile objFile.Path, NewFolder & "\"
Next

Open in new window

0
 
LVL 38

Accepted Solution

by:
Shift-3 earned 2000 total points
ID: 24841798
Paste the script below into a text file with a .vbs extension.  Customize the value of the strSource variable on line 1 with the location of the folder containing the report files.  Customize the value of the strDest variable on line 3 with the location of the folder under which to create dated subfolders.  This can be the same as the source directory.

Running the script (e.g. with cscript scriptname.vbs) will echo the actions to be taken.  Once you have tested it successfully and are sure it will do what you intend, remove the apostrophe from the beginning of line 30 to move and rename the files.


strSource = "C:\Report"
strExt = "csv"
strDest = "C:\Report"
 
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, 3) & "_" & strMM & strDD & strYY & 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

Open in new window

0
 

Author Comment

by:Ninjaguy900
ID: 24847728
Many thanks for you prompt response
I'm having a very steep learning curve !
Skipper68 - I am getting an error on your code on line 10 - really done know why
Shift-3 the code works to a point, it creates the folders based on the creation date, but fails to move them into the folder
Am I missing something?

What should the correct process be
copy , rename, then move, then delete
or is it possible to rename & move in the same segment of script

many thanks
0
 

Author Comment

by:Ninjaguy900
ID: 24847984
My bad Shift-3:
hadn't removed the apostrophe from the beginning of line 30!
doh


many thanks
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question