batch-file - how to use bat file to move files from subdirectories to one folder
问题描述
I'm trying to use a .bat file to go into a folder, and take all photos within it and its subfolders, and place them all into another directory. I know how to copy the folder exactly, with all subfolders remaining in place when copied with
@ECHO OFF
XCOPY E:\FromFolderNameX C:\toFolderNameY /m /y
but I only want all of the photos to be in one folder in the end, no subfolders. Can this be done with a batch file?
解决方案
I am assuming that you want to copy (not move) the photos from the subtree starting at
E:\FromFolderNameX
into the directoryC:\toFolderNameY
.I am assuming that by "photos" you mean
.jpg
files.The one-line interactive command is
for /r E:\FromFolderNameX %p in (*.jpg) do copy /y "%~p" C:\ToFolderNameY
If instead of JPG files you want to copy all files, just replace
*.jpg
with*
.If instead of an interactive one liner you want a batch file, the core of the batch file would be
for /r "%~1" %%p in (*.jpg) do copy "%%~p" "%~2"
(%1
is the first positional argument = the top of the subtree from where you want to copy the files. %2
is the second positional argument = the destination directory.)
In production, the batch file would probably check that the directories %1
and %2
exist and are really directories; and it should probably accept an optional third argument giving the pattern of the files to be copied.
Enter for /?
to read more about how for /r
works.
推荐阅读
- python - 对 DataFrame 进行排序,将重复项移至末尾
- filter - 嵌套 filter_time 过滤非序列月份
- reactjs - socket.io-client 在 ie 11 上导致语法错误 - Next js
- react-router - react route v4.1 除非刷新,否则无法显示组件,不使用 redux
- c# - 如何使用 C# 在 Slack 上共享 Extent 报告
- python - 有没有办法从py中的文本打印一行?
- reactjs - 尝试更改图层绘制属性但出现类型错误:无法读取未定义的属性“值”
- xml - 我如何继承模板门户 odoo 12
- javascript - 运行不安全 JavaScript 代码的最佳解决方案
- python - 我正在通过隐藏的 api 抓取 mysqltutorial 网站示例数据库表