问题
I made a script that makes 4 folders Images, Documents, Programs and Other. The script looks into selected directory and copies the files accordingly to the folders, but my problem is how to restrict the files that are copied to the specified folders to not copy into "Other" folder also?
Here is the part of the script:
:run1
mkdir %var2%\%1\Images
copy "%var%\*.jpg" "%var2%\%1\Images"
mkdir %var2%\%1\Documents
copy "%var%\*.docx" "%var2%\%1\Documents"
mkdir %var2%\%1\Programs
copy "%var%\*.exe" "%var2%\%1\Programs"
mkdir %var2%\%1\Other
copy "%var%" "%var2%\%1\Other"
goto complete
回答1:
This kind of thing should work:
:run1
pushd "%var2%"
for %%a in (Images Documents Programs Other) do mkdir "%~1\%%~a" 2>nul
for %%a in (jpg png bmp tif gif) do (
copy "*.%%a" "%~1\Images"
attrib +h "*.%%a"
)
for %%a in (doc docx txt pdf) do (
copy "*.%%a" "%~1\Documents"
attrib +h "*.%%a"
)
for %%a in (exe com bat cmd) do (
copy "*.%%a" "%~1\Programs"
attrib +h "*.%%a"
)
copy "*.*" "%~1\Other"
attrib -h "*.*"
popd
goto complete
回答2:
This kind of tasks (and the backup processes) was the reason for the creation of the archive attribute of files.
:run1
attrib +a "%var%\*"
xcopy /i /m "%var%\*.jpg" "%var2%\%1\Images"
xcopy /i /m "%var%\*.docx" "%var2%\%1\Documents"
xcopy /i /m "%var%\*.exe" "%var2%\%1\Programs"
xcopy /i /m "%var%\*" "%var2%\%1\Other"
goto complete
The attrib +a will set the archive attribute. The /i switch of xcopy will handle directory creation and the /m is asking xcopy to only copy files with the archive attribute set and, when the file is copied, remove the attribute. That way, the last command will only copy files that were not copied.
来源:https://stackoverflow.com/questions/23170821/batch-script-restricting-duplicate-copies