压扁目录 [英] Flattening a directory
问题描述
假设以下目录结构
my_script.bat
Dir1
File1.txt
Dir2
Dir3
File1.txt
我要压平,这样我只有一个目录中,其中每个文件名与所有当前目录下的父目录(在我的批处理文件是坐)的pfixed $ P $。
I want to flatten it so that I have just one directory, where each filename is prefixed with all of its parent directories below the current directory (where my batch file is sitting).
所以扁平化上面的列表中会产生类似
So flattening the above list would produce something like
Dir1-File1.txt
Dir2-Dir3-File1.txt
或者只是一些方法来识别哪个文件夹它是从哪里来的,也不一定是复姓。
Or just some way to identify which folder it came from, doesn't necessarily have to be hyphenated.
可以这样使用Windows批处理文件做了什么?或者,也许已经有一个命令呢?
Can this be done with a windows batch file? Or maybe there is already a command for it?
推荐答案
我要给予信贷雅各谢列兹尼奥夫对于原来的想法,但这里有一个解决方案,其中也包括在新的文件名的路径:
I have to give credit to Jacob Seleznev for the original idea, but here's a solution which also includes the path in the new filename:
Setlocal EnableDelayedExpansion
for /r %%F in (*) do (
set _name=%%~fF
rem Change the path to a relative path by replacing the current folder with ""
set _name=!_name:%cd%\=!
rem Replace slashes with hyphens
set _name=!_name:\=-!
copy "%%F" ".\!_name!"
)
输出:
Dir1-File1.txt
Dir2-Dir3-File1.txt
这篇关于压扁目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!