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

Page 3 of 4 FirstFirst 1234 LastLast

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

    Yep, they are. Even the other process that generates the alternate character count has it that way...
      My Computer


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

    This is what I've got so far (note that I'm using .txt files to test):

    Code:
    rem test
    echo off
    cls
    if exist "c:\users\cyclops\desktop\archives" (goto main)
    mkdir "c:\users\cyclops\desktop\archives"
    :main
    for %%a in ("c:\users\cyclops\desktop\folder\*.txt") do (call :parse %%~na)
    goto finished
    :parse
    set filename=%1
    if %filename:~0,20% == %filename% goto found20
    if %filename:~0,24% == %filename% goto found24
    :found20
    set year=%filename:~16,4%
    set name=%filename:~0,16%
    goto update
    :found24
    set year=%filename:~20,4%
    set name=%filename:~0,20%
    goto update
    :update
    if exist "c:\users\cyclops\desktop\archives\"%year% (goto update1)
    mkdir "c:\users\cyclops\desktop\archives\"%year%
    :update1
    if exist "c:\users\cyclops\desktop\archives\"%year%"\"%filename%".txt" (goto 
    update2)
    copy "c:\users\cyclops\desktop\folder\"%filename%".txt" "c:\users\cyclops\desktop
    \archives\"%year%
    :update2
    echo %year%
    echo %name%
    pause
    goto :eof
    :finished
    echo finished
    pause
    Again, the items in RED need changing to suit your requirements. These are the sources/destinations.
      My Computer


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

    If both of you aren't making a six-digit income, you better be taking someone to court...

    That said, I have retrofit the following to support the 7zip... Seems to be working like a charm!

    Code:
    echo off
    cls
    if exist "C:\www\projects\ems\EMS_Batch\archives" (goto main)
    mkdir "C:\www\projects\ems\EMS_Batch\archives"
    
    :main
    for %%a in ("C:\www\projects\ems\EMS_Batch\*.log") do (call :parse %%~na)
    goto finished
    
    :parse
    set filename=%1
    if %filename:~0,20% == %filename% goto found20
    if %filename:~0,24% == %filename% goto found24
    
    :found20
    set year=%filename:~16,4%
    set name=%filename:~0,16%
    goto update
    
    :found24
    set year=%filename:~20,4%
    set name=%filename:~0,20%
    goto update
    
    :update
    if exist "C:\www\projects\ems\EMS_Batch\archives\"%year% (goto update1)
    mkdir "C:\www\projects\ems\EMS_Batch\archives\"%year%
    
    :update1
    if exist "C:\www\projects\ems\EMS_Batch\archives\"%year%"\"%filename%".log" (goto update2)
    copy "C:\www\projects\ems\EMS_Batch\"%filename%".log" "C:\www\projects\ems\EMS_Batch\archives\"%year%
    7za a -tzip -mx9 "C:\www\projects\ems\EMS_Batch\archives\archive_for_"%year%".zip" "C:\www\projects\ems\EMS_Batch\archives\"%year%
    
    :update2
    goto :eof
    
    :finished
    pause
    Now I just need to delete the originals log files found in C:\www\projects\ems\EMS_Batch by deleting every log file that is two years below the difference between the present date minus 2. For example, if this year is 2011, anything less than 2010 (2009 and earlier) needs to be archived then their originals deleted from the directory they were originally in...

    Any suggestions with deleting the originals? I think I can handle it for the most part--I just finished the date routine and will post this later (probably tomorrow or Sunday).
      My Computer


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

    It seems redundant to me to create a zip file called archive_for_year_2008.zip and then place it in a folder called 2008. Why not just put the archive_for_2008.zip file directly in the archives folder?

    Also, why is it necessary to copy the log file to the year folder first? Just add it to the archive from the original folder.

    Code:
    @ECHO OFF
    CLS
    
    IF NOT EXIST "C:\users\dad\archives" MKDIR "C:\users\dad\archives"
    FOR %%G IN ("C:\users\dad\*.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
    
    REM Process logfiles with 20 char names
    :found20
    set archname=archive_for_%filename:~16,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip 
    7za a "C:\users\dad\archives\"%archname% "C:\users\dad\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL "C:\users\dad\"%filename%.log
    goto:EOF
    
    REM Process logfiles with 24 char names
    :found24
    set archname=archive_for_%filename:~20,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip 
    7za a "C:\users\dad\archives\"%archname% "C:\users\dad\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL %filename%.log
    Not sure how to only archive/delete the files 2 years previous to the current year from a batch file. Have to think about that.
      My Computer


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

    Thanks for the feedback, strollin.

    You see, with that 7zip program I can probably do what you say (about adding files directly to an individual / multiple archive). Before, I had thought I was going to be using some erroneous Windows zip app or something, but after realizing I had to download and use this third-party 7zip just to archive stuff on this Windows machine, I then learned that I could not only zip stuff up but probably also add to already-existing archive files.

    Anyway, yes, you're right about that direction being a redundant approach. That aside, one part of redundancy I've noticed from what I've made is the part where it creates the year folder and THEN zips it... Maybe we're talking about the same thing? Who knows... Either way, that 7zip is going to remedy this part.

    As for the date, I had thought I would create a "present" variable assigned with something like "%date:~-4%" and then just throw "present" against an LWQ condition to check whether it is LWQ the present variable. If it IS, then then it would know to go ahead and archive the file.

    -- But again, now that I think about what you said, you're definitely right about the redundant part. I'll need to figure something about about updating the archive file if the years match upon being checked for. Good eye.
      My Computer


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

    I think this may be pretty close to what you need.

    Code:
    @ECHO OFF
    CLS
    
    REM Get today's date
    FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
    
    REM Parse out current Year
    FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
        SET DayMonth=%%A
        SET MonthDay=%%B
        SET Year=%%C
    )
    
    REM Subtract 2 from current year
    set /a strtyr=%Year%-2
    
    IF NOT EXIST "C:\users\dad\archives" MKDIR "C:\users\dad\archives"
    FOR %%G IN ("C:\users\dad\*.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
    
    REM Process logfiles with 20 char names
    :found20
    
    REM Skip files that are within current and previous year
    set /a archive_yn=%strtyr% - %filename:~16,4%
    if %archive_yn% == -1 goto :EOF
    if %archive_yn% == -2 goto :EOF
    
    set archname=archive_for_%filename:~16,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip
    7za a "C:\users\dad\archives\"%archname% "C:\users\dad\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL "C:\users\dad\"%filename%.log
    goto:EOF
    
    REM Process logfiles with 24 char names
    :found24
    
    REM Skip files that are within current and previous year
    set /a archive_yn=%strtyr%-%filename:~20,4%
    if %archive_yn% == -1 goto :EOF
    if %archive_yn% == -2 goto :EOF
    
    set archname=archive_for_%filename:~20,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip
    7za a "C:\users\dad\archives\"%archname% "C:\users\dad\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL %filename%.log
      My Computer


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

    strollin, you are awesome! Everything worked out great except for the 24 counts that were not removed at the end--which I fixed (unless you see something wrong with what I did--it's in bold.):

    Code:
    @ECHO OFF
    CLS
    
    REM Get today's date
    FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
    
    REM Parse out current Year
    FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
        SET DayMonth=%%A
        SET MonthDay=%%B
        SET Year=%%C
    )
    
    REM Subtract 2 from current year
    set /a strtyr=%Year%-2
    
    IF NOT EXIST "C:\www\projects\ems\EMS_Batch\archives" MKDIR "C:\www\projects\ems\EMS_Batch\archives"
    FOR %%G IN ("C:\www\projects\ems\EMS_Batch\*.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
    
    REM Process logfiles with 20 char names
    :found20
    
    REM Skip files that are within current and previous year
    set /a archive_yn=%strtyr% - %filename:~16,4%
    if %archive_yn% == -1 goto :EOF
    if %archive_yn% == -2 goto :EOF
    
    set archname=archive_for_%filename:~16,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip
    7za a "C:\www\projects\ems\EMS_Batch\archives\"%archname% "C:\www\projects\ems\EMS_Batch\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL "C:\www\projects\ems\EMS_Batch\"%filename%.log
    goto:EOF
    
    REM Process logfiles with 24 char names
    :found24
    
    REM Skip files that are within current and previous year
    set /a archive_yn=%strtyr%-%filename:~20,4%
    if %archive_yn% == -1 goto :EOF
    if %archive_yn% == -2 goto :EOF
    
    set archname=archive_for_%filename:~20,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip
    7za a "C:\www\projects\ems\EMS_Batch\archives\"%archname% "C:\www\projects\ems\EMS_Batch\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL "C:\www\projects\ems\EMS_Batch\"%filename%.log
    goto:EOF
    Do you see any problems with that?
      My Computer


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

    Looks good, I forgot to add the path to the logfile at the end. The last goto :EOF isn't needed because you're already at the end of the file. Doesn't hurt, just isn't needed.
      My Computer


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

    So I'm having a difficult time understanding why the following isn't working... (I copied the original code and modified some locations to reflect a setup I'm working with on my machine.):

    Code:
    @ECHO OFF
    CLS
    
    REM Get today's date
    FOR /F "tokens=*" %%A IN ('DATE/T') DO FOR %%B IN (%%A) DO SET Today=%%B
    
    REM Parse out current Year
    FOR /F "tokens=1-3 delims=/-" %%A IN ("%Today%") DO (
        SET DayMonth=%%A
        SET MonthDay=%%B
        SET Year=%%C
    )
    
    REM Subtract 2 from current year
    set /a strtyr=%Year%-2
    
    IF NOT EXIST "C:\www\projects\ems\EMS_Batch\ICS Archive\archives" MKDIR "C:\www\projects\ems\EMS_Batch\ICS Archive\archives"
    FOR %%G IN ("C:\www\projects\ems\EMS_Batch\BannerBatch\*.log") DO (call :PARSE %%~nG)
    goto:EOF
    
    :parse
    SET filename=%1
    IF  %filename:~0,60% == %filename% goto found60
    
    REM Process logfiles with 60 char names
    :found60
    
    REM Skip files that are within current and previous year
    set /a archive_yn=%strtyr%-%filename:~52,4%
    
    if %archive_yn% == -1 goto :EOF
    if %archive_yn% == -2 goto :EOF
    
    set archname=archive_for_%filename:~52,4%.zip
    
    REM Add logfile to archive
    REM Redirection to NUL to suppress messages from 7 zip
    7zip a "C:\www\projects\ems\EMS_Batch\ICS Archive\archives\"%archname% "C:\www\projects\ems\EMS_Batch\BannerBatch\"%filename%.log > NUL
    
    REM Delete logfile after archived, no prompt
    ECHO Y | DEL "C:\www\projects\ems\EMS_Batch\BannerBatch\"%filename%.log
    goto:EOF
    I feel pretty stupid right now because I just can't wrap my head around what I've done here. Execution creates the "archives" directory just fine and I can echo the "%%~nG" perfectly fine inside the parenthesis of the DO command, but then if I try to echo the %1 inside the parse subroutine, it just gives me the folder name of "BannerBatch".

    What gives? This is so frustrating. Funny in a way, but very frustrating...

    Example files I'm working with now:

    BannerBatch SCT Banner Batch Download Interface 01012009.log
    BannerBatch SCT Banner Batch Download Interface 01012009.log
    BannerBatch SCT Banner Batch Download Interface 01012009.log
    BannerBatch SCT Banner Batch Download Interface 01012009.log
    ...
    (The files are consistently named this way.)
      My Computer


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

    You need to put quotes " around the filename to get it to include the whole filename or else it will only read up to the first blank.

    Code:
    FOR %%G IN ("C:\www\projects\ems\EMS_Batch\BannerBatch\*.log") DO (call :PARSE "%%~nG")
      My Computer


 
Page 3 of 4 FirstFirst 1234 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 04:38.
Find Us