Windows Batch file to copy certain file types from subdirectories to one folder with rename

余生颓废 提交于 2019-12-11 02:25:22

问题


I had tried to make a batch script that copies all *.mpg files located in G:(random named subfolders here)\000.mpg to E:\PVR.

for /R g:\ %%f in (*.mpg) do copy %%f E:\PVR\

the problem is that source file names are the same, while they are different files with same name in all subfolders. the script overwrites the previous file and so I only have the last file after batch copy. please help me rename copied files with a counter or something.


回答1:


This should do the trick.

@echo off
setlocal disableDelayedExpansion
set "src=."
set "dest=\temp"
set mask=*.mpg
for /r "%src%" %%F in (%mask%) do (
  if exist "%dest%\%%~nxF" (call :copyDup "%%F") else copy "%%F" "%dest%" >nul
)
exit /b

:copyDup
set /a cnt=1
:loop
set /a cnt+=1
if exist "%dest%\%~n1(%cnt%)%~x1" goto :loop
copy %1 "%dest%\%~n1(%cnt%)%~x1" >nul
exit /b


来源:https://stackoverflow.com/questions/14089371/windows-batch-file-to-copy-certain-file-types-from-subdirectories-to-one-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!