Follow via:
RSS
Email Alert
Question
0 Votes
+ -

DOS Batch script to Print data to a file only when certain condition exit

I have created the below batch file to search for 4 records (POHEADR, REFID, NAME and LINREF )in all the files in the Orders folder.
I want to print certain fields from these records only when the "NAME NN" record contains the value, 9555527668. See the below sample file.
The script is not working, the output file is empty. Any help or suggestions will be greatly appreciated.
_______________________________________________________
Script:
@echo off
SET Location= C:\Orders\*.*
if not exist %Location% goto end
set PONO=
set REF=

setlocal EnableDelayedExpansion
for /f "tokens=*" %%a in ('type "%Location%"^|findstr "POHEADR REFID NAME LINREF"') do (
set Line=%%a
if "!Line:~0,7!"=="POHEADR" set PONO=%%a
if "!Line:~0,5!"=="REFID" set REF=%%a
if "!Line:~0,11"=="NAME NN" set BT=%%a
if "!Line:~0,11"=="LINREF RE" set ORD=%%a
if "!BT:~73,9!"=="9555527668" echo !PONO:~17,15! !ORD:~12,7! !REF:~12,10! >> \GIS\ORDERLIST.txt
)
:end
______________________________________________
Sample FIle:
POHEADR NN0121227996286 08109563165 16006923262DFFF 1212271636205025452
REFID RE 36796700
TIMEDATE 1020121230
TIMEDATE 0220130104
TRANINST FO M
NAME TT 98538556400000
NAME NN 95555276680000
NAME VE 99955248620000
ALLOWANC 999029003 000000000000450000000000000000000000
ALLOWDES ADJUSTMENT
ITEMLIN 000000000000018BG0000000190000 UP99999999988666 00000000000
LINDESC APPLES, GOLDEN DELICIOUS
LINREF RE Z123456
LINREF SI 331584733
LINNAME DD TEXAS
ORTOTAL 000000000000018BG000000000000 000000000000 00000000000000000000000000000
Tags: windows
2nd Jan

Answers (1)

0 Votes
+ -
Couple of thoughts...
For debugging purposes, I would sprinkle in some echo statements so you can see if the data is being extracted or not and what your variables are (or are not) getting set to.

From a variable standpoint NAME NN and NAME TT are the same thing, no? Would the batch file evaluate both of those as 'name'? I could be wrong but, I would be careful about having spaces in variable names in DOS

Can you do multiple variable evaluations on one pass, or do you need to loop through the file for each evaluation (I don't know batch that well!)

Do you have to declare and/or reset variables BT and ORD?
2nd Jan

Replies

Thanks for your thoughts. Yes, I did try debugging by using echo statements but still did not receive any results. This tells me I am doing something wrong.

NAME NN and NAME TT are not variable names they are records in the file. The file has are 3 NAME records.

I too am not good with batch. Some times I do ok and other times I have to ask for help.
jutpro 3rd Jan
Answer the question
Formatting +
BB Codes - Note: HTML is not supported in forums
  • [b] Bold [/b]
  • [i] Italic [/i]
  • [u] Underline [/u]
  • [s] Strikethrough [/s]
  • [q] "Quote" [/q]
  • [ol][*] 1. Ordered List [/ol]
  • [ul][*] · Unordered List [/ul]
  • [pre] Preformat [/pre]
  • [quote] "Blockquote" [/quote]

Join the TechRepublic Community and join the conversation! Signing-up is free and quick, Do it now, we want to hear your opinion.