赢bat文件:如何前导零添加到一个变量在for循环? [英] Win bat file: How to add leading zeros to a variable in a for loop?

查看:151
本文介绍了赢bat文件:如何前导零添加到一个变量在for循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很简单,我想......我需要通过添加前导零的循环索引变量(%%一)项以获得一个可用的变量。

Very simple, I guess... I need to get a usable variable by adding leading zeros to the loop index variable (%%i) below.

@echo off
for /L %%i in (1, 1, 5) do (
     echo %%i

     rem     How to create a variable j here as a 
     rem     result of adding leading zeros to %%i? (001, 002, 003 etc.)

)
pause

如何?
我试过以下,但我不能让值超出了我%%在...

How? I've tried the following, but I can't get the value out of the %%i variable inte the var_ at a...

@echo off & setlocal enableextensions
for /L %%i in (1, 1, 5) do (
     echo %%i
     set var_=00000%%i
     set var_=%var_:~-5%
     echo %var_%
)
pause


推荐答案

的正常方式是preFIX用零的字符串,然后取字符的所需计数从右侧

The normal way is to prefix your string with zeros and then take the desired count of characters from the right side.

@echo off
set count=5
setlocal EnableDelayedExpansion
for /L %%i in (1, 1, %count%) do (
     set "formattedValue=000000%%i"
     echo !formattedValue:~-3!
)

此输出

001
002
003
004
005

这篇关于赢bat文件:如何前导零添加到一个变量在for循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆