1.

Solve : Newbie BAT file problem?

Answer»

Hi everyone,
I could use some pointers with an issue I have..

I am trying to do the following (I believe my logic is correct)...

1.) Create a BAT file to run during Windows Startup

2.) Within the BAT... Search a directory for a file (The file name is not the same every time - for example, Background1-002, Background1-628)

3.) If the file contains text 'ABC', reboot a Windows service. If not, do nothing.

I have look AROUND on this excellent site and found some GREAT resources to do the things I would like to do. I do have a few questions...

How do I search a file directory to find a specific file name that constantly changes (Step 2)? Can I use some sort of wildcard search since the beginning of the filename will constantly be the same? Sorry, my boss dumped this on me and I only have limited programming experience.

THANK you everyone for your help in advance!


HelloC:\>type findstring.bat
Code: [Select]@echo off

cd \

findstr /s "ABC" Background1*


findstr /s /m "ABC" Background1* > foundfile.txt


for /f "delims==" %%a in (foundfile.txt) do (

echo %%a
REM NET START "Error Reporting Service"


)
pauseREM the pause is necessary when run at startup time.


C:\>findstring.bat
GROUND\Background1-002:ABC
ground\Background1-003:ABC
ground\Background1-010:ABC
ground\Background1-002
ground\Background1-003
ground\Background1-010
Press any key to continue . . .

Output at Startup Time:

HelloThanks Bill. Could you help explain some of the code for me...

I have a basic idea, but I am new to this...

Code: [Select]@echo off

findstr /s "ABC" Background1*

findstr /s /m "ABC" Background1* > foundfile.txt


for /f "delims==" %%a in (foundfile.txt) do (

echo %%a
REM NET START "Error Reporting Service"

)
So, I am going to do a FindString in the Background.log file looking for ABC. If ABC appears, send it to the foundfile.txt. Then there is a loop that will start the services for each instance that comes back with ABC?

My boss actually MADE life a little easier for me. I can rename or move all of the older log files, so that the script will just search the folder for 1 .log file... would this code still provide the same results?

Thanks again for your help with this."So, I am going to do a FindString in the Background.log file looking for ABC. If ABC appears, send it to the foundfile.txt. Then there is a loop that will start the services for each instance that comes back with ABC?"

Yes, that is how it works. I did not test the start service line. I was not sure which service you planned to start. A REM ( remark ) at the beginning of the line will not allow the code to execute. As it is written the start service will execute each time ABC is found. ( might not harm ... a message will appear that it is already running )

The script will run the same if there is one or several "ABC" strings.

Good LuckHow do I specify the folder to do the Findstr command in? The files are only located in 1 folder, so it would make life a little easier to just have the batch file look in 1 location.. cd to the folder where the file is stored and remove the /s option from the findstr command. cd means change directory. A directory is the same as folder in windows.

For example:

C:\>cd ground

C:\ground>cd
C:\ground

C:\ground>findstr "ABC" background1*
Background1-002:ABC
Background1-003:ABC
Background1-010:ABC

C:\ground>


C:\>help cd
Hi Bill. Thank you for all your help. This will help me get started on solving the issue at hand.
The following findstr from the root directory will find the file( Background1*) on any machine.


C:\>cd \

C:\>findstr /s /m "ABC" Background1* > foundfile.txt

C:\>

I have no idea where the Background1* files are stored on your machine.

Quote from: seekupdmb5 on October 14, 2009, 07:32:58 AM


I am trying to do the following (I believe my logic is correct)...

1.) Create a BAT file to run during Windows Startup


To have a program run at startup time, the program must be placed in the startup folder:

C:\>cd %homepath%\Start Menu\Programs\startup

copy c:\findstring.bat .

( The "." above says copy to current directory. )

http://www.microsoft.com/windowsxp/using/setup/hwandprograms/autorun.mspx


To configure a program to start automatically

1.
Right-click Start, and then click Explore. Windows Explorer opens and displays your Start menu.



2.
In the left pane, click Programs. Browse to find the program you want to automatically open. In the right pane, right-click the program, and then click Copy.



3.
In the left pane, click Programs, right-click Startup, and then click Paste.



4.
Repeat steps 2 and 3 to add more programs to your Startup group.


The programs you add to the Startup group will start automatically the next time you log on to your computer.


Top of page
I tested the start service with the net command. It worked as expected. The first loop said we were able to start the service. The next two loops said the service was already working.


C:\>type findstring.bat
Code: [Select]@echo off

cd \

findstr /s "ABC" Background1*


findstr /s /m "ABC" Background1* > foundfile.txt


for /f "delims==" %%a in (foundfile.txt) do (

echo %%a
NET START "Error Reporting Service"


)
pause
REM the pause is necessary when run at startup time.


OUTPUT:

C:\>findstring.bat
ground\Background1-002:ABC
ground\Background1-003:ABC
ground\Background1-010:ABC
ground\Background1-002
The Error Reporting Service service is starting.
The Error Reporting Service service was started successfully.

ground\Background1-003
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

ground\Background1-010
The requested service has already been started.

More help is available by typing NET HELPMSG 2182.

Press any key to continue . . .


Discussion

No Comment Found