循环浏览文件夹中的文件,替换内容并另存为新名称

时间:2019-07-31 15:25:56

标签: batch-file

我需要遍历文件文件夹中以.edi结尾的结尾,替换内容中的字符,然后将文件末尾保存为“ _updated”。

例如

C:/Test/FileName.edi用'替换文件中的^并将文件保存到C:/Test/Output/FileName_Updated.edi

我尝试了以下代码,直到保存文件名部分为止,它一直有效,我在某个地方感到困惑,通常不编写批处理脚本:

@echo off
setlocal enabledelayedexpansion

for %%f in (C:\Test\*.edi) do (


    set "input=C:\Test\"
    SET "output=C:\Test\Output\"

    for %%a in (%%f) do (
        set "output=%output%%%~na_update.%%~xa"
    )

    (for /f "delims=" %%i in (%%f) do (
        set "line=%%i"
        setlocal enabledelayedexpansion
        set "line=!line:'=^!"
        echo(!line!
        endlocal
    )))>> %output%


)

2 个答案:

答案 0 :(得分:0)

在aschipfl的帮助下找到了答案

id     Values                    DateCol
 1      132                       01/20/2019          
 2      22                        01/20/2019
 3      33                        01/20/2019
 4     01/20/2019                 
 5      43                        02/21/2019  
 6      54                        02/21/2019  
 7      62                        02/21/2019  
 8     02/21/2019

答案 1 :(得分:0)

@echo off
setlocal enabledelayedexpansion

for %%f in (C:\Test\*.edi) do (


    set "input=C:\Test\"
    SET "output=C:\Test\Output\"

    (for /f "delims=" %%i in (%%f) do (
        set "line=%%i"
        rem setlocal enabledelayedexpansion
        set "line=!line:'=^!"
        echo(!line!
        rem endlocal
    )))>> "%output%%%~nf_update.%%~xf"


)

由于您正在循环中更改output的值,因此重定向应该是!output!而不是%output%-更改后的output的值,而不是该值外部 output循环时for的值。

由于%%f包含文件名,因此无需重新解析它。内联构造输出文件名更容易

由于delayedexpansion在过程开始时已被调用,因此无需重新调用它并关闭新的调用。我已经删除了这些行,因为可能是您正在提供缩写代码。