Windows 7 Forums

Welcome to Windows 7 Forums. Our forum is dedicated to helping you find support and solutions for any problems regarding your Windows 7 PC be it Dell, HP, Acer, Asus or a custom build. We also provide an extensive Windows 7 tutorial section that covers a wide range of tips and tricks.


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

10 Nov 2011   #1
caseyburk

Windows XP Professional 32bit
 
 
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 System SpecsSystem Spec
.
10 Nov 2011   #2
Dwarf

Windows 8.1 Pro RTM x64
 
 

Hi caseyburk and welcome to W7 Forums

Are all the filenames exactly the same length and structure?
My System SpecsSystem Spec
10 Nov 2011   #3
strollin

W10 Pro desktop, W10 laptop, W10 laptop, W10 Pro tablet (all 64-bit)
 
 

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 System SpecsSystem Spec
.

10 Nov 2011   #4
caseyburk

Windows XP Professional 32bit
 
 

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 System SpecsSystem Spec
10 Nov 2011   #5
strollin

W10 Pro desktop, W10 laptop, W10 laptop, W10 Pro tablet (all 64-bit)
 
 

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 System SpecsSystem Spec
11 Nov 2011   #6
caseyburk

Windows XP Professional 32bit
 
 

Excellent, strollin! That did the trick!
My System SpecsSystem Spec
11 Nov 2011   #7
caseyburk

Windows XP Professional 32bit
 
 

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 System SpecsSystem Spec
11 Nov 2011   #8
strollin

W10 Pro desktop, W10 laptop, W10 laptop, W10 Pro tablet (all 64-bit)
 
 

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.
My System SpecsSystem Spec
11 Nov 2011   #9
caseyburk

Windows XP Professional 32bit
 
 

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 System SpecsSystem Spec
11 Nov 2011   #10
strollin

W10 Pro desktop, W10 laptop, W10 laptop, W10 Pro tablet (all 64-bit)
 
 

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 System SpecsSystem Spec
Reply

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




Thread Tools Search this Thread
Search this Thread:

Advanced Search




Similar help and support threads
Thread Forum
How to add to RAR archive 40k files without using windows search.
I have over 40 thousand files I need to archive spread throughout a drive in different folders. I cannot use windows search because the drive cannot be index and will take quite some time to fix and solve. Is there another method that can allow me to search a drive and add to winrar .rar archive?...
General Discussion
Unable to correctly parse lines in a .txt file through batch
Good Afternoon All, This is my first post so if I inadvertently insult anyone through my mannerism I apologize in advance. So the puzzle is that my company creates programs for our clients and would like me to create a batch file that can be placed on the desktop and auto-copy all of the...
General Discussion
bulk delete certain types of files in rar/zip archive
i've got thousands of rar/zip files where each of them could have some types of files I don't want. for example, *.txt or *.html etc. I would like to know if there is any shortcut way to delete them all quickly? I don't want to do it one by one. I will go crazy
General Discussion
How to script a daily copy of files w/dt being name of archive folder
Hello, I need to figure out a Windows 7 script to copy/archive files which are located in a specific folder. It needs to copy daily to an archive folder, with the script creating the archive folder as MM-DD-YY (being that day's date that is being archived) before it archives the files to that...
General Discussion
removing damaged archive files?
Running win 7. Just scanned my C drive w/ESET and the scan turned up several damaged archived files (see below). Should I: 1. Delete the damaged archived files shown trusting that there won't be any repercussions later? 2. Leave everything as is and forget about it? 3. Any reason not to...
General Discussion
Batch code to read, then move files from one folder to another
I'm new to batch coding, so I was hoping someone could help me out. I need to read several .txt files and search them for a specific string, lets say "key1" and "key2." If the file has key1, then it needs to be moved into directory1. If the file has key2, then it needs to be moved to directory2....
General Discussion


Our Sites

Site Links

About Us

Find 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:31.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App