PDA

View Full Version : Batch Script Help. - I need a delay



[XC] riptide
02-07-2009, 08:06 AM
Ok so here it is...


:TOP
ping 127.0.0.1 -n 20
findstr /m "error" C:\CSW\RC572\dnetc.txt
if %errorlevel%==0 (
C:\CSW\RC572\dnetc.com -restart
)
GOTO TOP

Please don't laugh ... :D but basically I want the above to poll a log file "dnetc.txt" and search for the string "error". If it finds it, it starts command "dnetc.com -restart". And it goes back to polling teh logfile. Now... the logfile is auto cleared by dnetc.com so it won't find the same 'error' string again.

Now.. I needed a delay built into this... so my LAME way of doing it was to insert that Ping -n 20 command. :D Any more clever ways?

Thanks

BenchZowner
02-07-2009, 08:21 AM
Download the timeout.exe utility from http://www.dynawell.com/download/reskit/microsoft/win2000/timeout.zip
and enter the command timeout 20

whereas 20 = 20 seconds ;)

[XC] riptide
02-07-2009, 10:29 AM
Cheers. :)

Shadowmage
02-14-2009, 12:34 PM
WWhat I've done in the past is to compile a C program that just does a sleep command. Perl also works well for this.

RaZz!
02-22-2009, 03:03 PM
i never understood why ms removed the CHOICE command from the windows xp command line, as it does exactly what you want. i used the CHOICE command a lot while running win95 and 98 and had to rewrite all batchfiles i needed in winxp...

to use PING to let the batch wait is a common workaround if you want to rewrite batches which contain CHOICE cmds btw :) nothing to laugh at!

[XC] riptide
05-06-2009, 05:30 AM
OK. Short of starting a new thread, I'm now looking for a little script that will delete the 1st ALPHABETICAL file in a folder. So if I have files named A.txt, B.txt, C.txt I want a script that deletes A.txt (name of the file will be different everytime.... name is timestamp generated)

ahmad
05-06-2009, 10:09 AM
riptide;3773721']OK. Short of starting a new thread, I'm now looking for a little script that will delete the 1st ALPHABETICAL file in a folder. So if I have files named A.txt, B.txt, C.txt I want a script that deletes A.txt (name of the file will be different everytime.... name is timestamp generated)

That sounds easy enough to do...

do a

dir /b/on (bare/sort by name) > output.txt

then do a regex search using findstr

findstr /r ^[a-zA-Z] output.txt <= find anything beginning with a letter

then do a delete on the first thing returned from findstr.

Now working out the syntax and getting all the bits right requires some work. I'd do it for you except right now I have to run off to watch the ucl games :)

I should probably note that it maybe possible to use findstr to do all the work, I just haven't used it in a while.

tobe22
05-06-2009, 01:56 PM
in the current directory of execution:

REM start of code
for /R %%i in (*.txt) do (
echo %%i
goto end
)

:end
REM end of code

You could replace goto end with exit; replace the echo %%i with whatever command u want to use, such as

del /q %%i

tobe22
05-06-2009, 01:58 PM
if you don't want to recurse the sub directories, use "for /F" instead of "for /R"

ahmad
05-06-2009, 03:07 PM
if you don't want to recurse the sub directories, use "for /F" instead of "for /R"

I don't see how this would accomplish what he is looking for.

Explain to me what this does please? What I can read is: iterate through all the files and echo (or delete) them...

hecktic
05-06-2009, 04:50 PM
im not as qualified as the others here, but if I remember from what I dabbled in Qbasic and batch files that some kind of pause.exe program was used to countdown a timer and basically halt the process until the timer = 0

hope you get it working!

[XC] riptide
05-06-2009, 06:03 PM
Ya. I just used the ping command in the 1st problem I had.

Also the second problem I have 6 posts up, I should have explicitly stated that they are the oldest created files in any given directory. I said name is timestamp generated... but i should have been more specific.

ahmad
05-07-2009, 09:28 AM
riptide;3774789']Ya. I just used the ping command in the 1st problem I had.

Also the second problem I have 6 posts up, I should have explicitly stated that they are the oldest created files in any given directory. I said name is timestamp generated... but i should have been more specific.

You can easily do that with the dir command (sort by date).

What have you got so far?

[XC] riptide
05-07-2009, 11:00 AM
Well thata the easy bit, nearly the same as what your have.

dir /o /n > dir.txt


I was looking at piped commands to identify strings using find /i

dir /b /s | find /i "string">dir.txt

but I need to find an equivalent of the '/v' to just list out file names that do NOT find the string. :) These result/log files that I have end up borked by improper application shoutdown, meaning that they are ~70-80kb of empty space. So if I could get a list of files that DON'T have a certain common string, OR, just use a command that deleted the 1st file listed in the dir.txt file.

tobe22
05-07-2009, 12:14 PM
ahmad,

it does 1 iteration of the for loop, then exits, so from the description of the problem:

"So if I have files named A.txt, B.txt, C.txt I want a script that deletes A.txt."

works for me, if i have some test files, a.txt, b.txt, c.txt, d.txt

@REM start of code
@for /R %%i in (*.txt) do (
@echo Going to delete this file: %%i
del "%%i"
@pause
@exit
)

put the above in a batch file, test.bat, run test, it deletes a.txt, run again, it deletes b.txt

not that your steps won't work, but this avoids creating and then parsing temporary files, which could be huge, if u have a lot of files in your directories, which tend to be the case for "users".

tobe22
05-07-2009, 12:27 PM
to delete by oldest timestamp,

@REM start of code
@for /F %%i in ('dir /b /od *.txt') do (
@echo Going to delete this file: %%i
del "%%i"
@pause
@exit
)

remember to take out the pause, it's there for you to see what file it's deleting.

ahmad
05-07-2009, 12:38 PM
riptide;3775956']Well thata the easy bit, nearly the same as what your have.

dir /o /n > dir.txt


I was looking at piped commands to identify strings using find /i

dir /b /s | find /i "string">dir.txt

but I need to find an equivalent of the '/v' to just list out file names that do NOT find the string. :) These result/log files that I have end up borked by improper application shoutdown, meaning that they are ~70-80kb of empty space. So if I could get a list of files that DON'T have a certain common string, OR, just use a command that deleted the 1st file listed in the dir.txt file.

For that /v part you could just use regular expressions and findstr will work beautifully. I can't give you an easy regex examples if I don't know what you are trying to match against.

Not 100% sure this will work, but:




set deleted=0

FOR /F %%L in ('dir /b /s | find /i "string"') do (
if "deleted"=="1" goto :eof
del "%%L"
set deleted=1
)


Like tobe said, this maybe a little slow depending on how many files in that directory, if a few seconds is not an issue than this is perfectly ok. Not the smartest of solutions, but my basic is quite basic!

Please don't test this in a folder with files you need :)



ahmad,

it does 1 iteration of the for loop, then exits, so from the description of the problem:

"So if I have files named A.txt, B.txt, C.txt I want a script that deletes A.txt."

works for me, if i have some test files, a.txt, b.txt, c.txt, d.txt

@REM start of code
@for /R %%i in (*.txt) do (
@echo Going to delete this file: %%i
del "%%i"
@pause
@exit
)

put the above in a batch file, test.bat, run test, it deletes a.txt, run again, it deletes b.txt

not that your steps won't work, but this avoids creating and then parsing temporary files, which could be huge, if u have a lot of files in your directories, which tend to be the case for "users".

According to his specs, 1. file name is not always the same, 2. you must make sure that it is the first file with the an alphabet character in the first position, files MUST be sorted for this unless you want to write some really complicated if statements :)

tobe22
05-07-2009, 12:39 PM
"but I need to find an equivalent of the '/v' to just list out file names that do NOT find the string"

get a win32 version of grep, with these options:

-L, only print FILE names containing no match
-l, only print FILE names containing matches (lower case "el")

grep -L string *.txt

tobe22
05-07-2009, 12:41 PM
there are a lot of options for the dos for loop, as well as passing a set to the for loop, so how you want to order the files is trivial =) I just wanted to show that it could be done without creating and parsing an additional temporary file.

[XC] riptide
05-07-2009, 01:35 PM
OK. I'm going to see what I can do.

[XC] riptide
05-07-2009, 01:43 PM
to delete by oldest timestamp,

@REM start of code
@for /F %%i in ('dir /b /od *.txt') do (
@echo Going to delete this file: %%i
del "%%i"
@pause
@exit
)

remember to take out the pause, it's there for you to see what file it's deleting.

Works. :up: I'll try it on a few more folders and confirm. All backuped of course 1st.

EDIT: Ya. Its fine. Today, auto deleting old files... tomorrow... hacking the Pentagon. :cool::D

hecktic
05-07-2009, 10:43 PM
riptide;3776207']Works. :up: I'll try it on a few more folders and confirm. All backuped of course 1st.

EDIT: Ya. Its fine. Today, auto deleting old files... tomorrow... hacking the Pentagon. :cool::D

grats and lol @ pentagon joke