我已經嘗試過,以便在早上更好的部分進行排序。如何將前導零的數字填充到固定長度?
它實際上是同一個問題,因爲這一個從2013年開始,到沒有人回答說:
batch script with FOR does not work
我會盡我所能來格式化代碼,這樣就很容易跟進,也許我會得到一個答案......
我從我們的服務檯售票系統做一個歸檔項目。 爲了分類目的,這些文件將包含票號和日期。
然而,票號的長度變化。爲了解決這個問題,所有票號都是6位數字,較短的數字用前面的零填充(即1234變成0)。
我可以拿到票號,但我需要找到它的長度知道有多少個零添加。
這工作:
echo off
set my_str=12345
set length=0
:Loop
if defined my_str (
set my_str=%my_str:~1%
set /A "length+=1"
goto Loop)
echo the string is %length% characters long
不過,我得到一幫票號的列表。 於是,我通過這個閱讀:
set statements don't appear to work in my batch file
,結果迷失了閱讀本:
http://www.computing.net/howtos/show/batch-script-variable-expansion-win200-and-up/248.html
我試過這樣:
setlocal enabledelayedexpansion
echo off
for /f %%a IN (test.txt) do (
set my_str=%%a
set length=0
:Loop
if defined my_str (
set my_str=!my_str:~1!
set /A "length+=1"
echo %length%
goto Loop)
echo the string is %length% characters long
)
但它只能讀取第一個test.txt行
爲什麼FOR/F循環失敗?
我該如何讓它起作用?
在FOR循環中,你總是會遇到困難。 – RGuggisberg