Help with multiple for loop in a DOS batch script (Part 2)

350310 pts.
Tags:
DOS
DOS Batch
DOS program
Below is a copy of a batch file, GetItemNum.bat, I created to loop through files in a PO directory and output certain data in all the LINEITEM and LINDESC fields. Below is a copy of the PO_FILE I am working with. In the LINEITEM field I want the 12 character data that is in the 38[sup]th[/sup] position.  In the LINDESC field I want everything that is there.  When the batch is executed the output contains the directory path, file names and also the words “ECHO is off.”, plus the output is all wrong. Any help with this script will be greatly appreciated.[o:p][/o:p]

Answer Wiki

Thanks. We'll let you know when a new response is added.

1. The variables used for the FOR loops were not cleared before use (initialized).
2. The FIND command was outputting some extraneous lines. Switched to FINDSTR and TYPE to input file.
3. Changed the tokens for the description FOR to grab entire description.
4. Personally I do not like using variables which match commands as these can cause bad results. Replaced varable Dir with Location.
5. Using C:FileName can give unexpected results. Always give path i.e. C:\, C:\Temp.
6. GOTO END may not give expected results. Use a defined lable or EXIT.

<pre>@echo off
SET Location=C:\Temp\
if not exist %Location% goto :DONE
setlocal EnableDelayedExpansion
set ITEM=
set DESC=
for /f “tokens=1″ %%a in (‘type “%Location%*PO_FILE_*”^|findstr /i “LINEITEM”‘) do (
set ITEM=%%a
for /f “tokens=1,*” %%a in (‘type “%Location%*PO_FILE_*”^|findstr /i “LINDESC”‘) do (
set DESC=%%b
echo !ITEM:~38,12! >> C:\Temp\ITEMLIST.txt
echo !DESC! >> C:\Temp\ITEMLIST.txt
)
)

:DONE
endlocal
</pre>

Discuss This Question: 4  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • ITKE
    Below is a copy of a batch file, GetItemNum.bat, I created to loop through files in a PO directory and output certain data in all the LINEITEM and LINDESC fields. Below is a copy of the PO_FILE I am working with. In the LINEITEM field I want the 12 character data that is in the 38th position. In the LINDESC field I want everything that is there. When the batch is executed the output contains the directory path, file names and also the words “ECHO is off.”, plus the output is all wrong. Any help with this script will be greatly appreciated. This is how the output file should look: 000000078861 ORANGES,BLOOD RED 12/3LB 000000072862 BANANAS,GREEN 000000099630 CHERRIES 12/3 000000089632 PLUMS, 3 LB This is a copy of the batch script, GetItemNum.bat: @echo off SET dir=C:PO if not exist %dir% goto END setlocal EnableDelayedExpansion for /f "tokens=1" %%a in ('find /i "LINEITEM" "%dir%*PO_FILE_*"') do ( set ITEM=%%a for /f "tokens=1,2 delims= " %%a in ('find /i "LINDESC" "%dir%*PO_FILE_*"') do ( set DESC=%%b echo !ITEM:~38,12! >> C:ITEMLIST.txt echo !DESC! >> C:ITEMLIST.txt ) ) This is a sample of the PO_FILE: HEADER R2011120208957861000000000000000001996484583000000160068232620000001112021205000000018T DATETIME0220111211 NOTES INTBuyer:555 Lane, TRANINSTPP P 04Atlanta, Ga. 30368-2, CUSTNAME1 BTXYZ Company 90721498690090 CUSTNAME2 STXYZ Company 90721498690080 VENNAME VNUS FRUIT VENDOR 90000006975262 LINEITEM000000000000084CA0000000145000000000078861 BI78861 LINDESC ORANGES,BLOOD RED 12/3LB. LIDOOOO 000012000000030000LB LINEITEM000000000000084CA0000000145000000000072862 BI72862 LINDESC BANANAS,GREEN LIDOOOO 000012000000030000LB LINEITEM000000000000084CA0000000145000000000099630 BI99630 LINDESC CHERRIES 12/3 LIDOOOO 000012000000030000LB LINEITEM000000000000126CA0000000145000000000089632 BI89632 LINDESC PLUMS, 3 LB LIDOOOO 000012000000030000LB TOTAL 000000000000378UN000001545600LB000007161000CF00000000000000000000000548100T
    350,310 pointsBadges:
    report
  • jutpro
    Hello Pjb0222, Thank you for your help. I did include the “” in the batch script but somehow it did not transfer with the rest of the information. I followed all the suggestions you outlined and that did indeed correct the problem; the file names and directory name are no longer part of the output. However, I am having another problem; the script is repeating each line of information that is found. As it checks each file in the directory it is writing the information in the LINEITEM and LINDESC fields multiple times. I have tried initializing the ITEM and DESC variables after each one gets written to the file, but that did not resolve the problem. Could the problem stem from the fact that I have the file name repeated in the second FOR loop? (Not sure if there is a syntax for a FOR loop without the directory path and file name).
    480 pointsBadges:
    report
  • ITKE
    [...] 4. A user goes for broke with DOS looping. [...]
    0 pointsBadges:
    report
  • jutpro
    I finally got this batch script to work. I got rid of the 2nd "For" loop and Use spaces to separate the 2 search strings. I also made the token=* Then I use several IF statements to evaluate the value of the ITEM variable. See example below.
    for /f "tokens=*" %%a in ('type "%Location%"^|findstr "LINEITEM LINDESC"') do (
    set ITEM=%%a
    if "!ITEM:~0,8!"=="LINEITEM" set PROD=%%a
    if "!ITEM:~0,7!"=="LINDESC" (set DESC=%%a) & !PROD:~38,12! !DESC:~8,30! >> C:TempITEMLIST.txt
    480 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following