批处理命令根据文件名创建文件夹并将文件/文件夹移动到创建的文件夹

时间:2022-07-12 10:34:18

Basically what I'm trying to do is create a folder from a filename, which this does:

基本上我要做的是从文件名创建一个文件夹,这样做:

for %%i in (*.png) do mkdir "%%~ni"

for %% i in(* .png)do mkdir“%% ~ni”

After folder creation I'd like move a folder into this new folder.

创建文件夹后,我想将文件夹移动到这个新文件夹中。

move "other_folder" %%~ni

移动“other_folder”%% ~ni

4 个解决方案

#1


1  

Based on your now completely changed question:

基于您现在完全改变的问题:

If there was only one .png file in the working directory, then you could simply do it in one line at the Command Prompt:

如果工作目录中只有一个.png文件,那么您只需在命令提示符下的一行中执行:

For %A In ("*.png") Do RoboCopy "other_folder" "%~nA" /E /MOVE >Nul

However, if you think about it, once the other_folder has been moved the first time, it is no longer there to be moved again! You would therefore need to instead copy it, then after all .png files have been processed, remove other_folder.

但是,如果您考虑一下,一旦第一次移动了other_folder,它就不再再被移动了!因此,您需要复制它,然后在处理完所有.png文件后,删除other_folder。

At the Command Prompt: (two different commands, the first copies, the second removes)

在命令提示符处:(两个不同的命令,第一个副本,第二个删除)

For %A In ("*.png") Do RoboCopy "other_folder" "%~nA" /E > Nul
RD /S /Q "other_folder"

Similarly from a batch file:

同样来自批处理文件:

@For %%A In ("*.png") Do @RoboCopy "other_folder" "%%~nA" /E > Nul
@RD /S /Q "other_folder"

Just take account that if anything goes wrong, (e.g. all of the content of other_folder doesn't copy), and you remove other_folder, you've lost that content.

请记住,如果出现任何问题,(例如,other_folder的所有内容都不会复制),并且您删除了other_folder,那么您就丢失了该内容。

#2


1  

just extend the part to be executed by DO:

只需扩展DO要执行的部分:

for %%i in (*.png) do (
  mkdir "%%~ni"
  convert "%%i" --folder="%%ni\test.bmp"
)

(not sure, how your convert command works - above is a guess)

(不确定,你的转换命令如何工作 - 上面是一个猜测)

#3


1  

Based on your title request, the bellow script will create a folder named after each *.png in the directory then move the matching file into the newly created file.

根据您的标题请求,bellow脚本将创建一个以目录中每个* .png命名的文件夹,然后将匹配文件移动到新创建的文件中。

Batch:

for %%i in (*.png) do (mkdir "%%~ni" && move %%i %%~ni)

Command Prompt:

for %i in (*.png) do (mkdir "%~ni" && move %i %~ni)

#4


0  

Thanks everybody I was able to figure it out

谢谢大家,我能够弄明白

for %%i in (*.png) do mkdir "%%~ni" && move "folder" "%%~ni"

for %% i in(* .png)do mkdir“%% ~ni”&& move“folder”“%% ~ni”

#1


1  

Based on your now completely changed question:

基于您现在完全改变的问题:

If there was only one .png file in the working directory, then you could simply do it in one line at the Command Prompt:

如果工作目录中只有一个.png文件,那么您只需在命令提示符下的一行中执行:

For %A In ("*.png") Do RoboCopy "other_folder" "%~nA" /E /MOVE >Nul

However, if you think about it, once the other_folder has been moved the first time, it is no longer there to be moved again! You would therefore need to instead copy it, then after all .png files have been processed, remove other_folder.

但是,如果您考虑一下,一旦第一次移动了other_folder,它就不再再被移动了!因此,您需要复制它,然后在处理完所有.png文件后,删除other_folder。

At the Command Prompt: (two different commands, the first copies, the second removes)

在命令提示符处:(两个不同的命令,第一个副本,第二个删除)

For %A In ("*.png") Do RoboCopy "other_folder" "%~nA" /E > Nul
RD /S /Q "other_folder"

Similarly from a batch file:

同样来自批处理文件:

@For %%A In ("*.png") Do @RoboCopy "other_folder" "%%~nA" /E > Nul
@RD /S /Q "other_folder"

Just take account that if anything goes wrong, (e.g. all of the content of other_folder doesn't copy), and you remove other_folder, you've lost that content.

请记住,如果出现任何问题,(例如,other_folder的所有内容都不会复制),并且您删除了other_folder,那么您就丢失了该内容。

#2


1  

just extend the part to be executed by DO:

只需扩展DO要执行的部分:

for %%i in (*.png) do (
  mkdir "%%~ni"
  convert "%%i" --folder="%%ni\test.bmp"
)

(not sure, how your convert command works - above is a guess)

(不确定,你的转换命令如何工作 - 上面是一个猜测)

#3


1  

Based on your title request, the bellow script will create a folder named after each *.png in the directory then move the matching file into the newly created file.

根据您的标题请求,bellow脚本将创建一个以目录中每个* .png命名的文件夹,然后将匹配文件移动到新创建的文件中。

Batch:

for %%i in (*.png) do (mkdir "%%~ni" && move %%i %%~ni)

Command Prompt:

for %i in (*.png) do (mkdir "%~ni" && move %i %~ni)

#4


0  

Thanks everybody I was able to figure it out

谢谢大家,我能够弄明白

for %%i in (*.png) do mkdir "%%~ni" && move "folder" "%%~ni"

for %% i in(* .png)do mkdir“%% ~ni”&& move“folder”“%% ~ni”