gpt4 book ai didi

powershell - Padleft函数在PowerShell中带有序列号

转载 作者:行者123 更新时间:2023-12-02 23:20:52 25 4
gpt4 key购买 nike

我需要在txt文件的每一行的开头添加固定长度的递增编号。例如:

0001 aaaaaaaaaaa0002 bbbbbbbbbb...0010 gggggggggg

I created a .bat file to run a PowerShell which should solve the problem:

@echo off &setlocal
set "path=C:\Users..."
set "filein=%~1"
set "fileout=%filein%_out"

setlocal EnableDelayedExpansion

call %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe -Command "& {(Get-Content %path%\%filein%.txt) |ForEach-Object {$_.Insert(0,($id++).PadLeft(10,'0'))} |Set-Content %path%\%fileout%.txt}"

但这是行不通的。可能存在语法错误。

最佳答案

由于您添加了[batch-file]标签-这是一个纯批处理解决方案:

@echo off
setlocal enabledelayedexpansion
set count=0
(for /f "delims=" %%A in (input.txt) do (
set /a count+=1
set "index=00000!count!"
echo !index:~-4! %%A
))>Output.txt

关于powershell - Padleft函数在PowerShell中带有序列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51287669/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com