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...

11 Nov 2011   #21
caseyburk

Windows XP Professional 32bit
 
 

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


My System SpecsSystem Spec
.
11 Nov 2011   #22
Dwarf

Windows 8.1 Pro RTM x64
 
 

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

Windows XP Professional 32bit
 
 

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

11 Nov 2011   #24
strollin

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

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

Windows XP Professional 32bit
 
 

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 System SpecsSystem Spec
12 Nov 2011   #26
strollin

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

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 System SpecsSystem Spec
13 Nov 2011   #27
caseyburk

Windows XP Professional 32bit
 
 

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 System SpecsSystem Spec
13 Nov 2011   #28
strollin

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

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 System SpecsSystem Spec
15 Nov 2011   #29
caseyburk

Windows XP Professional 32bit
 
 

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:

Quote:
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 System SpecsSystem Spec
15 Nov 2011   #30
strollin

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

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 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 17:57.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App