...

View Full Version : Can i use WSH to execute all .exe files of a directory?



raf
10-28-2004, 11:11 AM
i need to do some clientside work on a windowsmachine, that basivcally involves:

- starting a script each month (--> sheduled task, no problem);
- the script needs to create a new directory, that contains the current year as subdirectory, and the current month as subdirectory of te yeardirectory. (example k:\...\outputs\y_2004\m_01\ ) (--> with WSH, using the FSO,Create Folder)
- the script needs to execute all executables (40 or so) that are in a specific directory (--> How ? I can loop through the directory and check the fileextension, but how do i actually run the executables?)
- the outputfiles that are create by the executables need to be moved to the new directory. (-->with WSH, using the FSO, MoveFile)

I am thinking to do this with WSH (VBscript) but i'm not sure if that realy is the way to go. I am also a bit unclear how i can execute the found .exe files + if and how long the script then waits until the file is executed (can take several minutes)

Anyone done such tasks or has experience with WSH?

codegoboom
10-28-2004, 02:40 PM
Points to ponder...


Running Programs From WSH Scripts (Run vs. Exec methods) (http://www.microsoft.com/technet/community/columns/scripts/sg1002.mspx)

A Brief Introduction to WMI Events (http://www.microsoft.com/technet/community/columns/scripts/sg0103.mspx)

Scripting Guide: Processes (WMI) (http://www.microsoft.com/resources/documentation/windows/2000/server/scriptguide/en-us/sas_prc_overview.mspx)

Scripting API for WMI (SDK) (http://msdn.microsoft.com/library/en-us/wmisdk/wmi/scripting_api_for_wmi.asp)

raf
10-28-2004, 03:43 PM
Ha, thanks. That first link is exactly what i needed.

Looks like WSH will be the way to go. (Unless someone knows a better alternative ...)

Roy Sinclair
10-28-2004, 05:45 PM
If the machine has the .NET framework loaded (and most do these days) you can write a full .NET program. Maybe not necessary but you should keep it in mind as an option.

aman
10-28-2004, 09:58 PM
You could also use a good old-fashioned batch file to do a job like that. http://www.robvanderwoude.com/

raf
10-29-2004, 04:07 PM
If the machine has the .NET framework loaded (and most do these days) you can write a full .NET program. Maybe not necessary but you should keep it in mind as an option.
That could be an option, but like you said, it's kinda overkill for this rather simple task + i'm not realy planning to do much .NET work in the immediate future so i'm not realy keen on investing time in it now.
WSH with VBscript is then kinda like the Classical ASP, where i'm quite experience with.

You could also use a good old-fashioned batch file to do a job like that. http://www.robvanderwoude.com/
Hmm.
That was my first idea, but i don't know how to:
- use the date (or just the current year) as name of a directory;
- create a loop to run some commands for each .exe file in a specific directory
and i nowhere found info on these two (i also didn't see it in the link you posted, so maybe it can't be done ...)

Roelf
10-29-2004, 05:26 PM
but i don't know how to:
- use the date (or just the current year) as name of a directory;

the next command in a cmd file gives you the year as a variable, so use md to make a forlder with this name:

For /f "tokens=3 delims=-" %%a in ('Date/t') do set CURYEAR=%%a
perhaps you have to set the "3" inhere to another number to get the right part from the string which the Date command returns

very useful for other purposes too :D

raf
10-29-2004, 08:48 PM
Hey, thanks for that.

So this means i'd only need to be able to
"create a loop to run some commands for each .exe file in a specific directory"

So some sort of find that returns a collection i can loop through
:confused:

Roelf
10-29-2004, 09:12 PM
So this means i'd only need to be able to
"create a loop to run some commands for each .exe file in a specific directory"

So some sort of find that returns a collection i can loop through
:confused:
that is easy, run this command in a cmd file in the folder where the exe's are:

for %%a in (*.exe) do %%a
or even from another folder:

for %%a in (dir thepathtoyourdir\*.exe) do %%a

a bit more info from the date than previous post:


For /f "tokens=1 delims=- " %%a in ('Date/t') do set CURDAY=%%a
For /f "tokens=2 delims=- " %%a in ('Date/t') do set CURDATE=%%a
For /f "tokens=3 delims=- " %%a in ('Date/t') do set CURMONTH=%%a
For /f "tokens=4 delims=- " %%a in ('Date/t') do set CURYEAR=%%a


gives all date components (day, date, month, year) in variables, ready for use as you wish

some explanation:
the date/t command returns a string, with the /f combined with tokens and delim it is splitted based on the delim characters (in this case dash and space) the index number after token, says which part to take and put it in the variable

raf
10-29-2004, 10:07 PM
that is great. I never knew you could do that in a batchfile.
it's actually gonna be real easy to do it like that.


Thanks all ! :thumbsup:

Roy Sinclair
10-29-2004, 10:29 PM
Just a quick note: those more powerful BAT file capabilities are not available on Windows 9x or ME machines. They came from the Windows NT side of the house so think in terms of NT, 2K, XP and Server 2003 as the platforms where that will work. But it's cool that Roelf added them to the discussion :thumbsup: since I wrote and sold an article about them some time around 1996 to a magazine in the UK.

raf
10-29-2004, 11:27 PM
Just a quick note: those more powerful BAT file capabilities are not available on Windows 9x or ME machines. They came from the Windows NT side of the house so think in terms of NT, 2K, XP and Server 2003 as the platforms where that will work.
I see. That's why i never seen it (stopped writing batchfiles a while ago). The script will run on NT so i should be ok there.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum