Trying to use batch code to parse and archive log files... Errors...

Page 1 of 4 123 ... LastLast

  1. Posts : 19
    Windows XP Professional 32bit
       #1

    Trying to use batch code to parse and archive log files... Errors...


    Hi all. I have a quick question about compressing a bunch of log files that have specific years at the end of their file names...

    Let's say I have the following set of files needing to be archived by year:
    BVC2v2Batch.01042008.log
    BVC2v2Batch.02042008.log
    BVC2v2Batch.01042009.log
    BVC2v2Batch.02042009.log
    BVC2v2Batch.03042009.log
    BVC2v2Batch.01042010.log
    BVC2v2Batch.02042010.log
    BVC2v2Batch.01042011.log
    ...

    What I need is to basically place each group prior to 2010 inside their own year's ZIP folder. So for example, everything with the year of 2008 would be in bvc2008.zip, 2009 would be in bvc2009.zip, etc. Each year's log files inside their year's ZIP folder.

    So below is where I am right now:
    Code:
    @ECHO OFF
    CLS
    
    :: 
    :: If the archives directory doesn't  exist, create it...
    :: 
    :START
    @ECHO "Step 1..."
    IF NOT  EXIST archives GOTO CREATE_DIR
    
    
    ::
    :: Subroutine to create  archives directory...
    ::
    :CREATE_DIR
    @ECHO "Step 2..."
    MKDIR  archives
    
    
    ::
    :: Initialize main routine...
    :: Notes: ~n  excludes the file extension.
    ::
    :INITIALIZE
    @ECHO "Step 3..."
    FOR  %%G IN (*.log) DO (CALL :PARSE %%~nG)
    PAUSE
    
    
    ::
    ::  Parse filename...
    :: Notes: This parses names based on the number of  characters the file name has. The first IF looks for 20-counts.
    ::         The next IF looks for 24-counts.
    ::
    :PARSE
    SET filename=%1
    IF  %filename:~0,20% == %filename% (
       @ECHO %filename:~16,4%
    )
    IF  %filename:~0,24% == %filename% (
       @ECHO %filename:~20,4%
    )
    EXIT  /b
    When the above is executed, it looks for an "archives" folder. If it's not there, it tries to create it as a place to store the yearly ZIP archives (notice the years are in bold in the above list of example files). Beyond that, it just goes through steps of reading the file names and parsing the name itself if and only if they're of type *.log. As you can see, I'm still working on this but wanted to understand why "ECHO is off." keeps repeated during runtime... I thought I had it set to OFF. It has me stumped. Why does it do this?

    Any help with this is appreciated.
      My Computer


  2. Posts : 9,582
    Windows 8.1 Pro RTM x64
       #2

    Hi caseyburk and welcome to W7 Forums

    Are all the filenames exactly the same length and structure?
      My Computer


  3. Posts : 3,371
    W10 Pro desktop, W11 laptop, W11 Pro tablet (all 64-bit)
       #3

    After you set @ECHO OFF, it's not necessary to use the @ sign anymore on your ECHO statements.

    Try

    ECHO "Step 1..."

    instead of

    @ECHO "Step 1..."
      My Computer


  4. Posts : 19
    Windows XP Professional 32bit
    Thread Starter
       #4

    Thanks for the welcome, Dwarf. :.)

    And yes, the file names are the same length and structure.

    strollin, I tried what you said but it still repeated "Echo is off."
      My Computer


  5. Posts : 3,371
    W10 Pro desktop, W11 laptop, W11 Pro tablet (all 64-bit)
       #5

    Try this:

    Code:
    @ECHO OFF
    CLS
    
    :: 
    :: If the archives directory doesn't  exist, create it...
    :: 
    :START
    ECHO "Step 1..."
    IF NOT  EXIST archives GOTO CREATE_DIR
    
    
    ::
    :: Subroutine to create  archives directory...
    ::
    :CREATE_DIR
    ECHO "Step 2..."
    MKDIR  archives
    
    
    ::
    :: Initialize main routine...
    :: Notes: ~n  excludes the file extension.
    ::
    :INITIALIZE
    ECHO "Step 3..."
    FOR  %%G IN (*.log) DO (call :PARSE %%~nG)
    PAUSE
    goto bye
    
    
    ::
    ::  Parse filename...
    :: Notes: This parses names based on the number of  characters the file 
    
    name has. The first IF looks for 20-counts.
    ::         The next IF looks for 24-counts.
    ::
    :PARSE
    SET filename=%1
    IF  %filename:~0,20% == %filename% goto found20
    IF  %filename:~0,24% == %filename% goto found24 
    :found20
    ECHO %filename:~16,4%
    goto bye
    :found24
    ECHO %filename:~20,4%
    :bye
    EXIT /b
    Batch files always want to process all lines so you need to jump over lines that you don't want processed.
      My Computer


  6. Posts : 19
    Windows XP Professional 32bit
    Thread Starter
       #6

    Excellent, strollin! That did the trick! :)
      My Computer


  7. Posts : 19
    Windows XP Professional 32bit
    Thread Starter
       #7

    Okay guys, I'm back...

    Unfortunately, I came up with the bright idea to improve the readability with the code, and of course, I broke it... Any ideas why? (See below.)

    Also, why does it immediately exit from the entire window instead of simply stopping or displaying the last value it encounters before faulting? (In other words, how the heck does one debug / test something like this when you can't even get the window to stay open despite having pause statements and purposely created structures meant to assist in assessing a given issue at any one time?)

    Code:
    @ECHO OFF
    CLS
    
    IF NOT EXIST archives (
      MKDIR archives
      GOTO start
    ) ELSE (
      :start
        FOR %%G IN (*.log) DO (call :PARSE %%~nG)
      :parse
        SET filename=%1
        IF  %filename:~0,20% == %filename% goto found20
        IF  %filename:~0,24% == %filename% goto found24 
      :found20
        ECHO %filename:~16,4%
        goto bye
      :found24
        ECHO %filename:~20,4%
        goto bye
    )
    
    :bye
    EXIT /b
    PAUSE
    Any light you can shine on this is appreciated. Obviously, I don't know something about what I thought I was doing and if you can help this new guy out, it would really be appreciated.
      My Computer


  8. Posts : 3,371
    W10 Pro desktop, W11 laptop, W11 Pro tablet (all 64-bit)
       #8

    This works for me:

    Code:
    @ECHO OFF
    CLS
    
    IF NOT EXIST archives MKDIR archives
    FOR %%G IN (*.log) DO (call :PARSE %%~nG)
    goto:EOF
    
    
       :parse
       SET filename=%1
        IF  %filename:~0,20% == %filename% goto found20
        IF  %filename:~0,24% == %filename% goto found24 
      :found20
        ECHO %filename:~16,4%
        goto:EOF
      :found24
        ECHO %filename:~20,4%
    Batch language doesn't really support IF THEN ELSE type structures like a full programming language. As I mentioned in my previous post, batch files want to process each line in sequence so if you need things processed other than sequentially, you need to use the goto to jump around.

    The EXIT /b you had at the end isn't necessary. The behavior you are describing of the window closing is what I would expect if you had EXIT at the end (without the /b). EXIT means to exit and close the window, EXIT /b means to exit the batch file immediately without closing the window.

    You could replace the first goto:EOF line with EXIT /b in this case because they both cause the batch file to exit at that point.
    Last edited by strollin; 11 Nov 2011 at 10:43.
      My Computer


  9. Posts : 19
    Windows XP Professional 32bit
    Thread Starter
       #9

    Yep, works for me too. :)

    Am I crazy for trying to use this as a batch way of backing-up files through the vanilla ZIP feature of Windows (specially, Windows Server 2003)?

    The original intent behind all this was that I was in need of a way to back up a bunch of log files based on year with each year equating to its own ZIP file archive, but if batch doesn't even support basic IF/ELSE constructs, then it's making me wonder what else it probably doesn't support...

    Thoughts?
      My Computer


  10. Posts : 3,371
    W10 Pro desktop, W11 laptop, W11 Pro tablet (all 64-bit)
       #10

    I modified your file and made it work with the IF ELSE you had. The real problem is that :PARSE is a subroutine that is being called so it can't be part of the ELSE. The ELSE isn't necessary as I showed earlier but it can be used.

    Code:
    @ECHO OFF
    CLS
    
    IF NOT EXIST archives (
      MKDIR archives
    ) ELSE (
        FOR %%G IN (*.log) DO (call :PARSE %%~nG)
       goto:EOF
    )
      :parse
        SET filename=%1
        IF  %filename:~0,20% == %filename% goto found20
        IF  %filename:~0,24% == %filename% goto found24 
      :found20
        ECHO %filename:~16,4%
        goto:EOF
      :found24
        ECHO %filename:~20,4%
      My Computer


 
Page 1 of 4 123 ... LastLast

  Related Discussions
Our Sites
Site Links
About Us
Windows 7 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 7" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:56.
Find Us