VB script

I am looking to move files in a folder that bases on yesterdays date then remove that folder when empty.  

Folder structure z:\Orders\Problems\mm-dd-yyyy

I would like to move the files from  z:\Orders\Problems\mm-dd-yyyy  (date is current date – 1 day), to z:\orders

Once the folder is empty remove that folder.
JaysonJacksonAsked:
Who is Participating?
 
Bill PrewCommented:
In your original post of the question, you showed dashes in the date format, now you seem to be saying it is underscores.  Just adjust line 16 to your needs.

~bp
0
 
Bill PrewCommented:
Give this VBS script a test, I think it should do what you need.

' Specify folder to work in
strBaseDir = "z:\Orders\Problems"
strDestDir = "z:\Orders"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Quit if specified folder doesn't exist
If Not objFSO.FolderExists(strBaseDir & "\") Then 
   Wscript.Echo "Base folder [" & strBseDir & "] missing, ending."
   Wscript.Quit
End If

' Calculate yesterdays date, format as MM_DD_YYYY
dtmValue = DateAdd("d", -1, Now())
strDate = LPad(Month(dtmValue), 2, "0") & "-" & LPad(Day(dtmValue), 2, "0") & "-" & Year(dtmValue)

' Build name of yesterdays folder using date
strYesterdayFolder = strBaseDir & "\" & strDate

' Access the folder
Set objFolder = objFSO.GetFolder(strYesterdayFolder)

' Process it if it has files to move
If objFolder.Files.Count > 0 Then
   ' Move all files
   objFSO.MoveFile strYesterdayFolder & "\*.*", strDestDir
End If

' Make sure it's now empty and delete
If objFolder.Files.Count > 0 Then
   ' Delete folder
   objFSO.DeleteFolder strYesterdayFolder
End If

' Done
Set objFSO = Nothing

' Left pad a string to any length with a specified character
Function LPad( strText, intLen, chrPad )
   LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
0
 
JaysonJacksonAuthor Commented:
i changed Orders to  Results

received message   Line 22  char 1   path not found    cod 800A004c


' Specify folder to work in
strBaseDir = "z:\results\Problems"
strDestDir = "z:\results"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Quit if specified folder doesn't exist
If Not objFSO.FolderExists(strBaseDir & "\") Then
   Wscript.Echo "Base folder [" & strBseDir & "] missing, ending."
   Wscript.Quit
End If

' Calculate yesterdays date, format as MM_DD_YYYY
dtmValue = DateAdd("d", -1, Now())
strDate = LPad(Month(dtmValue), 2, "0") & "-" & LPad(Day(dtmValue), 2, "0") & "-"

& Year(dtmValue)

' Build name of yesterdays folder using date
strYesterdayFolder = strBaseDir & "\" & strDate

' Access the folder
Set objFolder = objFSO.GetFolder(strYesterdayFolder)

' Process it if it has files to move
If objFolder.Files.Count > 0 Then
   ' Move all files
   objFSO.MoveFile strYesterdayFolder & "\*.*", strDestDir
End If

' Make sure it's now empty and delete
If objFolder.Files.Count > 0 Then
   ' Delete folder
   objFSO.DeleteFolder strYesterdayFolder
End If

' Done
Set objFSO = Nothing

' Left pad a string to any length with a specified character
Function LPad( strText, intLen, chrPad )
   LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

 
Bill PrewCommented:
That would indicate the dated folder for yesterday was not found.  The will check for that and display a message when that happens.

' Specify folder to work in
strBaseDir = "z:\Results\Problems"
strDestDir = "z:\Results"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Quit if specified folder doesn't exist
If Not objFSO.FolderExists(strBaseDir & "\") Then 
   Wscript.Echo "Base folder [" & strBseDir & "] missing, ending."
   Wscript.Quit
End If

' Calculate yesterdays date, format as MM_DD_YYYY
dtmValue = DateAdd("d", -1, Now())
strDate = LPad(Month(dtmValue), 2, "0") & "-" & LPad(Day(dtmValue), 2, "0") & "-" & Year(dtmValue)

' Build name of yesterdays folder using date
strYesterdayFolder = strBaseDir & "\" & strDate

If Not objFSO.FolderExists(strYesterdayFolder & "\") Then 
   Wscript.Echo "Yesterday folder [" & strYesterdayFolder & "] missing, ending."
   Wscript.Quit
End If

' Access the folder
Set objFolder = objFSO.GetFolder(strYesterdayFolder)

' Process it if it has files to move
If objFolder.Files.Count > 0 Then
   ' Move all files
   objFSO.MoveFile strYesterdayFolder & "\*.*", strDestDir
End If

' Make sure it's now empty and delete
If objFolder.Files.Count > 0 Then
   ' Delete folder
   objFSO.DeleteFolder strYesterdayFolder
End If

' Done
Set objFSO = Nothing

' Left pad a string to any length with a specified character
Function LPad( strText, intLen, chrPad )
   LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
0
 
Bill PrewCommented:
One additional small change to get the folder deleted after the files are moved.

' Specify folder to work in
strBaseDir = "Z:\Results\Problems"
strDestDir = "Z:\Results"

' Create filesystem object
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Quit if specified folder doesn't exist
If Not objFSO.FolderExists(strBaseDir & "\") Then 
   Wscript.Echo "Base folder [" & strBseDir & "] missing, ending."
   Wscript.Quit
End If

' Calculate yesterdays date, format as MM_DD_YYYY
dtmValue = DateAdd("d", -1, Now())
strDate = LPad(Month(dtmValue), 2, "0") & "-" & LPad(Day(dtmValue), 2, "0") & "-" & Year(dtmValue)

' Build name of yesterdays folder using date
strYesterdayFolder = strBaseDir & "\" & strDate

If Not objFSO.FolderExists(strYesterdayFolder & "\") Then 
   Wscript.Echo "Yesterday folder [" & strYesterdayFolder & "] missing, ending."
   Wscript.Quit
End If

' Access the folder
Set objFolder = objFSO.GetFolder(strYesterdayFolder)

' Process it if it has files to move
If objFolder.Files.Count > 0 Then
   ' Move all files
   objFSO.MoveFile strYesterdayFolder & "\*.*", strDestDir
End If

' Make sure it's now empty and delete
If objFolder.Files.Count = 0 Then
   ' Delete folder
   objFSO.DeleteFolder strYesterdayFolder
End If

' Done
Set objFSO = Nothing

' Left pad a string to any length with a specified character
Function LPad( strText, intLen, chrPad )
   LPad = Right( String( intLen, chrPad ) & strText, intLen )
End Function

Open in new window

~bp
0
 
JaysonJacksonAuthor Commented:
the attached is the scrip host message.   Missing ending.
missing.png
0
 
JaysonJacksonAuthor Commented:
the files to be moved are in
Z:\Results\Problems\2015_05_07
0
 
JaysonJacksonAuthor Commented:
Thanks Bill,  i changed that and all is good now. thanks again.
0
 
Bill PrewCommented:
Welcome, glad that was helpful.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.