Solved

Help with sendkeys automated process

Posted on 2004-10-01
18
434 Views
Last Modified: 2012-08-14
Hello,

This is what needs to be done on a daily basis :

1) Copy X file to Y directory.
2) Compare X file in both directories.
3) Delete original file

Yes, a simple enough task to be done manually. Yet I am trying to automate it with the use of sendkeys.

I have two problems as of now.

#1

This is currently what I have :

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 1000
WshShell.SendKeys "copy \\path\path\path\current\*.zip \\path\path\path\Archive\
WScript.Sleep 5000

WshShell.SendKeys "fc \\path\path\path\current\*.zip \\path\path\path\Archive\
--- Here is my problem.

The name of the file changes everday ... example : name.1.20041001200005.

Thus, I can not do a fc to compare ...


#2
After completing a file compare successfully. I will have the outputted compare.txt sent locally and opened. Ensure that there are no differences, and delete the original.

My question is, how do I tell sendkeys, that once I close the notepad, to continue with the script - to delete the original file?

I know an easy solution would be to just simply create a new temporary directory, but I'd like to see if there is any other way.

Any help is much appreciated.

Thank you
0
Comment
Question by:MrBean
  • 9
  • 5
  • 2
18 Comments
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
An automatic file compare would be more efficient if you can predict the names each day using the date/time or what not, but i'm assuming that it does need to be done manually. Now, the best way to check if you have closed notepad would most probably be checking once its no longer the active window.

WScript.Sleep 250
Do Until Success = False
Success = objShell.AppActivate ("AR System User")
WScript.Sleep 250
Loop

However, if your doing other things, and changing the active window will doing the file compare, this might not be an option. In that case, using WMI to check running processes might be a better option.

set service = GetObject ("winmgmts:")

for each Process in Service.InstancesOf ("Win32_Process")
      If Process.Name = "notepad.exe" then
            wscript.echo "Notepad running"
            wscript.quit
      End If
next
wscript.echo "notepad not running"

You could just have this in a loop which breaks out when notepad is detected as not running. Although, the only issue with this is if you have several instances of notepad running on your machine, not involved in this comparison procedure, then they will also have to be closed down before it reports notepad as not running. Let me know how it goes.
0
 

Author Comment

by:MrBean
Comment Utility
Well ... the file name creation does follow a pattern ... so the name could be predicted. Yet, changing the .vbs file on a daily basis kind've makes the whole 'automated' process useless ...

And the rest of what you posted, on closing notepad and what not ... I'm really not following ...

Before we get into the notepad section, is there anyway around my first obstacle?

Thanks
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Sure thing, as for the first obstacle regarding file comparison, you could enumerate the files in the directory using FSO, and select the most recently created file (if that's what the file is), or, from what i can see, the filename is based on the date and time, so if you can predict the filename based on the system time, then you could use filecompare as you know the name each day. If you could give me details of exactly how the naming system works etc. i'll see what i can come up with.
0
 

Author Comment

by:MrBean
Comment Utility
Well bad news ...

The first part of the file is in date format : 20041002, but the remaining 6 characters are in number form, from a secondary company, that has no pattern. Its a system they use ... that we do not know their naming scheme.

Is it possible to use a wild card after the date? 20041002* for example? Any possibility of working at it like that?

I really appreciate your help on this

0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Alright, here is what i put together for you

CurrentDate = Date()
CurrentYear = Year(CurrentDate)

'process month to add a 0 on the front if before the 10th month
If Month(CurrentDate) < 10 Then
CurrentMonth = "0" & Month(CurrentDate)
Else
CurrentMonth = Month(CurrentDate)
End If

'process day to add a 0 on the front if before the 10th day
If Day(CurrentDate) < 10 Then
CurrentDay = "0" & Day(CurrentDate)
Else
CurrentDay = Day(CurrentDate)
End If

MsgBox(CurrentYear & CurrentMonth & CurrentDay)
'this should return the first part of your file name
'from here we should be able to use wildcards to get the correct file
dateString = CurrentYear & CurrentMonth & CurrentDay

Set fso = CreateObject("Scripting.FileSystemObject")
Set folderObject = fso.GetFolder("C:\")
Set filesObject = folderObject.Files

On Error Resume Next
For Each file In filesObject
'lists each file in the dir, and searches for you datestring in the filename
pos = InStr(file.Name,dateString)
If pos > 0 Then
'if it finds the datestring, returns the file name
correctFile = file.Name
End If
Next

'msgbox should contain the correct file name, and from there you should be able to do a file comparison
MsgBox(correctFile)

Set fileObject = Nothing
Set folderObject = Nothing
Set fso = Nothing
'cleans up file system objects


Hopefully this will get you on the right track, if you need any further help let me know but i'm pretty sure this should be all you need, and with this you shouldn't need to compare the two files manually :) Let me know how it goes

James
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
If you need some information on some of the commands used in the code in order to modify them, most of it can be found in these two places:

http://www.winguides.com/scripting/reference.php?category=23 - Date/Time in VBScript

http://www.sloppycode.net/Reference/FSO/?ref=132 - File System Object for manipulating files and folders, you can actually use this to copy and move your files, instead of sending keys to the command window, although what you have in place now works as it is now.
0
 

Author Comment

by:MrBean
Comment Utility
So Im kinda confused here ...

Where do I enter my area of what needs to be done?

Copy X file to Y Dir
Compare two files
Ensure they're the same
Remove original file
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Well, the above code i gave you just allows you to identify the exact filename each day, so just enter your code after all of that with the variable correctFile giving you the filename for that day.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:MrBean
Comment Utility
hmmm, I ran it ... all it does is give me a popup box with the date, then a blank popup box ...
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Its just a demonstration, you have to adjust

Set folderObject = fso.GetFolder("C:\")

change "C:\" to the folder where you have the file stored. And you can remove the msgboxes as i just put them in to demonstrate what it was doing. Ultimately, the only thing you want out of that script is the correctFile variable, which should represent the filename (e.g. name.1.20041001200005) that you wanted.

"The name of the file changes everday ... example : name.1.20041001200005.

Thus, I can not do a fc to compare ..."

The idea of the above code was allow you to get the filename so you can do a fc, just place the code before all your other commands and leave it there, then adjust your code to something like this

WshShell.SendKeys "fc \\path\path\path\current\*.zip \\path\path\path\Archive\"
becomes WshShell.SendKeys "fc \\path\path\path\current\" & correctFile & "\\path\path\path\Archive\"

Give that a go
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Correction:
WshShell.SendKeys "fc \\path\path\path\current\" & correctFile & "\\path\path\path\Archive\"

Should probably be:
WshShell.SendKeys "fc \\path\path\path\current\" & correctFile & "\\path\path\path\Archive\" & correctFile
0
 
LVL 5

Expert Comment

by:naiea1231
Comment Utility
Sorry about that
0
 
LVL 5

Expert Comment

by:kemp_a
Comment Utility
Perhaps this is making if too complicated.

Why not just use a batch file to complete the whole operation (including using fc!)

@echo off
copy \\path\path\path\current\*.zip \\path\path\path\Archive\
FOR /F  %%i IN (\\path\path\path\current\*.zip) DO "fc \\path\path\path\current\%%i \\path\path\path\Archive\%%i >\\path\path\path\fc.txt"
START /WAIT NOTEPAD "\\path\path\path\fc.txt"
...
whatever else you need to do
0
 

Author Comment

by:MrBean
Comment Utility
I thank you both for all your help ...

Yet ... I'm still stuck.

The fact that these files end with a 5-7 digit randomly generated number that I have no control over, is really stumping me ...
0
 
LVL 5

Accepted Solution

by:
naiea1231 earned 50 total points
Comment Utility
CurrentDate = Date()
CurrentYear = Year(CurrentDate)

'process month to add a 0 on the front if before the 10th month
If Month(CurrentDate) < 10 Then
CurrentMonth = "0" & Month(CurrentDate)
Else
CurrentMonth = Month(CurrentDate)
End If

'process day to add a 0 on the front if before the 10th day
If Day(CurrentDate) < 10 Then
CurrentDay = "0" & Day(CurrentDate)
Else
CurrentDay = Day(CurrentDate)
End If

'MsgBox(CurrentYear & CurrentMonth & CurrentDay)
'this should return the first part of your file name
'from here we should be able to use wildcards to get the correct file
dateString = CurrentYear & CurrentMonth & CurrentDay

Set fso = CreateObject("Scripting.FileSystemObject")
Set folderObject = fso.GetFolder("\\path\path\path\current\")
Set filesObject = folderObject.Files

On Error Resume Next
For Each file In filesObject
'lists each file in the dir, and searches for you datestring in the filename
pos = InStr(file.Name,dateString)
If pos > 0 Then
'if it finds the datestring, returns the file name
correctFile = file.Name
End If
Next

'msgbox should contain the correct file name, and from there you should be able to do a file comparison
MsgBox(correctFile)

Set fileObject = Nothing
Set folderObject = Nothing
Set fso = Nothing

set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd"
WScript.Sleep 1000
WshShell.SendKeys "copy \\path\path\path\current\*.zip \\path\path\path\Archive\
WScript.Sleep 5000
WshShell.SendKeys "fc \\path\path\path\current\" & correctFile & "\\path\path\path\Archive\" & correctFile


Just give the above a go, the whole reason for the part of the script i wrote before was to allow you to get the filename, what it does is generate today's date and then find the file with that in it, giving you the filename of the current zip, including the 5-7 digit random number, allowing you to do a filecompare (see above). You really need to look at what the code is doing here and work with it.
0
 
LVL 5

Assisted Solution

by:kemp_a
kemp_a earned 50 total points
Comment Utility
Hey mrbean!

The reason I suggested a simple batch file was because your opening a command window and then sending keystrokes to it, effectively creating an batch file interactively.

The full batch file, call it ArchiveDailyZip.Bat should look more like this:
@echo off
xcopy \\path\path\path\current\*.zip \\path\path\path\Archive\ /V /Q /R /-Y /Z
FOR /F  %%i IN (\\path\path\path\current\*.zip) DO "fc \\path\path\path\current\%%i \\path\path\path\Archive\%%i >\\path\path\path\fc.txt"
START /WAIT NOTEPAD "\\path\path\path\fc.txt"
SET /P FileDeleteOK OK to Continue [Y/N] ?
IF NOT /I %FileDeleteOK% NEQ y GOTO NODELETE
DEL "\\path\path\path\current\*.zip" /F /Q
GOTO FINISH
:NODELETE
ECHO File Not Deleted, Please correct and retry process
:FINISH

Then when you want to perform this operation, just use ArchiveDailyZip.Bat as your command.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

763 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now