Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 91
  • Last Modified:

Need a VB script that will create a folder and move files.

I am looking for some assistance with a VBS script.

The script will be scheduled to run nightly and do the following

Create a subfolder with yesterdays date (example today is 11_23_2014 the folder created would be 11_22_2014)

The move all files from c:\scan\ to the Newly created folder


This does not sound hard but it is kicking my rear.
0
JaysonJackson
Asked:
JaysonJackson
  • 3
  • 2
1 Solution
 
Bill PrewCommented:
Under what folder would you like the dated daily folders created?

I assume there are only files and not subfolders to move from C:\SCAN?

~bp
0
 
JaysonJacksonAuthor Commented:
yes c:\scan
0
 
Bill PrewCommented:
Give this a try and see if it is acceptable, it should do what you described.

' Specify folder to work in
strBaseDir = "C:\Scan"

' 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 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)

' Create destination folder with yesterdays date
strDestDir = strBaseDir & "\" & strDate
objFSO.CreateFolder strDestDir

' Move all files in base directory to dated folder
objFSO.MoveFile strBaseDir & "\*.*", strDestDir

' 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:
Thanks so much Bill!!   this worked
0
 
Bill PrewCommented:
Great, glad that was useful.

~bp
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now