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   #11
caseyburk

Windows XP Professional 32bit
 
 

strollin, you must have been doing this stuff for some time... I was curious as to why you used CALL instead of GOTO and why with GOTO statements do you not need the colon in front of the subroutine labels but with CALL, you do?


My System SpecsSystem Spec
.
11 Nov 2011   #12
Dwarf

Windows 8.1 Pro RTM x64
 
 

An alternative, tested and working. Note that the source/destination locations highlighted in RED will need to be changed to suit your requirements.

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)
:parse
set filename=%1
if %filename:~0,20% == %filename% goto found20
if %filename:~0,24% == %filename% goto found24
:found20
echo %filename:~16,4%
echo %filename:~0,16%
goto bye
:found24
echo %filename:~20,4%
echo %filename:~0,20%
goto bye
:bye
pause
My System SpecsSystem Spec
11 Nov 2011   #13
strollin

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

LOL. Yeah, I wrote my first batch files under DOS in 1984 so I guess that means I've been doing it for awhile.

If you use CALL without the colon : it will look externally for a second batch file with that name. CALL with the colon allows you to call a subroutine within the current batch file. If you CALL either an external batch file or an internal subroutine, execution will return and resume with the line following the CALL. GOTO is used to jump to a line indicated by a label and doesn't return. The colon is required to indicate the label but is not used to GOTO that label.

As for what Dwarf posted, it's good practice to use a path to a specific location so that you don't need to be in a particular folder when the batch file is executed. If you're not careful and execute a batch file that assumes it's being executed within a particular folder and it isn't, there can be very unwanted results.
My System SpecsSystem Spec
.

11 Nov 2011   #14
strollin

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

BTW, it occurs to me that there is actually a bug in the batch file using the ELSE statement. As written, if the archives folder doesn't exist then it will be created but the rest of the files won't be processed. If the archives folder is found then the other files will be processed. The batch file will need to be executed twice if the archives folder doesn't exist the first time the batch is executed. I don't think that was your intention.

You don't want IF/ELSE in this case. You want to check for the existence of the folder but you want to parse the files regardless so the ELSE shouldn't be used. This has nothing to do with batch limitations, it would be the same with any language you used.
My System SpecsSystem Spec
11 Nov 2011   #15
caseyburk

Windows XP Professional 32bit
 
 

I'm really amazed at the response time you guys have demonstrated so far. (I'll definitely be spreading the word about this *place* for any future projects...) So thanks again for the help!

As for the explanations, I really appreciate that too. Am I to understand you correctly in that when you use a GOTO statement, the processing doesn't return back to that point after completing a FOR loop cycle (if it's inside a FOR loop)?--that explains why the GOTO statement inside a previous FOR loop wouldn't return after each iteration completed...

And 10-4 about the directory pathing. I'll remember that! It would be a shame to exclude that and inadvertently store over 1,000 log files inside something like C:\ or something... Ha. My boss would love that.
My System SpecsSystem Spec
11 Nov 2011   #16
Dwarf

Windows 8.1 Pro RTM x64
 
 

Why the 2 different lengths for the filenames?
My System SpecsSystem Spec
11 Nov 2011   #17
caseyburk

Windows XP Professional 32bit
 
 

The logs (I think) are coming from 2 different batch jobs...
My System SpecsSystem Spec
11 Nov 2011   #18
Dwarf

Windows 8.1 Pro RTM x64
 
 

Ah, OK. I'll work out some generic code and highlight the sections that need modifying/repeating to accommodate this.
My System SpecsSystem Spec
11 Nov 2011   #19
caseyburk

Windows XP Professional 32bit
 
 

I appreciate that Dwarf.

But if you're going to go that far with this, do you think you could provide some insight into this too?: Using the 7zip command line to archive (zip up) these log files based on their year? I was thinking that a routine could be created to copy each file into their respective year's directory, zip-up each year's directory, and then just delete the log files from the directory they were originally copied from IF and ONLY IF the original files DO NOT == the present year.

I think this would be possible and I'm more than willing to do the leg work with this, but have you ever done anything like this before? The copy+delete route was the only way I could think up to store a list as batch doesn't seem to have array support.
My System SpecsSystem Spec
11 Nov 2011   #20
Dwarf

Windows 8.1 Pro RTM x64
 
 

Are the filenames always of the format nameYYYY.ext,where the 4-digit year immediately precedes the file extension?
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 16:51.

Twitter Facebook Google+



Windows 7 Forums

Seven Forums Android App Seven Forums IOS App