簡體   English   中英

批處理文件可從文本文件中讀取“ N”個字符

[英]Batch File to read 'N' characters from a text file

我已經在網上搜索了此代碼,發現有很多代碼可用於從文本中檢索整行或將文本替換為另一行,但不是我想要的。

將For循環與標記一起使用將返回用空格分隔的集合(單詞)。

我只想從行中拉出幾個字符。

例如: 12345qwerty67890

如果在文本文件中打開,我只想拉'12345'並將其分配給變量。

任何幫助是極大的贊賞。

設置“ HELP SET然后嘗試以下操作以開始使用

@echo off
setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (sample.txt) do (
  set line=%%a
  set chars=!line:~0,5!
  echo !chars! --- !line!
)

在命令提示符下,執行help set 在這里,您會找到比您想知道的更多有關set命令的信息。 您感興趣的部分說:

May also specify substrings for an expansion.

    %PATH:~10,5%

would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result.  If the length is not specified, then it defaults to the
remainder of the variable value.  If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.

當然,為了將這種東西與for循環變量一起使用,您需要首先熟悉在Windows NT批處理文件中擴展變量的非常特殊的方式。 如果您對此有疑問,請告訴我,我可以添加更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM