问题
I've been searching around for a while and tried mixed various different answers together to make something that works but have been unsuccessful. I thought the solution might be to parse the DIR to a text file and then read each line for the specific string in a loop, which then robocopys it to the different destination based on the result found.
Folders have the same name but just with a different version number.
Any ideas/help would be very much appreciated.
Example:
- Network/Share/1st Website name unique_Sting_v1 > c:\website backups\1st website
Network/Share/1st Website name unique_Sting_v2 > c:\website backups\1st website
Network/Share/2nd Website name unique_Sting_v1 > c:\website backups\2nd website
Network/Share/2nd Website name unique_Sting_v2.5 > c:\website backups\2nd website
set vidx=0 >&2 for /F "tokens*" %%A in (get_dirs.txt) do ( SET /A vidx=!vidx! + 1 SET var!vidx!=%%A )
回答1:
It's not entirely clear what you're asking, but I think this might help. It seems that you want to test a folder name and determine whether it contains string A. If true, move it to backup folder A. Else if it contains string B, move it to backup folder B. And perhaps so on.
There are a few different ways to test whether a variable contains a string. One of the most efficient ways is to use string substitution. Here's a quick example.
rem // loop through all directories
for /d %%I in (*) do (
rem // set a variable
set "dir=%%~I"
rem // now comes the fun part
setlocal enabledelayedexpansion
if not "!dir!"=="!dir:1st Website=!" (
rem // 1st website match!
xcopy "%%~I" "destination1"
) else if not "!dir!"=="!dir:2nd Webiste=!" (
rem // 2nd website match!
xcopy "%%~I" "destination2"
)
endlocal
)
The way this works is thus. In the first if
statement, you are taking the value of !dir!
and, wherever there's an occurrence of 1st Website
(case-insensitive), replace it with nothing. So if untouched dir
no longer equals dir
with bits removed, a substring match was found.
There are other ways to test for substring matches as well. echo %%~I | find /i "Website 1" >NUL && ( match found ) || ( no match )
would be another common one. But using built-in cmd-interpreter functionality is usually more efficient than calling an executable -- in this case, find.exe
(or findstr.exe
, as you'll also commonly see).
来源:https://stackoverflow.com/questions/28820518/backup-batch-file-which-parses-dir-for-a-certain-string-and-send-it-to-a-differe