0

My purpose of storing multiple strings in array is because I wrote a batch script to read text files. If specified word is detected inside the text file, it will store a string to an array. After go through multiple text files (it will be multiple strings in the array), I would like to echo out all the strings inside the array and email to specified person. After go through many post and all I saw was set myarray[0]=abc or the array already declared manually.

Here is my code :

@echo off
setlocal EnableDelayedExpansion
(
  for %%x in (
    C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\sucess.txt
  ) do for /f "usebackq delims=" %%a in ("%%~fx") do (
    set "str=%%a"
    set "part1=!str:~0,10!"
    set "part2=!str:~11,8!"
    set "part3=!str:~20,4!"
-------------------------------------------------------------------
    **I wanted to set an array here (Do not want manually set the string) **

    **If the "NoneFolder" word detected, store the string into array**
    if "!part1!" == "NoneFolder" (
    echo No Folder **<< Wanted to store this**
    )
    if "!part1!" == "Downloaded" (
    echo Downloaded 
    )
    ** If the "EmptyFiles" word detected, store string into array **
    if "!part1!" == "EmptyFiles" (
    echo No files **<< Wanted to store this**
    )
    ** Echo the array here **
------------------------------------------------------------------
  )
)>"C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\email.txt"
pause

Expected Output:

After for loop, the array should got No folder and No files.

FYI, this is a monitoring script that wrote to scan through the multiple text files. So I need it(array) to collect multiple strings and echo out and EMAIL to user.

P.s. Any posts regarding on how to email to a user to share =)? If there's any question please do ask me!

Edit(Additional): Tell you guys about my task

My task is to loop through the log files. While going through each line, if keyword detected it should email that whole line information to the user(i do not know how to hold the string and email all the stored string to user, Impossible each time keyword detected and it will send one email. Then my user will mad at me...) So I have the idea maybe I can do it on array. I totally can't think other way besides array =/.

Thanks for viewing, comments and answers. Please explain to me! Thanks!

  • Email.bat here: http://stackoverflow.com/questions/21432006/script-to-start-service-if-not-running-then-email-results/21435658#21435658 – foxidrive Apr 07 '14 at 08:32
  • Your task seems to be to check files for a string and echo something into a file depending on the result. This could be done in a simpler manner if you describe the task in your question, or open a new question. – foxidrive Apr 07 '14 at 08:34
  • @foxidrive Thanks for email.bat ! I will take a look of it. Yes, my task is to loop through the log files. While going through each line, if keyword detected it should email that whole line information to the user(i do not know how to hold the string and email all the stored string to user). So I have the idea maybe I can do it on array. I totally can't think other way besides array =/. Gosh~ – Thor_that_new_in_programming Apr 07 '14 at 08:51

2 Answers2

1

As discussed in this question Arrays, linked lists and other data structures in cmd.exe (batch) script Windows batch files don't support complex data types like arrays. However, you are free to use variable names that resemble array accessors like var[0].

In your case why don't you simply set var[0], var[1], var[2] as required for each case and output of all of them? For more flexibility you could introduce a counter variable and use it like set /A i=i+1, set var[!i!]=abc. This answer explains the approach in greater detail: https://stackoverflow.com/a/10167990/1059776.

Community
  • 1
  • 1
Paul B.
  • 2,394
  • 27
  • 47
  • Hi Paul, I got what you mean. Unfortunately, the text files contain unique key word that need to email to the user. That's why I would like to store into array. But, you stated that windows batch files do not support complex data types. IS OK! I will look through the post! Thanks for the explanation! – Thor_that_new_in_programming Apr 07 '14 at 07:59
1

This will put the lines containing EmptyFiles or NoneFolder into the text file.

@echo off
del "C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\email.txt" 2>nul

for /f "usebackq delims=" %%a in ("C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\sucess.txt") do (
   findstr "EmptyFiles NoneFolder" "%%a" >>"C:\Users\310152922\Desktop\Mohit_Task\AutomatedScript\COB\email.txt"
)
foxidrive
  • 40,353
  • 10
  • 53
  • 68