[BATCH] Help with little batch operation

Discussion in 'Scripting' started by lysy.lbn, Dec 29, 2011.

  1. lysy.lbn

    lysy.lbn MDL Member

    Apr 20, 2011
    125
    65
    10
    #1 lysy.lbn, Dec 29, 2011
    Last edited by a moderator: Apr 20, 2017
    I mean something like
    Code:
    if file.txt exist goto 1 else goto 2
    
    :1
    save.exe -file1.txt
    goto end
    
    :2
    save.exe
    goto end
    
    :END
    but to make it "infinite".. I mean if file1.txt exist, save file2.txt, if file2.txt exist, save file3.txt

    This code above is just explain what I need to have, any help appreciated :)
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    What you need is a FOR-loop, see this post on the subject and see if you can adapt it for your own needs :)
     
  3. lysy.lbn

    lysy.lbn MDL Member

    Apr 20, 2011
    125
    65
    10
    Tried this but can't bring it to work :( It's always replace file1.txt

    Could You please give example how it should looks?
     
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    #4 Calistoga, Dec 31, 2011
    Last edited by a moderator: Apr 20, 2017
    I think I understand, you want the script to save files numbered ascending, so that when you have already saved file1.txt and file2.txt, the next one should naturally be file3.txt. To be honest I am not sure how to do that in Batch. In for example C# or AutoIt3 I would've used Regular Expressions to match the file names of all the text files, identified the highest number and then continued the numbering.

    Here is an example of how you can loop just the files that are formatted as file[...].txt
    Code:
    @echo off
    
    for %%i in (file*.txt) do echo %%i
    
    pause
    I'll get back to you if I come up with a solution, meanwhile maybe one of the Batch gurus will step in? :)
     
  5. bphlpt

    bphlpt MDL Junior Member

    Aug 2, 2010
    60
    36
    0
    #5 bphlpt, Dec 31, 2011
    Last edited by a moderator: Apr 20, 2017
    This might do what you want (minimally tested)

    Code:
    (SET "FILECNT=")
    GOTO TESTCOUNT
    :INCFILECOUNT
    (SET /A FILECNT=%FILECNT%+1)
    :TESTCOUNT
    IF EXIST file%FILECNT%.txt GOTO INCFILECOUNT
    save.exe file%FILECNT%.txt        REM or whatever you need to do with the file
    
    If you embed this in a loop or a subroutine you might need to change %FILECNT% to !FILECNT!

    Cheers and Regards
     
  6. lysy.lbn

    lysy.lbn MDL Member

    Apr 20, 2011
    125
    65
    10
    Thank You both for helping :)
    bphlpt's method works perfectly!

    PS. Happy New Year!
     
  7. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    421
    199
    10
    Glad you got it solved lysy, I learned something as well ;)

    Happy New Year!
     
  8. lysy.lbn

    lysy.lbn MDL Member

    Apr 20, 2011
    125
    65
    10
    To not make a mess I ask here..

    How to make my script read from .ini (or whatever else wchich could be edited in notepad)
    and place it content somewhere in my script

    For example, I have ini file with content of "/type:ANSI"
    and want to be placed "save.exe there file%FILECNT%.txt"