prove your existence!
August 21, 2007 8:43 AM   Subscribe

Is there a simple way to create a script to check if a specific file exists on a Windows network share? How can I learn to do this?

I work with a company that produces data files for us, which we deliver to other companies as a data feed, delivered by FTP. I have access to the FTP server as a network share. This company produces maybe 10 files on a daily basis, created throughout the day, posted to different directories. The filenames are generally something like "yourfile.YYYYMMDD" with year, month, day represented in the extension. sometimes these files are late, and i get nasty emails from our clients telling me all about it.

i'd like to write a script that simply tells me whether these files are available yet, so I am in the loop before clients come calling. it would have to be date dependent, meaning the script would know to look for "yourfile.20070821" on August 21, 2007.

i am not looking for someone to code this for me in a nice tidy application or something - though if you really want to, i wouldn't object ;) - but really how to get the knowledge and what platform (java?, visual basic?, etc) to use. i have about zero programming experience, but if i knew what to research and how to generally go about doing something like this, i could probably do it.

i am fairly good with technical learning. free web-based tutorials would be much better than books, since my budget is limited.
posted by uaudio to Computers & Internet (9 answers total)
 
Almost any programming or scripting language will work, since any and all of them can check for the existence of a file. You might find visual basic the easiest, I don't know. I've done stuff like this more times I can can count in perl, tcl, basic, C, java, even plain shell scripts like ksh or bash...

You'd probably need to learn at least the rudiments of some kind of programming language... shell scripting is attractive because it's very simple.
posted by RustyBrooks at 9:34 AM on August 21, 2007 [1 favorite]


Best answer: If you don't want to become a programmer, I recommend AutoIt, since the language is extremely simple and you'll find dozens of other uses for it. AutoIt has a Visual Basic-like syntax, but it's geared towards automating Windows processes. It's free and fully documented online, with lots of tutorials.

What you want to write would be something like:

If FileExists("X:\yourfile." & @YEAR & @MON & @MDAY) Then
MsgBox(0, "File Available", "The yourfile file is available.")
EndIf

If you check out a few tutorials, you'll work out how to do more complex things, like checking for multiple files, running the program in a loop, and only alerting once (by setting a variable when you pop up a message box, then checking it.) You could work in a comparison where you would only be alerted if the file didn't exist and the time was too late (If NOT FileExists(...) AND @HOUR > 10 Then....)
posted by pocams at 9:45 AM on August 21, 2007 [1 favorite]


You can use a batch file. Put the following line into a blank file and save it as something.bat

Easiest one liner

@IF EXIST C:\file.txt ECHO "File Exists"


If you want to check over a network share use this as the filename:

\\networkcomputername\sharename\file.txt
posted by mphuie at 9:56 AM on August 21, 2007


Best answer: Echoing pocams: you'll want to learn some basic vbscript if you're principally dealing with Windows machines It's super-easy, nearly "plain-language" scripting, and there's a ton of pre-made common task scripts available. Check out the MSDN Script Repository for lots more info (in this case, in the section for "Storage").

If the tricky part is the date, then something like this would work. If you change the strFolder at the top to the base path network share, then every time it is run it'll walk through that share and its subdirectories, and echo to the console all the files that match the YYYYMMDD string, anywhere in that share tree. It should pretty much work as-is provided you set the strFolder path correctly in the top of the file. Just save as myscanner.vbs or something similar. You'll probably want to run at a command line (you only have to do this once on your machine) cscript //H:cscript //NOLOGO //S, so that your script host is console based

If you want to get more sophisticated, it can notify you in other ways (email, pop-up dialog box, etc), but that's something you can solve later.



'base directory to scan; make sure it ends in the \ symbol
strFolder = "\\server\share\folder\"

' create the timestamp string YYYYMMDD
currentTime = Now
myYear = Year( currentTime )
myMonth = Month( currentTime )
myDay = Day( currentTime )
' make sure that single digits are converted to the 2007 08 21 form
if len( myMonth ) = 1 then myMonth = "0" & myMonth
if len( myDay ) = 1 then myDay = "0" & myMonth
strTimeStamp = myYear & myMonth & myDay


' Create a file system object called FSO, so you can interact with the file system
set FSO = CreateObject("Scripting.FileSystemObject")

' Start a recursive scan of the folders starting with the base share strFolder
scanDir strFolder

' when done, quit
wscript.quit

' this function calls itself recursively, to walk a folder path
' it can be after the wscript.quit
Function scanDir( strPath )
 on error resume next
 set myFolder = FSO.GetFolder( strPath )
 set myFiles = myFolder.Files
 set myFolders = myFolder.SubFolders

' first check all the files to see if they match
 for each item in myFiles
  if Instr(1, item.Name, strTimeStamp, 1) > 0 then
   ' file has the current YYYYMMDD in it, so list it
   'instead of echo, we could do things like add to a list, etc, and email at the end
   wscript.echo item.Path
  end if
 next


' next, scan through every subfolder using this same function "scanDir"
 for each item in myFolders
  scanDir strPath & item.Name & "\"
 next
End Function

posted by hincandenza at 11:10 AM on August 21, 2007 [1 favorite]


Seconding the suggestion of VBScript for your particular needs. It's a lightweight scripting language that is fairly straightforward, and compatible with every modern version of Windows.
posted by Brak at 11:32 AM on August 21, 2007


hicandenza: I'm a little curious how you would handle sending email with VBScript. I'm guessing you could do it with an Outlook.Application object, but do you know an easier or better way?
posted by zixyer at 1:02 PM on August 21, 2007


You can make use of the CDO objects to send an email with VBScript. Take a look at this link for an example.
posted by Brak at 1:38 PM on August 21, 2007 [1 favorite]


Best answer: What Brak said- there's lots of samples out there, I like to have a re-usable function as below that I can just quickly copy/paste in my scripts, like so, so that I simply call sendmail( from, to, subject, body) to send an email from my script.


' ---------------------------------------------------------
Sub SendMail( strMailFrom, strMailTo, strMailSubject, strMailBody )
On Error Resume Next

Set oMail = CreateObject("CDO.Message")

oMail.From = strMailFrom
oMail.To = strMailTo
oMail.Subject = strMailSubject
oMail.Textbody = strMailBody

oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.100.100.100" ' use the IP of an internal SMTP relaying server
oMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
oMail.Configuration.Fields.Update

oMail.Send

If Err.Number <> 0 Then
' can do your own echoing/error handling
wscript.echo "Could not send email to " & strMailTo & ": " & Err.Number & "-" & Err.Description
End if

set oMail = nothing

End Sub
' -----------------------------------------------

posted by hincandenza at 5:04 PM on August 21, 2007


Batch files can do this. Check the GetDate.cmd link at the bottom of that page, and then try plugging %yy%%mm%%dd% into the IF EXIST line in mphuie's example.
posted by Myself at 9:52 PM on August 21, 2007


« Older How do you prefer to find out what's on?   |   From a guy's point of view Newer »
This thread is closed to new comments.