> For the complete documentation index, see [llms.txt](https://docs.gpmautomate.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/variables/count.md).

# 计数

计数是用于计算一维数组（或数据列表）中元素数量的操作，并返回一个数值。这个操作在您需要在处理之前检查数据大小或为循环设置限制时非常有用。

常见的实际应用案例包括：

#### 1. 计算文本文件的行数

当您需要知道一个文本文件（包含账户列表、代理、文章等）总共有多少行时：

* 做法：使用“文件读取所有行”操作将文件中的所有行读取到一个变量列表中，然后使用“计数”操作来计算该列表。返回的值就是文件的总行数。

#### 实际示例：计算文件 `D:\2.txt` 中 Facebook 链接的数量

假设您有一个文件存储了 Facebook 个人资料链接列表，路径为 `D:\2.txt`，内容包含 9 行，如下所示：

```
https://www.facebook.com/nguyenvana
https://www.facebook.com/leminhc
...
https://www.facebook.com/demo.account99
```

为了计算总行数并提取文件中的每个链接，脚本将通过 2 个步骤进行配置：

* 步骤 1：使用“文件读取所有行”操作读取文件 `D:\2.txt`。返回的结果将被保存到一个名为 `$content` 的数组（列表）中。此时系统自动理解：
  * `$content[0]` = `"https://www.facebook.com/nguyenvana"`（第 1 行）
  * `$content[1]` = `"https://www.facebook.com/leminhc"`（第 2 行）
  * ...
  * `$content[8]` = `"https://www.facebook.com/demo.account99"`（第 9 行）
* 步骤 2：使用“计数”操作传入刚刚读取的数组 `$content`，并将输出变量命名为 `$lineCount`。

运行完成后，变量 `$lineCount` 将接收值 `9`。您可以立即使用这个变量 `$lineCount` 作为 For 循环的结束参数，以便自动逐个打开每个 Facebook 个人资料进行互动。

#### 2. 计算文件夹中的文件数量

当您需要检查计算机上某个文件夹中有多少张图片、视频或文档时：

* 做法：使用“文件夹获取文件列表”操作获取所有文件的列表，然后使用“计数”操作准确知道该文件夹中包含多少个文件。

#### 实际示例：计算文件夹 `D:\bds\cat bi\` 中的房地产图片数量

假设您有一个文件夹，存储了位于路径 `D:\bds\cat bi\` 的房地产照片，包含 3 个简短命名的图片文件，如下所示：

* `1.jpg`
* `2.jpg`
* `3.jpg`

为了计算有多少个图片文件用于自动发布，脚本将通过 2 个步骤进行配置：

* 步骤 1：使用“文件夹获取文件列表”操作指向文件夹路径 `D:\bds\cat bi\`。系统将扫描该文件夹并返回一个包含所有文件路径的数组，保存到名为 `$fileList` 的变量中。此时数组数据将自动赋值为：
  * `$fileList[0]` = `"D:\bds\cat bi\1.jpg"`
  * `$fileList[1]` = `"D:\bds\cat bi\2.jpg"`
  * `$fileList[2]` = `"D:\bds\cat bi\3.jpg"`
* 步骤 2：使用“计数”操作传入刚刚获取的数组 `$fileList`，并将输出变量命名为 `$totalFiles`。

运行完成后，变量 `$totalFiles` 将接收值 `3`。您可以使用这个结果来运行条件语句（例如：如果文件夹中有超过 0 个文件，则执行上传图片到 Facebook/网站的操作）。

#### 3. 计算分割字符串后的元素数量

当您有一个长文本字符串，包含许多信息，并且用特殊字符分隔，想知道分割后会得到多少个元素时：

* 做法：在使用“分割文本”操作根据字符 `|` 分割字符串后，您让“计数”操作处理刚刚分割的列表。此时返回的计数结果将是 `6`。

#### 实际示例：计算使用分割文本后的字符串元素数量

假设您有一个配置文件或一行数据，包含账户信息，格式为用竖线 `|` 分隔的结构，如下所示：

```
thanhnguyen|Pass1234|2FAXYZ|103.83.77.15:9898
```

为了计算这个字符串有多少个组成信息，以便检查账户的有效性，脚本将通过 2 个步骤进行配置：

* 步骤 1：使用“分割文本”操作根据分隔符 `|` 分割上述字符串。返回的结果将保存到一个名为 `$accountData` 的数组中。此时系统将自动分割数据为：
  * `$accountData[0]` = `"thanhnguyen"`（账户）
  * `$accountData[1]` = `"Pass1234"`（密码）
  * `$accountData[2]` = `"2FAXYZ"`（2FA 码）
  * `$accountData[3]` = `"103.83.77.15:9898"`（代理）
* 步骤 2：使用“计数”操作传入刚刚分割的数组 `$accountData`，并将输出变量命名为 `$infoCount`。

运行完成后，变量 `$infoCount` 将接收值 `4`。您可以使用这个结果结合 If 语句来检查（例如：如果 `$infoCount = 4`，即账户信息结构完整，则继续执行登录脚本，否则跳过以避免错误）。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.gpmautomate.com/automate-zh/cao-zuo-zhi-nan/variables/count.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
