Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 EOFand 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,
tgghelp. me.

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 /fAnother 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%)

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

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
)

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.txtAny idea why "o was unexpected" and why its returning %code% as a blank?
Thanks again!
help. me.

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

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

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!
tgghelp. me.

![]() |
Simple Batch ? - Txt File...
|
c++ system() and variable...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |