Concatenate String and Int to form file name prefix

空扰寡人 提交于 2020-01-23 13:24:07

问题


I am working with PowerShell to create a renaming script for a number of files in a directory.

Two questions here:

I have a string variable $strPrefix = "ACV-100-" and an integer counter $intInc = 000001 and I wish to increment the counter $intInc 1 -> 2 and then concatenate the two and store it in a variable $strCPrefix in the following format: ACV-100-000002.

I believe the $intInc will need to be cast in order to convert it once incrementing is complete but I am unsure how to do this.

Secondly, I have found that the script will display 000001 as 1, 000101 as 101 and so on... I need the full 6 digits to be displayed as this will form a file name. How do I keep or pad the numbers before I process the concatenation?


回答1:


$intInc = 1

$strCprefix = $strprefix + "{0:000000}" -f $intInc # give ACV-100-000001

hope can help




回答2:


This should do it:

$intInc = 1
...
$filename =  $strPrefix + ('0' * (6 - $intInc.ToSTring().Length)) + ($intInc++).ToString()


来源:https://stackoverflow.com/questions/7037710/concatenate-string-and-int-to-form-file-name-prefix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!