New
#11
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?
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?
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
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.
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.
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.
The logs (I think) are coming from 2 different batch jobs...
Ah, OK. I'll work out some generic code and highlight the sections that need modifying/repeating to accommodate this.
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.
Are the filenames always of the format nameYYYY.ext,where the 4-digit year immediately precedes the file extension?