• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 942
  • Last Modified:

Transfering files based on Date stamp using VB script

Hello Experts,

With the help of some of you, I was able to create exactly what I needed.  But like always, they have decided to change the way it is written.  I need a VB script that will check to see if the person is apart of a group membership and if they are the program will look at a file that will be manualy updated or it could write a file with the date somewhere.  If the date is more current then it will prompt the user to enter the usb key and hit okay, search for a removeable usb drive  and copy all the files that are more current from the hard coded directory to the root of the usb device.  The code is the snippet box is my original code.  I have created some new code for this project but I do require some help.

If (IsMember(objUser, "TEST") = True) Then
@REM This batch file will find the removable drive letter.  It will then copy a file from the specified path and put it on the root of the removable drive.
@REM The file is update to the removable drive by date.
 
@echo off
 
 
setlocal
 
for /f "tokens=1-3" %%a in ('wmic logicaldisk get caption^, description') do if "%%b %%c"=="Removable Disk" set drive=%%a&goto FOUND
 
echo Couldn't detect removable drive
goto :EOF
 
:FOUND
 
echo Removable drive letter is %drive%
 
:continue
 
xcopy "X:\Information Technology\test\*.*" "%drive%\" /C/E/H/R/K/D/M/Y
 
:end

Open in new window

0
dbrs_helpdesk
Asked:
dbrs_helpdesk
  • 7
  • 6
1 Solution
 
dbrs_helpdeskAuthor Commented:
This is what I have come up with so far


If (IsMember(objUser, "TEST") = True) Then

Check to see if any of the files in "\\bluewater\departments$\test\ "are more current (It needs to be checked via date stamp)

If it is then prompt the user to click okay to continue

If not then end

Search for all drives to determine what the removeable usb drive is

if it is not there then end

if it is there then

objFilesystem.CopyFile "\\bluewater\departments$\test\*.*", "%drive%\"

end
0
 
sr75Commented:
I had a script that allowed me to copy files from a network share to a bunch of pen drives.  It didn't look at any specific drive letter, but parsed the DriveType property from the Win32_LogicalDisk WMI class.  Below is a sample of some of what I wrote (I don't have a copy of the original script on me at the moment, sorry).
On Error Resume Next
 
strComputer = "."
 
 
arrDriveType = array("Unknown",_
		     "No Root Directory",_
		     "Removable Disk",_
		     "Local Disk",_
		     "Network Drive",_
		     "Compact Disk",_
		     "RAM Disk")
 
set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")
 
For Each Disk in ColDisks
	strType = arrDriveType(Disk.DriveType)
	strID = Disk.DeviceID
	strSys = Disk.SystemName
 
	If strType = "Removable Disk" then
 
              ######  File Transfer Code here ######
 
	End If
Next

Open in new window

0
 
dbrs_helpdeskAuthor Commented:
I am not trying to use a specific drive letter because if you insert a pen drive it could be drive letter e,f,g,h etc....  I am not strong in VB scripting, but this code you posted seems to find the pen drive
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
dbrs_helpdeskAuthor Commented:
This is what I have so far.  

 
If (IsMember(objUser, "TEST") = True) Then
 
Check to see if any of the files in "\\bluewater\departments$\test\ "are more current (It needs to be checked via date stamp)
 
If it is then prompt the user to click okay to continue
 
If not then end
 
********************************   
 
On Error Resume Next
 
strComputer = "."
 
 
arrDriveType = array("Unknown",_
		     "No Root Directory",_
		     "Removable Disk",_
		     "Local Disk",_
		     "Network Drive",_
		     "Compact Disk",_
		     "RAM Disk")
 
set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")
 
For Each Disk in ColDisks
	strType = arrDriveType(Disk.DriveType)
	strID = Disk.DeviceID
	strSys = Disk.SystemName
 
	If strType = "Removable Disk" then
 
              objFilesystem.CopyFile "\\bluewater\departments$\test\*.*", "%drive%\"
 
	End If
Next
 
(Search for all drives to determine what the removeable usb drive is)
 
(if it is not there then end)
 
***********************************************************
 
if it is there then 
 
 
 
end 

Open in new window

0
 
sr75Commented:
Yes, it finds all the "removable disks" and loops through each one.  The [ strID = Disk.DeviceID ] line actually gives you the drive letter so you can just use:

strID & "\"

instead of "%drive%\"
 
0
 
dbrs_helpdeskAuthor Commented:
I have attached the code below.  How do I get it to check if the file has been updated on the network share?

If (IsMember(objUser, "TEST") = True) Then
 
Check to see if any of the files in "\\bluewater\departments$\test\ "are more current (It needs to be checked via date stamp)
 
If it is then prompt the user to click okay to continue
 
If not then end
 
********************************   
 
On Error Resume Next
 
strComputer = "."
 
 
arrDriveType = array("Unknown",_
		     "No Root Directory",_
		     "Removable Disk",_
		     "Local Disk",_
		     "Network Drive",_
		     "Compact Disk",_
		     "RAM Disk")
 
set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")
 
For Each Disk in ColDisks
	strType = arrDriveType(Disk.DriveType)
	strID = Disk.DeviceID
	strSys = Disk.SystemName
 
	If strType = "Removable Disk" then
 
              objFilesystem.CopyFile "\\bluewater\departments$\test\*.*", strID & "\" 
 
	End If
Next
 
(Search for all drives to determine what the removeable usb drive is)
 
(if it is not there then end)
 
***********************************************************
 
 
 
end 

Open in new window

0
 
sr75Commented:
you could write a function that gets the creation (or modified) date of each file and compares them using the datediff command.  I cannot remember how to get the creation (or modified) date of a file, but I know it is possible as I have done so in the past.
0
 
dbrs_helpdeskAuthor Commented:
Alright this is what I have but I need to do a few things.  I need it to check if the files in the location are newer by date stamp.  If it is not more current then end.  If it is more current  then I need a prompt to come up saying click okay to continue.  After the prompt then it checks for the usb key.  If the key is not there then end the program.  If it is there then copy the files over.  Just need a bit more help with this.
If (IsMember(objUser, "TEST") = True) Then
 
function getParent()
	fPath="\\bluewater\departments$\test\"
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	Set oFolder = oFSO.GetFolder(fPath)
	set oSubfolders = oFolder.Subfolders
	
	for each oSubfolders in oSubfolders
		filePath=GetNewestFile(oSubfolders)
		if(instr(filePath, sToday)) then
			fileName = re.replace(GetFilenameFromPath(filePath),"")
			oFSO.CopyFile filePath , "\\bluewater\departments$\test\" & fileName , OverwriteExisting
		else
			 Set objTextFile = oFSO.OpenTextFile _
(fPath & "exception" & date() & ".txt", ForAppending, True)
objTextFile.WriteLine("output data")
objTextFile.Close
		end if
	next 
end function
 
On Error Resume Next
 
strComputer = "."
 
 
arrDriveType = array("Unknown",_
		     "No Root Directory",_
		     "Removable Disk",_
		     "Local Disk",_
		     "Network Drive",_
		     "Compact Disk",_
		     "RAM Disk")
 
set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")
 
For Each Disk in ColDisks
	strType = arrDriveType(Disk.DriveType)
	strID = Disk.DeviceID
	strSys = Disk.SystemName
 
	If strType = "Removable Disk" then
 
              objFilesystem.CopyFile "\\bluewater\departments$\test\*.*", strID & "\" 
 
	End If
 
 
 
 
end 

Open in new window

0
 
sr75Commented:
Here is a website that will explain how to use a MessageBox (i.e. User prompt) and how to do something with the results:

http://www.computerperformance.co.uk/ezine/ezine46.htm

Here is a website detailing how to use the MsgBox different options:

http://www.devguru.com/technologies/vbscript/13955.asp
0
 
dbrs_helpdeskAuthor Commented:
how would I get this section of code to work?

Option Explicit
'Dim objWMIService, objItem, colItems
Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\")
 
 
'****************** check to see if the files are more current ************************************************ 
 
' For Each file in objFolderA.Files
' Check for reg key "here"
' if no reg key found on computer then continue with script
' if reg key is more current then file then
' WScript.Quit  
 
 
 
'**************************************************************************************************************

Open in new window

0
 
dbrs_helpdeskAuthor Commented:
I have almost finished this code but I need a bit of help to touch it up.  Only two sections need to be refined.  The first is the checking section.  This is what it is suppose to do.

If there is a reg key with a date stamp and if the files in objectfolderA date stamp are the same then end script other wise continue.

The second issue is that I get an error on

line 82
Char 1
Error name redefined: hkey_local_machine
Option Explicit
Dim strComputer, strMbox, ArrDriveType, WMI, coldisks, disk, strtype, strID, strSys, UsbKey, file, USBFound, varToday, Verify, LastRunDate, WshShell
Dim objReg, keypath, return, NextSection, oFile, oLastFile, dteFileDate, strFileDate
Dim objFSO:Set objFSO=CreateObject("Scripting.FileSystemObject")
Dim objFolderA: Set objFolderA=objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\")
Set WshShell = WScript.CreateObject("WScript.Shell")
 
Set WshShell = CreateObject("Wscript.Shell")
 
varToday = Weekday(Date)
 
Verify = "HKEY_LOCAL_MACHINE\SOFTWARE\EMERGENCY PLANNING\"
 
On Error Resume Next
LastRunDate = WshShell.RegRead(Verify & "File\")
On Error GoTo 0
 
For Each oFile In objFSO.GetFolder("\\bluewater\departments$\Information Technology\test\").files
Set oLastFile = oFile
Next
 
dteFileDate = oLastFile.DateLastAccessed
 
strFileDate = Month(dteFileDate) & "/" & Day(dteFileDate) & "/" & Year(dteFileDate)
 
WScript.Echo "I am comparing " & LastRunDate & " TO " & strFileDate
 
If strFileDate = LastRunDate  AND LastRunDate <> "NONE" Then
WScript.Quit
End If 
 
strMbox = "."
 
strMbox = msgBox("Please insert your Emergency Planning usb key and press the enter key to continue")
 
If strMbox = 1 Then
 
  strmbox ="."
 
end if
 
strComputer = "."
 
 
arrDriveType = array("Unknown",_
          "No Root Directory",_
          "Removable Disk",_
          "Local Disk",_
          "Network Drive",_
          "Compact Disk",_
          "RAM Disk")
 
set WMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colDisks = WMI.ExecQuery("Select * from Win32_LogicalDisk")
 
For Each Disk in ColDisks
 
    strType = arrDriveType(Disk.DriveType)
    strID = Disk.DeviceID
    strSys = Disk.SystemName
 
    If strType = "Removable Disk" then
 
          'strMbox = MsgBox("USB drive letter is " & Disk.DeviceID)
          UsbKey = strId & "\"
          USBFound = True
    End If
 
Next
 
If USBFound = False Then
    MsgBox "No USB drive was found."
    WScript.Quit
End If
 
For Each file in objFolderA.Files
  'If DateDiff("n", file.DateLastModified, Now) =< 60 Then
         objFSO.CopyFile file.Path, UsbKey
  'End If
 
 
const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\"&_
strComputer & "\root\default:StdRegProv")
KeyPath = "Software\Emergency Planning\File"
Return = objReg.CreateKey(HKEY_LOCAL_MACHINE, KeyPath)
'Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.RegWrite "HKLM\Software\Emergency Planning\File\", date()
 
Next
 
MsgBox "All files have sucessfully copied"
 
Set objFSO=Nothing 

Open in new window

0
 
sr75Commented:
You are setting the constant inside of a FOR loop.  Put it at the top of the script with all your DIMs
0
 
sr75Commented:
you should use the DATEDIFF method to compare the two dates.  Then if datediff <> "0" do something.

Attached is a sample of how to use it for your needs
DiffDate = DateDiff("d",dteFileDate,LastRunDate)
 
If DiffDate <> 0 then
	DO SOMETHING
Else 
	DO SOMETHING ELSE
End IF

Open in new window

0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now