[英]Windows batch script to name files after containing subfolder and copy to root
consider following folder structure: 考虑以下文件夹结构:
root
Folder1
file1.txt
file2.dat
Folder2
file3.doc
file4.pdf
rename.bat
I want to rename the files (using rename.bat) according to the name of the respective subdirectories, copy them to the root directory and delete the subfolders so that I get 我想根据各个子目录的名称来重命名文件(使用rename.bat),将它们复制到根目录并删除子文件夹,以便
root
Folder1.txt
Folder1.dat
Folder2.doc
Folder2.pdf
rename.bat
Actually I know this is possible (and actually with very few lines of code) since I already found the code somewhere some time ago. 实际上,我知道这是可能的(并且实际上只有很少的几行代码),因为我早在某个地方已经找到了代码。 Sadly I lost my scipt and am not able to find the code again now.
遗憾的是,我失去了密码,现在无法再次找到代码。
Regards, Eduard 问候,爱德华
Here I made this my self. 我在这里做了我自己。 I created a similar situation to yours and it worked fine for me.
我创建了一种与您类似的情况,对我来说效果很好。 However you have to specify the location to copy all the files to.
但是,您必须指定要将所有文件复制到的位置。
for /d %%a in (*) do (
cd %%a
for /r %%b in (*) do (
copy %%b C:\ [root] \%%a%%~xb
)
cd..
)
pause
Hopes this helps. 希望这会有所帮助。
Yours Mona. 莫娜
This uses Mona's code but handles long filenames. 这使用Mona的代码,但处理长文件名。 Test it on some sample folders.
在一些示例文件夹上对其进行测试。
Call it renfolder.bat or something as rename.bat uses the name of an internal command. 称它为renfolder.bat或其他名称(rename.bat)使用内部命令的名称。
@echo off
for /d %%a in (*) do (
cd "%%a"
for %%b in (*) do (
echo copying "%%a%%~xb"
copy "%%b" "\%%a%%~xb" >nul
)
cd..
rd "%%a"
)
pause
Thanks for the answers! 感谢您的回答!
I improved upon your code to work as intended so it perfectly fit my needs now: 我对您的代码进行了改进,使其可以按预期工作,因此现在非常适合我的需求:
@echo off
for /d %%a in (*) do (
cd "%%a"
for %%b in (*) do (
echo moving "%%a\%%b" to "%%a%%~xb"
move "%%b" "..\%%a%%~xb"
)
cd ..
rd "%%a"
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.