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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Bill PrewIT / Software Engineering ConsultantCommented:
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
Bill PrewIT / Software Engineering ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JaysonJacksonAuthor Commented:
Thanks Bill,  i changed that and all is good now. thanks again.
0
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome, glad that was helpful.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.