|
|
|
BATCH reading structured Text File
|
Original Message
|
Name: thegrantguy
Date: July 18, 2006 at 02:35:28 Pacific
Subject: BATCH reading structured Text FileOS: XP SP2CPU/Ram: CeleronR 2.93GHz, 512 MBModel/Manufacturer: Dell Dimension DIM3000 |
Comment: Hello All! I have a formatted text file that I wish to read with a batch file for further processing. It is formatted like so: o,object1.sql r,report1.xml r,report2.xml b,table1.sql . . . and so on, where the first character is a code which tells what kind of file it is (since some of them have the same file extensions) followed by a (,) delimiter, and the file name. I am trying to write a batch that goes through the whole file and completes the commands for each file. Here is my code: for /f "delims=," %%a in (testing.txt) do ( if %%a==o goto OBJECTS if %%a==r goto REPORTS if %%a==b goto TABLES ) :OBJECTS for /f "tokens=2 delims=," %%b in (testing.txt) do echo.This is where further commands will go on %%b goto EOF and so on... for each type of file. My problem is, when a match is found in the first for command, it goes to the correct section, but then it does the command for all files in testing.txt, not just the one found. How can I get it to go through all the files, and do the neccessary command on each depending on their code? ANY help on this would be GREATLY appreciated as I have been working on this for some time now. Thank you all in advance. Cheers, tgg help. me.
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: Shr0Om
Date: July 18, 2006 at 04:17:05 Pacific
|
Reply: (edit)You are running a new FOR loop in your sections which will run trough all the file again: "for /f "tokens=2 delims=," %%b in (testing.txt) do .." You need a different way to filter out objects in the text file. A way around this problem could maybe be to divide testing.txt into Objects.txt, Reports.txt etc. Something like this: for /f "delims=," %%a in (testing.txt) do ( if %%a==o echo %%a>> OBJECTS.txt if %%a==r echo %%a>>REPORTS.txt if %%a==b echo %%a>>TABLES.txt ) and then do operations on respective files with the FOR /f Another way could be to use delayed expansion. (A bit more complicated i guess). This way you would check evey line if its a object or a report etc, then execute some code on that spesific filename and then check the next line in testing.txt, and so forth. I dont know if you are about do something with actual files tho? Some pseudo example: @echo off SetLocal EnableDelayedExpansion For /F "delims=," %%A in (testfile.txt) Do ( Set code=%%A Set FileName=%%B Set WhatEverIsAfterTheDelimiter=%%C if %code% equ o call :OBJECTS if %code% equ r call :REPORTS ) :OBJECTS (Do some command on %FileName%) :REPORTS (Do some command on %FileName%)
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: thegrantguy
Date: July 18, 2006 at 05:03:56 Pacific
|
Reply: (edit)Hello again, I've been playing around with it a little: for /f "delims=," %%a in (testing.txt) do ( if %%a==o echo %%a>>OBJECTS.txt if %%a==r echo %%a>>REPORTS.txt if %%a==f echo %%a>>FORMS.txt ) This works just fine in creating the new files, but instead of the filename in the new files, its only showing the code. i.e. OBJECTS.txt contains: o REPORTS.txt contains: r r How do you get it to show the filename instead of the code?? Thank you so much for the help so far, this will really help me out (the may even start paying me... haha) Cheers. help. me.
Report Offensive Follow Up For Removal
|
|
Response Number 4
|
Name: Shr0Om
Date: July 18, 2006 at 06:05:33 Pacific
|
Reply: (edit)Ah sorry.. You arent using any tokens,so it will only write the first token before the "," to the file (which is o, r etc). Try this code instead (Not tested): @echo off SetLocal EnableDelayedExpansion For /F "tokens=1-2* delims=," %%A in (testfile.txt) Do ( Set code=%%A Set FileName=%%B if %code% equ o echo %%B >>OBJECTS.txt if %code% equ r echo %%B >>REPORTS.txt )
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: thegrantguy
Date: July 18, 2006 at 06:34:25 Pacific
|
Reply: (edit)Hello and thanks for the quick reply again! When the .bat enables DelayedExpansion, this is the CMD Window: C:\workfolder>SetLocal EnableDelayedExpansion o was unexpected at this time. C:\workfolder>if equ o echo %B>>OBJECTS.txt Any idea why "o was unexpected" and why its returning %code% as a blank? Thanks again! help. me.
Report Offensive Follow Up For Removal
|
|
Response Number 6
|
Name: Mechanix2Go
Date: July 18, 2006 at 17:06:03 Pacific
|
Reply: (edit)Try this: ::== @echo off for /f "tokens=1-2 delims=," %%T in (testing.txt) do ( if %%T equ o echo progA %%U if %%T equ r echo progB %%U if %%T equ b echo progC %%U ) ::== ===================================== If at first you don't succeed, you're about average.M2
Report Offensive Follow Up For Removal
|
|
Response Number 7
|
Name: Shr0Om
Date: July 18, 2006 at 23:58:21 Pacific
|
Reply: (edit)Sorry for my sloppyness.. I didnt test the code, was just a quickly modified copy/paste from some old source. M2 has provided you with a code that works! :) I modified it slightly so it writes the filenames with code o to Objects.txt and the ones with code r to Reports.txt etc.. Its tested and should work. ::---CODE--- @echo off for /f "tokens=1-2 delims=," %%A in (testfile.txt) do ( if %%A equ o echo %%B>>Objects.txt if %%A equ r echo %%B>>Reports.txt if %%A equ b echo %%B>>Tables.txt ) ::---CODE---
Report Offensive Follow Up For Removal
|
|
Response Number 8
|
Name: thegrantguy
Date: July 19, 2006 at 01:13:13 Pacific
|
Reply: (edit)M2 and Shr0Om, THANK YOU! It works great! This will definately help me out. I am very appreciative of the help, and I'm sure I'll be back in the future with more of my problems... ;) Cheers! tgg help. me.
Report Offensive Follow Up For Removal
|
Use following form to reply to current message:
|
|

|