批处理脚本从设置位置复制文件

时间:2018-04-23 14:56:24

标签: batch-file search copy criteria

我正在尝试根据特定条件获取此bat文件以复制文件。

我错过了什么才能让它发挥作用?

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

set "PATTERN=*.nwd"
set "SOURCE=C:\NWD\"
set "TARGET=C:\NWM\"
set "STRING1=191058"

pushd "%SOURCE%" && (
    for /F "delims=" %%F in ('findstr /S /M /I /R /C:"\<%STRING1%\>" "%PATTERN%"') do (
            ECHO copy "%%F" "%TARGET%"\"%%~nxE"
        )
    )
    popd
)

endlocal
exit /B

1 个答案:

答案 0 :(得分:1)

要修复代码,您可以执行以下操作:

@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION

set "PATTERN=*.nwd"
set "SOURCE=C:\NWD\"
set "TARGET=C:\NWM\"
set "STRING1=191058"

pushd "%SOURCE%" && (
    for /F "delims=" %%F in ('findstr /S /M /I /R /C:"\<%STRING1%\>" "%PATTERN%"') do (
        ECHO copy "%%F" "%TARGET%%%~nxF"
    )
)
popd

endlocal
exit /B

我会改变它:

@Echo Off

Set "PATTERN=*.nwd"
Set "SOURCE=C:\NWD"
Set "TARGET=C:\NWM"
Set "STRING1=191058"

CD /D "%SOURCE%" 2>Nul || Exit /B

For /F "Delims=" %%A In ('FindStr /SM "\<%STRING1%\>" "%PATTERN%"') Do (
    Echo Copy /Y "%%A" "%TARGET%\%%~nxA")

您可能希望添加暂停,以便在脚本关闭之前查看Echo