批量复制源文件到某些子目录,复制名称基于其中的文件? [英] Batch copying a source file to certain subdirectories, with copy names based on files within?
问题描述
正如标题中所述,我需要将源txt文件复制到驻留在目录中特定子文件夹集中的任意数量的tga文件中,其中txt文件的副本具有与tga匹配的名称文件在那里.假设我有一个看起来像这样的文件夹结构:
As it says in the title, I have a need to copy a source txt file to any number of tga files that reside within a certain set of subfolders in the directory, where the copies of the txt file have names matching the tga files there. Let's say I have a folder structure looking like this:
destination1/aaa.tga
destination1/bbb.tga
destination2/aaa.tga
otherfolder/ccc.tga
ddd.tga
copyme.txt
所以我想要的是这样的最终结果,其中每个新的txt文件都是 copyme.txt
的副本:
So what I want is an end result that's like this, where each new txt file is a copy of copyme.txt
:
destination1/aaa.tga
destination1/aaa.txt
destination1/bbb.tga
destination1/bbb.txt
destination2/aaa.tga
destination2/aaa.txt
otherfolder/ccc.tga
ddd.tga
copyme.txt
我已经获得了这段代码,但是我知道它不太正确.folder变量不会发送到内部进行循环(将其保留在目录中的任何位置并将其全部复制到其主文件夹中),并且当前使用%%〜nxF令牌获得的任何结果都将显示为[文件名] .txt.tga,这是不正确的.
I've gotten this code, but I know it's not quite right. The folder variable doesn't get sent to the interior for loop (leaving it searching everywhere in the directory and copying them all to its main folder), and currently any results that would get made with the %%~nxF token wind up as [filename].txt.tga, which is incorrect.
for /d %%G in ("..\materialsrc\VGUI*") do (
SETLOCAL
set folder=%%~nxG
for /r %%F in ("..\materialsrc\%folder%\*.tga") do (
copy "..\materialsrc\VGUIBase.txt" "..\materialsrc\%folder%\%%~nxF.txt"
)
ENDLOCAL
)
很抱歉,这是一件容易的事,我找不到任何地方记录的这种特定用法,而且我对编写批处理文件也不是很精通.
I'm sorry if this is an easy thing to do, I couldn't find this specific sort of usage documented anywhere and I'm not super well versed in writing batch files.
推荐答案
标准延迟扩展
问题(有关此问题的成千上万个SO项目-使用搜索工具.
Standard delayedexpansion
problem (thousands of SO items about this - use the Search facility.
但是-在任何情况下,您都不需要设置变量 folder
.
But - you don't need to set the variable folder
in any case.
使用
for /r %%F in ("..\materialsrc\%%~nxG\*.tga") do (
copy "..\materialsrc\VGUIBase.txt" "..\materialsrc\%%~nxG\%%~nxF.txt"
(并且 setlocal/endlocal
命令对是多余的)
(and the setlocal/endlocal
command-pair is superfluous)
这篇关于批量复制源文件到某些子目录,复制名称基于其中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!