gpt4 book ai didi

windows - 解析和重命名文本文件

转载 作者:可可西里 更新时间:2023-11-01 10:04:39 25 4
gpt4 key购买 nike

我需要将所有这些文件重命名为第 3 行的 6 个字符的部件号 (306391)。

目前我有:

setlocal enabledelayedexpansion 

set first=1

for /f "skip=3 delims= " %%a in (Name.txt) do (

if !first! ==1 (

set first=0

echo %%a > out.txt
ren Name.txt %%a.txt

)
)

找到 6 位零件号并将文件重命名为正确的名称。但是,如果我使用 *.txt 而不是 .txt 文件的实际名称,则会中断。我需要它来处理目录中的所有 .txt 文件。

最佳答案

用另一个 for 循环围绕您的 for/f 循环,然后在您的 ren 命令中引用外部循环变量。您还可以通过使用 if defined 进行 bool 检查来消除延迟扩展的需要。我在这里和那里进行了其他调整。只要询问您是否需要详细信息。

@echo off
setlocal

for %%I in (*.txt) do (

set first=

for /f "usebackq skip=3" %%a in ("%%~fI") do (

if not defined first (

set first=1

echo %%~nxI ^> %%a.txt
rem // uncomment this when you're satisfied that it works correctly
rem // ren "%%~fI" "%%a.txt"

)
)
)

关于windows - 解析和重命名文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36160327/

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