iT邦幫忙

0

BAT批次檔如何判斷字串?

  • 分享至 

  • xImage

請問各位大神, 我想用bat讀取txt檔案內的特地字串並且存成變數去判斷做更新,
爬文看了一些分享案例, 但是一直沒辦法成功做判斷, 執行的結果都會一直跳到Echo Not Match, 該怎麼改才能夠成功判斷呢?

FOR /F "delims=:_ tokens=2" %%a in ('findstr /p /i /c:"SSD" 2TB.txt') do set model=%%a
ECHO %model%
FOR /F "delims=:, tokens=2" %%b in ('findstr /p /i /c:"2.04" 2TB.txt') do set size=%%b
ECHO %size%

if "%model%" == "SSD" goto update
goto Error

:update
if "%size%" == "128" goto 128GB
if "%size%" == "256" goto 256GB
if "%size%" == "512" goto 512GB
if "%size%" == "1.02" goto 1TB
if "%size%" == "2" goto 2TB
goto Error

:128GB
echo 128GB Firmware Updating...

:256GB
echo 256GB Firmware Updating...

:512GB
echo 512GB Firmware Updating...

:1TB
echo 1TB Firmware Updating...

:2TB
echo 2TB Firmware Updating...

:Error
ECHO Not Match

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
1
網世儒凮
iT邦新手 4 級 ‧ 2023-09-14 17:42:19

請問你要讀取的 TXT 檔案內容大概長甚麼樣子 ??
要貼出來才知道問題是甚麼~~

wa177 iT邦新手 5 級 ‧ 2023-09-14 17:44:29 檢舉

內容是這些訊息, 感謝

smartctl 7.3 2022-02-28 r5338 [x86_64-w64-mingw32-w10-1809] (sf-7.3-1)
Copyright (C) 2002-22, Bruce Allen, Christian Franke, www.smartmontools.org

=== START OF INFORMATION SECTION ===
Device Model: SSD_ISSS316-002TCTB5
Serial Number: 2N1329A1D9JL
LU WWN Device Id: 5 707c18 100ca00cd
Firmware Version: A221125a
User Capacity: 2,048,408,248,320 bytes [2.04 TB]
Sector Size: 512 bytes logical/physical
Rotation Rate: Solid State Device
Form Factor: 2.5 inches
TRIM Command: Available, deterministic, zeroed
Device is: Not in smartctl database 7.3/5319
ATA Version is: ACS-4 (minor revision not indicated)
SATA Version is: SATA 3.2, 6.0 Gb/s (current: 6.0 Gb/s)
Local Time is: Thu Sep 14 13:57:44 2023 TST
SMART support is: Available - device has SMART capability.
SMART support is: Enabled

0
wdchiang
iT邦新手 2 級 ‧ 2023-09-15 09:49:10

前面多了個空白,去掉就可以

set model=!model:~1!
set size=!size:~1!

看更多先前的回應...收起先前的回應...
wa177 iT邦新手 5 級 ‧ 2023-09-15 13:58:54 檢舉

do set model=%%a
do set size=%%b

請問是這裡嗎?為啥=後面變成!model:~1!呢?我按照您的方式改, 也無法成功

wdchiang iT邦新手 2 級 ‧ 2023-09-15 14:22:53 檢舉

你原來的 script 執行後,前面都會多一個空白,只要把那個空白拿掉,就會正常。

wdchiang iT邦新手 2 級 ‧ 2023-09-15 14:23:44 檢舉

@echo off
setlocal enabledelayedexpansion

FOR /F "delims=:_ tokens=2" %%a in ('findstr /i "SSD" 2TB.txt') do set model=%%a
FOR /F "delims=:, tokens=2" %%b in ('findstr /i "2.04" 2TB.txt') do set size=%%b

set model=!model:~1!
set size=!size:~1!

ECHO Model: !model!
ECHO Size: !size!

if "!model!" == "SSD" (
goto update
) else (
goto Error
)

:update
if "!size!" == "128" (
goto 128GB
) else if "!size!" == "256" (
goto 256GB
) else if "!size!" == "512" (
goto 512GB
) else if "!size!" == "1.02" (
goto 1TB
) else if "!size!" == "2" (
goto 2TB
) else (
goto Error
)

:128GB
echo 128GB Firmware Updating...
goto end

:256GB
echo 256GB Firmware Updating...
goto end

:512GB
echo 512GB Firmware Updating...
goto end

:1TB
echo 1TB Firmware Updating...
goto end

:2TB
echo 2TB Firmware Updating...
goto end

:Error
echo Not Match
goto end

:end

wdchiang iT邦新手 2 級 ‧ 2023-09-15 14:27:12 檢舉

還有多加這一行

setlocal enabledelayedexpansion

wa177 iT邦新手 5 級 ‧ 2023-09-15 15:08:19 檢舉

我知道原因了, 感謝大大的回應, 已改成功

0
PaddyPang
iT邦新手 5 級 ‧ 2023-09-15 10:23:34
package main

import (
	"fmt"
	"io/ioutil"
	"regexp"
	"strconv"
	"strings"
)

func calculateDiskSize(bytes uint64) string {
	const (
		KB = 1000
		MB = KB * 1000
		GB = MB * 1000
		TB = GB * 1000
	)

	var size string

	switch {
	case bytes >= TB:
		size = fmt.Sprintf("%dT", bytes/TB)
	case bytes >= GB:
		size = fmt.Sprintf("%dG", bytes/GB)
	case bytes >= MB:
		size = fmt.Sprintf("%dM", bytes/MB)
	case bytes >= KB:
		size = fmt.Sprintf("%dK", bytes/KB)
	default:
		size = fmt.Sprintf("%dB", bytes)
	}

	return size
}

func extractValue(output, key string) string {
	re := regexp.MustCompile(key + `(.+)`)
	match := re.FindStringSubmatch(output)
	if len(match) == 2 {
		return strings.TrimSpace(match[1])
	}
	return ""
}

func extractUserCapacity(userCapacityStr string) (int, error) {
	re := regexp.MustCompile(`([\d,]+) bytes`)
	match := re.FindStringSubmatch(userCapacityStr)
	if len(match) != 2 {
		return 0, fmt.Errorf("user capacity not found")
	}
	capacityStr := strings.ReplaceAll(match[1], ",", "")
	capacity, err := strconv.Atoi(capacityStr)
	if err != nil {
		return 0, err
	}
	return capacity, nil
}

func updateTrigger() {
	fmt.Println("Update Triggered by: SSD")
}

func capacityTrigger(capacity string) {
	fmt.Printf("User Capacity Triggered by: %s\n", capacity)
}

func main() {
	// Read file
	content, err := ioutil.ReadFile("1.txt")
	if err != nil {
		fmt.Println("Cannot read:", err)
		return
	}

	// Extract Device Model
	deviceModel := extractValue(string(content), "Device Model:")
	if strings.Contains(deviceModel, "SSD") {
		updateTrigger()
	}

	// Extract User Capacity
	userCapacityStr := extractValue(string(content), "User Capacity:")
	userCapacity, err := extractUserCapacity(userCapacityStr)

	if err == nil {
		capacity := calculateDiskSize(uint64(userCapacity))
		capacityTrigger(capacity)
	}
}

使用Go呢

PaddyPang iT邦新手 5 級 ‧ 2023-09-15 10:25:05 檢舉

返回結果如下:

Update Triggered by: SSD
User Capacity Triggered by: 2T
wa177 iT邦新手 5 級 ‧ 2023-09-15 13:55:41 檢舉

感謝大大的回覆, Go我沒用過, 我研究看看

我要發表回答

立即登入回答