问题
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? :)
回答1:
Below is a script that can be called with QUOTED parameters For:
- 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 ]
来源:https://stackoverflow.com/questions/64089609/including-excluding-files-from-batch-script