Introduction to batch files (for statements, arrays, deferred environment variables)

Motivation

"I want to know the caller of the XX function, so list it." When I was told to count

Since it was there, it seemed difficult to search 150 times using the IDE, so I managed to do it with a batch file. Don't ask why it's not bash or why you have to do that.

Created bat file and execution result

grepCallHierarchy.bat


@echo off

setlocal enabledelayedexpansion
set i=0
for /f %%a in (targetList.txt) do (
  set /a i+=1
  echo !i! %%a
  call set Arr[!i!]=%%a
)

pause

for /r %%a in (*.java) do (
  for /f "tokens=1,2,3 delims= " %%b in (%%a) do (
    for /l %%j in (1,1,%i%) do (
      if !Arr[%%j]!==%%c (
        echo %%a %%b %%c %%d
      )
    )
  )
)

targetList.txt


NantokaComponent
KantokaComponent

output

C:\path\to\file1.java private NantokaComponent nantokaComponent;
C:\path\to\file2.java private KantokaComponent kantokaComponent;

Impressions (learned)

for statement

It became a practice of various for sentences.

Array

call set Arr[!i!]=%%a

As I learned for the first time, batch files do not have arrays. It seems that they are just creating the variable ʻArr [1]ʻArr [2]...

Delay environment variable

setlocal enabledelayedexpansion

When you want to loop an array with subscripts, it seems that it will always be ʻArr [1] `(initial value) unless lazy evaluation is performed. It's difficult. ..

The part you want to lazy-evaluate is surrounded by a surprise mark like ʻArr [! I!] Instead of ʻArr [% i%]. I was surprised.

General comment

was fun. (Small feeling) While I was making it, I thought that there was also PowerShell. Next time I want to play with PowerShell.

Recommended Posts

Introduction to batch files (for statements, arrays, deferred environment variables)
Introduction to Programming for College Students: Variables
Introduction to kotlin for iOS developers ①-Environment construction
Introduction to JAR files
Introduction to Programming for College Students: Introduction
Note: Assigning and displaying values to multidimensional arrays, and practicing extended for statements
Introduction to java for the first time # 2
Introduction to kotlin for iOS developers ⑥-Kotlin creation
How to use environment variables in RubyOnRails
Introduction to kotlin for iOS developers ④-Type