# VB Script to Run a Batch File and Utilize a Text File



## ncktravis (Aug 10, 2012)

Backround: I am a college student majoring in Information Technology with a focus in programming. I also work for my school's IT department as an IT associate. I have 4 years of experience dealing with hardware and software and have studied Net+ and A+ certification books.

Problem: At my school my boss, the IT Director, wants to schedule a batch file to run weekly that will delete items from the desktop and folders of the "Student" account on all of the student computers. He asked us associates to go around to every student computer with this batch file on a thumb drive and schedule the task. Seems simple enough right? The problem is we are talking upwards of 300 computers throughout the campus. I proposed a solution to him where we would use a script that would essentially consult a text document with the names of all of the student computers in it, then run the file.

I am 99% this can be done, I just have a very small amount of programming experience. If anyone has an answer or knows of an easier way to accomplish this I appreciate it. Thanks in advance. 

EDIT: All student workstations are running Windows XP SP2


----------



## Ent (Apr 11, 2009)

By the sound of it it could be done; the question is whether it's the best way of doing it.
1: How do you propose to get these computers to execute the VBS? Taking it round on a flash drive isn't a great improvement .


Other questions about your scheme: 
Why use both batch and VBS instead of just one or the other? (worst case scenario VBS can simply execute each of the shell commands in the batch file.)
How involved do you want to be in writing the script?


----------



## ncktravis (Aug 10, 2012)

I'm almost certain there is an automated way to get this job done, like you said perhaps using a batch file or script isn't the best way and Bill Gates has an easier way to do it built into MS Server. 

I've been searching through countless web sites and trying to research and the conclusion I came to is a VBS may be the least painstaking way of going about this. I actually just recently came upon a thread on another site where someone was looking to shutdown every computer on their domain with a few clicks using a shutdown batch. Similar situation, just a different end result. We already have the simple batch file, I wasn't sure if it could just be edited to run on all of the workstations named in a text file or if I would have to use a VBS for that. As far as how involved I'd like to get in writing this script, I've done simple programming, and this is a simple objective. So I'm hoping it won't have to be too involved, but I'm not afraid to get my hands dirty so to speak.


----------



## ckphilli (Apr 29, 2006)

If all user accounts are named 'student' and all are XP...why not write the bat to execute from any machine on the network with proper credentials...like so (as an example)...

del \\computer1\c$\documents and settings\student\desktop\*.*
del \\computer2\c$\documents and settings\student\desktop\*.*
del \\computer3\c$\documents and settings\student\desktop\*.*

...

And then schedule it.

Of course this assumes that the 300 are on the same network and that you have the proper credentials. Obviously, you'll want to test...


----------



## Ent (Apr 11, 2009)

It's impossible to give specific advice without knowing how the network is actually set up. 
I would imagine that students are in a specific group for security policies et cetera, and I would therefore imagine that you could ignore the whole text file stage and just put some extra lines on the end of the logon script that I imagine members of said group already run. 

Incidentally, I love writing scripts and programs, but in the real world there can be a time when it's genuinely not worth it. In terms of time alone the time taken to design, test, and deploy an automated solution could be longer than getting a bunch of guys to wander round the school with USB sticks. In terms of experience writing software would do you a lot more good than plugging in flash drives, which is why it probably is in your interest to get your hands dirty.


----------



## ncktravis (Aug 10, 2012)

@Chris I think that should do it, I didn't even think of that. Of course it will have to be run with administrative credentials, which I have. Every computer is connected to the same network, and doing it that way I can easily exclude staff computers.

@Josiah That is very true, programming is definitely what my concentration is, and I wouldn't mind, in fact would love to get my hands dirty. I knew it could be automated in some way, somehow.

As far as I'm concerned I think I've got my solution. Time to go test it out.

Cheers


----------



## Ent (Apr 11, 2009)

Remember that you have to test your solution in all the situations it could encounter. 
The last thing you want is that it fails on one line (because a computer has been shut down for example) and then exits instead of doing the rest of the script.

Also, use a loop.


----------



## ncktravis (Aug 10, 2012)

Right, I could set it to ping each target computer, and if found online it would then delete the files, if not, well then it wouldn't?

Or would a computer being offline even have an effect on the process? Other than the fact that that computer or computers wouldn't have their items being deleted?


----------



## Ent (Apr 11, 2009)

That's one good check. You really do have to think about what kinds of situations you'll encounter.
Another possible problem I'd foresee is the possibility that a file is still in use and/or locked.


----------



## ckphilli (Apr 29, 2006)

Yes! Error handling is a must. I wouldn't ping though, just write something for if the path is not found. Also, I would log what's being deleted for trends analysis (security, productivity, etc). Don't forget to have fun also.


----------



## ncktravis (Aug 10, 2012)

That is also true, it'll definitely be an all day affair if not longer. But once completed, and tested/tweaked to function properly it will be simple to add/del computers from the bat. For example if the school receives a new shipment of workstations it is much faster to edit a file than to individually schedule a task.


----------



## ncktravis (Aug 10, 2012)

@Chris Could I get an example of what that would look like including the loop, etc?


----------



## ckphilli (Apr 29, 2006)

I'm actually on my phone waiting for a meeting, sorry. But if you haven't gotten it by tomorrow or if Ent doesn't do it I'll put something up for you.


----------



## ncktravis (Aug 10, 2012)

I'm attempting to work this out myself, but if I had something to check against it would make my night a tad easier.


----------



## ncktravis (Aug 10, 2012)

Not a problem, I'm going to see what I can do. Here's to a night of trial and error.


----------



## Ent (Apr 11, 2009)

For once I'm not going to write it, but I will give you a few pointers.

You're using Batch, and I'm assuming that you're going to go with Chris's suggestion involving executing the command from a single machine.

Batch is *very* good at running through a list stored in a different file; less so if it's stored in the same file. To loop through every line in a file in sequence, look up the FOR command with the /F switch.

Because you'll want to work with changing variables inside this loop, you'll probably want to use delayed expansion.

Logging information could be as simple as appending to a text file. 
To append to a file, use two > signs. (ECHO foo > Foo.txt replaces Foo.txt and then puts "foo" in it, which would wipe your log. ECHO foo >> Foo.txt just sticks the text "foo" at the end of it.)
You can use IF to decide whether information deserves to be logged.

I'm not sure about checking whether a path works. You'll have to experiment with that one, but I suspect it's as likely to hang your program as to give you something useful. 
Personally I'd stick with pinging initially. Look into the command line options; there should be no reason that you have to wait for 4 responses and doing so would slow your script to a crawl. Perhaps once you know that the computer is up a file check would be more reliable, but I'm not sure it would serve any useful purpose.

Finally, you can use %ERRORLEVEL% to determine whether the previous command was successful.
For example a successful command normally gives an errorlevel of 0, If DEL receives an ACCESS denied it sets the ERRORLEVEL to 18.

Come back if you get stuck.


----------



## ncktravis (Aug 10, 2012)

So I spent some time working on trial and error, finally got this. Everything works with the exception of the if statement. It appends Computer1:Failed each time, even if nothing exists in the directory d:\folder. I think I'm missing something here. (This is of course just a small scale batch to test the logging and see if I did everything right)

Also as far as the For /f , that is exactly what I was looking for. I just wasn't sure how to word it. 

@echo off
del /q d:\folder\?.?
if exist d:\folder\?.? 
( echo Computer1:Failed >> d:\logs\log.txt
) Else
( echo Computer1:Complete >> d:\logs.txt
exit


----------



## ncktravis (Aug 10, 2012)

Here we are

@echo off
del /q d:\folder\?.?
if %errorlevel% equ 0 (
echo Computer1:Complete >> d:\logs\log.txt
) Else (
echo Computer1:Failed >> d:\logs\log.txt
)
exit

Using %errorlevel% I finally got the output I wanted. Again just a small scale, but definitely something to work off of for the large scale. I'm looking into For with the /f switch, and assuming all goes well I'll be ready to test it.


----------



## ncktravis (Aug 10, 2012)

@echo off
For /F "tokens=1,2,3,4 delims=," %%G IN (four.txt) DO Del /q %%G 
if %errorlevel% equ 0 (
echo Purge Complete >> d:\logs\log.txt
) Else (
echo Purge Failed >> d:\logs\log.txt
)
pause
exit

Gentlemen, please be informed that there is a Santa Claus.

Four different folders, essentially representing 4 different computers, all of which had their files deleted in one click. Now to decide whether I am going to log for each computer and how that would be done, or if I will just leave it as it is.


----------



## ckphilli (Apr 29, 2006)

ncktravis said:


> Now to decide whether I am going to log for each computer and how that would be done, or if I will just leave it as it is.


I'm happy for you! :up:

As far as the logging goes...see if your people want it. They may not care...personally, I would. I would want to see types of files being saved for more than a few reasons, and not all negative. Also, you may want to do the logging piece for your own knowledge.

Have a great night-


----------



## Ent (Apr 11, 2009)

In terms of checking for problems, it's normally better to identify the likely problems first than to just see whether it worked afterwards. Think of a lion hunter; either he can make sure that he's loaded his gun and it's pointed at the lion before he fires, or he could shoot blindly and then check whether there's a dead lion in front of him. The former is probably safer. 

Aslo, remember that you can expand the for across multiple lines

For example

```
For /F "tokens=1,2,3,4 delims=," %%G IN (four.txt) DO (
Del /q %%G 
if %errorlevel% equ 0 (
     echo Purge Complete >> d:\logs\log.txt
    ) Else (
     echo Purge Failed >> d:\logs\log.txt
    )
)
```


----------

