Results 1 to 22 of 22

Thread: Batch Script Help. - I need a delay

  1. #1
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556

    Batch Script Help. - I need a delay

    Ok so here it is...

    Code:
    :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 ... 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. Any more clever ways?

    Thanks

  2. #2
    One-Eyed Killing Machine
    Join Date
    Sep 2006
    Location
    Inside a pot
    Posts
    6,340
    Download the timeout.exe utility from http://www.dynawell.com/download/res...00/timeout.zip
    and enter the command timeout 20

    whereas 20 = 20 seconds
    Coding 24/7... Limited forums/PMs time.

    -Justice isn't blind, Justice is ashamed.

    Many thanks to: Sue Wu, Yiwen Lin, Steven Kuo, Crystal Chen, Vivian Lien, Joe Chan, Sascha Krohn, Joe James, Dan Snyder, Amy Deng, Jack Peterson, Hank Peng, Mafalda Cogliani, Olivia Lee, Marta Piccoli, Mike Clements, Alex Ruedinger, Oliver Baltuch, Korinna Dieck, Steffen Eisentein, Francois Piednoel, Tanja Markovic, Cyril Pelupessy (R.I.P. ), Juan J. Guerrero

  3. #3
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    Cheers.

  4. #4
    Xtreme Addict
    Join Date
    Aug 2004
    Location
    Austin, TX
    Posts
    1,346
    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.
    oh man

  5. #5
    Xtreme Addict
    Join Date
    Aug 2005
    Location
    Germany
    Posts
    2,247
    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!
    1. Asus P5Q-E / Intel Core 2 Quad Q9550 @~3612 MHz (8,5x425) / 2x2GB OCZ Platinum XTC (PC2-8000U, CL5) / EVGA GeForce GTX 570 / Crucial M4 128GB, WD Caviar Blue 640GB, WD Caviar SE16 320GB, WD Caviar SE 160GB / be quiet! Dark Power Pro P7 550W / Thermaltake Tsunami VA3000BWA / LG L227WT / Teufel Concept E Magnum 5.1 // SysProfile


    2. Asus A8N-SLI / AMD Athlon 64 4000+ @~2640 MHz (12x220) / 1024 MB Corsair CMX TwinX 3200C2, 2.5-3-3-6 1T / Club3D GeForce 7800GT @463/1120 MHz / Crucial M4 64GB, Hitachi Deskstar 40GB / be quiet! Blackline P5 470W

  6. #6
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    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)

  7. #7
    Muslim Overclocker
    Join Date
    May 2005
    Location
    Canada
    Posts
    2,786
    Quote Originally Posted by [XC] riptide View Post
    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.

    My watercooling experience

    Water
    Scythe Gentle Typhoons 120mm 1850RPM
    Thermochill PA120.3 Radiator
    Enzotech Sapphire Rev.A CPU Block
    Laing DDC 3.2
    XSPC Dual Pump Reservoir
    Primochill Pro LRT Red 1/2"
    Bitspower fittings + water temp sensor

    Rig
    E8400 | 4GB HyperX PC8500 | Corsair HX620W | ATI HD4870 512MB


    I see what I see, and you see what you see. I can't make you see what I see, but I can tell you what I see is not what you see. Truth is, we see what we want to see, and what we want to see is what those around us see. And what we don't see is... well, conspiracies.



  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    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

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    if you don't want to recurse the sub directories, use "for /F" instead of "for /R"

  10. #10
    Muslim Overclocker
    Join Date
    May 2005
    Location
    Canada
    Posts
    2,786
    Quote Originally Posted by tobe22 View Post
    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...

    My watercooling experience

    Water
    Scythe Gentle Typhoons 120mm 1850RPM
    Thermochill PA120.3 Radiator
    Enzotech Sapphire Rev.A CPU Block
    Laing DDC 3.2
    XSPC Dual Pump Reservoir
    Primochill Pro LRT Red 1/2"
    Bitspower fittings + water temp sensor

    Rig
    E8400 | 4GB HyperX PC8500 | Corsair HX620W | ATI HD4870 512MB


    I see what I see, and you see what you see. I can't make you see what I see, but I can tell you what I see is not what you see. Truth is, we see what we want to see, and what we want to see is what those around us see. And what we don't see is... well, conspiracies.



  11. #11
    Xtreme Mentor
    Join Date
    Mar 2007
    Posts
    2,588
    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!

  12. #12
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    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.

  13. #13
    Muslim Overclocker
    Join Date
    May 2005
    Location
    Canada
    Posts
    2,786
    Quote Originally Posted by [XC] riptide View Post
    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?

    My watercooling experience

    Water
    Scythe Gentle Typhoons 120mm 1850RPM
    Thermochill PA120.3 Radiator
    Enzotech Sapphire Rev.A CPU Block
    Laing DDC 3.2
    XSPC Dual Pump Reservoir
    Primochill Pro LRT Red 1/2"
    Bitspower fittings + water temp sensor

    Rig
    E8400 | 4GB HyperX PC8500 | Corsair HX620W | ATI HD4870 512MB


    I see what I see, and you see what you see. I can't make you see what I see, but I can tell you what I see is not what you see. Truth is, we see what we want to see, and what we want to see is what those around us see. And what we don't see is... well, conspiracies.



  14. #14
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    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.

  15. #15
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    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".

  16. #16
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    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.

  17. #17
    Muslim Overclocker
    Join Date
    May 2005
    Location
    Canada
    Posts
    2,786
    Quote Originally Posted by [XC] riptide View Post
    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:


    Code:
    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


    Quote Originally Posted by tobe22 View Post
    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
    Last edited by ahmad; 05-07-2009 at 12:45 PM.

    My watercooling experience

    Water
    Scythe Gentle Typhoons 120mm 1850RPM
    Thermochill PA120.3 Radiator
    Enzotech Sapphire Rev.A CPU Block
    Laing DDC 3.2
    XSPC Dual Pump Reservoir
    Primochill Pro LRT Red 1/2"
    Bitspower fittings + water temp sensor

    Rig
    E8400 | 4GB HyperX PC8500 | Corsair HX620W | ATI HD4870 512MB


    I see what I see, and you see what you see. I can't make you see what I see, but I can tell you what I see is not what you see. Truth is, we see what we want to see, and what we want to see is what those around us see. And what we don't see is... well, conspiracies.



  18. #18
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    "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

  19. #19
    Registered User
    Join Date
    Jun 2007
    Posts
    39
    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.

  20. #20
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    OK. I'm going to see what I can do.

  21. #21
    Moderator
    Join Date
    Mar 2006
    Posts
    8,556
    Quote Originally Posted by tobe22 View Post
    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. 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.
    Last edited by [XC] riptide; 05-07-2009 at 03:31 PM.

  22. #22
    Xtreme Mentor
    Join Date
    Mar 2007
    Posts
    2,588
    Quote Originally Posted by [XC] riptide View Post
    Works. 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.
    grats and lol @ pentagon joke

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •