包含/排除批处理脚本中的文件 [英] Including/excluding files from batch script
本文介绍了包含/排除批处理脚本中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的用于从目录复制最近创建的文件的批处理脚本中,我想包括和排除特定的文件名,但是不确定如何做.
In the following batch script for copying the most recently created files from a directory, I would like to include and exclude specific file names, but not sure how to.
@echo off
set source="C:\FolderA\FolderB"
set target="C:\FolderC\FolderD"
FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END1
:END1
...
set source="C:\FolderE\FolderF" <--- From this directory I would also want to copy, in addition to the most recent file created, the most recent file created that has the word "error" in the file name.
set target="C:\FolderC\FolderD"
FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END2
:END2
…
set source="C:\FolderG\FolderH" ***<---From this folder i copy the most recent created file, but would like to exclude the files that contains the word "Graphical" in the file name.***
set target="C:\FolderC\FolderD"
FOR /F "delims=" %%I IN ('DIR %source%\*.* /A:-D /O:-D /B') DO COPY %source%\"%%I" %target% & echo %%I & GOTO :END3
:END3
请问有人可以帮助我吗?:)
Is there anyone who could help me please? :)
推荐答案
下面是一个可以使用QUOTED参数调用的脚本,用于:
Below is a script that can be called with QUOTED parameters For:
- Arg 1:Dir开关和搜索模式
- Arg 2:Findstr匹配的字符串
- Arg 3:条件运算符
||
或&
来执行以下命令 - Arg 4:在
Dir |的输出上执行的初始命令语法|Findstr
- [可选]:精简版5:尾随命令语法/字符串,可在
Dir |的输出上执行或附加到|Findstr
- Arg 1: Dir Switches and Search Pattern
- Arg 2: The String for Findstr to Match
- Arg 3: Conditional Operator
||
or&&
to execute the following command - Arg 4: Initalial command syntax to execute on Output of
Dir | Findstr
- [Optional]: Arg 5: trailing command syntax / string to execute on or append to Output of
Dir | Findstr
@Echo Off
Set "Param3="
Set "Usage=Echo/%~n0 ^<Pattern^> ^<Match String^> ^<[^|^|]^|[^&^&]^> ^<"Command"^> [String / Command]&Echo/Args 4 or 5 can reference %%G and it's modifiers to access output.&Exit /B 0"
IF "%~4"=="" (%Usage%)
IF "%~3"=="&&" (Set Param3=_)Else IF "%~3"=="||" (Set Param3=_)
If not Defined Param3 (Echo/Invald Param for Operator ^<[^|^|]^|[^&^&]^> & %Usage%)
If Not "%~5"=="" For /F "Delims=" %%G in ('Dir %~1 ^|^| ^(Echo/Invalid Dir Options^&Exit /b 0^)')Do Echo/"%%~G" | Findstr /LIC:"%~2" > Nul 2> Nul %~3 ( %~4 %%~G %~5 2> Nul || Echo/ Error in Command Usage: "%~4" %%G "%~5" )
If "%~5"=="" For /F "Delims=" %%G in ('Dir %~1 ^|^| ^(Echo/Invalid Dir Options^&Exit /b 0^)')Do Echo/"%%~G" | Findstr /LIC:"%~2" > Nul 2> Nul %~3 ( %~4 %%~G 2> Nul || Echo/ Error in Command Usage: "%~4" %%G )
在命令行中的用法示例:
An example of a Usage from the command line:
C:\Users\tcdou\Desktop $> $MATCH "/A:-D /B *.bat" "$" "&&" "Echo/[%~tG] " " [%~zG ]"
[05/09/2020 09:35 PM] $Array.bat [8940 ]
[26/09/2020 12:13 PM] $base.bat [4930 ]
[12/09/2020 04:11 PM] $Case.bat [748 ]
[23/09/2020 11:50 AM] $compare.bat [1737 ]
[12/09/2020 03:10 PM] $find.bat [334 ]
[24/09/2020 12:28 PM] $game.bat [4161 ]
[15/09/2020 02:41 AM] $Group.bat [463 ]
[26/09/2020 02:36 PM] $in.bat [511 ]
[25/09/2020 04:58 AM] $LA.bat [2098 ]
[23/09/2020 10:39 AM] $label.bat [156 ]
[26/09/2020 11:55 AM] $list.bat [1554 ]
[28/09/2020 12:48 AM] $MATCH.bat [744 ]
[23/09/2020 03:55 AM] $mc2.bat [1697 ]
[07/09/2020 01:35 AM] $nArray.bat [1581 ]
[07/09/2020 12:55 AM] $oldlost.bat [1199 ]
[12/09/2020 12:35 AM] $remstr.bat [572 ]
[12/09/2020 12:23 AM] $remstr2.bat [682 ]
[12/09/2020 03:38 AM] $RGN.bat [1277 ]
[12/09/2020 01:36 PM] $SSM.bat [850 ]
[10/09/2020 06:37 AM] $Str.bat [515 ]
[22/09/2020 01:37 AM] $VT.bat [1194 ]
[06/09/2020 11:59 PM] test$.bat [1184 ]
这篇关于包含/排除批处理脚本中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文