简体   繁体   English

Windows批处理脚本在包含子文件夹后命名文件并复制到根目录

[英]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.

相关问题 Windows批处理脚本,用于复制和修改包含重定向字符的文件 - Windows batch script to copy and modify files containing redirect characters 使用Windows批处理脚本复制最新的2个文件 - Copy latest 2 files using Windows batch script Windows 7 Batch - 创建子文件夹,然后查找文件名中包含特定文本的文件并将这些文件移动到新创建的子文件夹中 - Windows 7 Batch - Create subfolder, then find files with certain text in file name and move those files in the newly created subfolder Windows脚本批处理将文件从不同文件夹复制到其他文件夹 - Windows script batch to copy files from different folders to other folders Windows Shell批处理脚本复制最后10个修改的文件 - Windows Shell Batch Script to copy the last 10 modified files Windows Batch脚本复制最近x分钟内修改的文件 - Windows Batch script copy the files modified in last x minutes 使用批处理脚本在 Windows 上压缩同名文件 - Zip files with same name on Windows using batch script Windows批处理,以递归方式复制文件 - Windows batch, recursively copy files Windows cmd批处理中的文件副本 - copy of files in windows cmd batch Windows Batch脚本在for循环中使用其文件夹名称重命名文件 - Windows Batch script to rename files with it's folder name within a for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM