Copy a set of files into specific directories based on filename

拈花ヽ惹草 提交于 2019-12-11 23:54:16

问题


How can I copy series of files located in one folder into a specified folder based on the each file name in Windows?

For example the files:

ProviderA.pdf copied to folder-> \ProviderA\report\

ProviderB.pdf copied to folder-> \ProviderB\report\

ProviderC.pdf copied to folder-> \ProviderC\report\

I would like to traverse the list of files and from the file name (minus the extension) be able to copy each file to the folder specified by the file name.

Thank you in advance!


回答1:


dir /b *.pdf >foo
for /f "tokens=1 delims=. " %i in (foo) do copy %i.pdf %i\report
del foo

If the directories don't already exist, substitute this command:

for /f "tokens=1 delims=. " %i in (foo) do md %i\report&copy %i.pdf %i\report



回答2:


Simplest way is to loop through files in current folder, create a folder using the filename (without the extension) if it doesn't already exist, then copy file into the new folder.

for %%G in (*.pdf) do (
    if not exist %%~nG md %%~nG
    copy %%G %%~nG
)


来源:https://stackoverflow.com/questions/7651406/copy-a-set-of-files-into-specific-directories-based-on-filename

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