根据一系列文件批量创建文件夹并移至子目录 [英] Batch create folders based on series of files and move to subdirectory

查看:203
本文介绍了根据一系列文件批量创建文件夹并移至子目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上找到了这个批处理脚本,该脚本使用文件名并根据文件名创建一个文件夹,然后将文件移入该文件夹:



<$ (*)中%% f的p $ p> 做@(md %%〜nf&move %% f %%〜nf)> nul 2& 1& 1

我想获取相似的文件并将其移动到相同的文件夹中,所有多个文件都有001 002 003 ...等,所以我只想根据末尾的数字对它们进行排序和移动。我做了一些挖掘工作,我认为我需要在该脚本中使用%% var:〜start,end %%,但不能100%确定如何配置它。



所以如果我有:

  render001.png 
render002.png
render003.png

  txt001.png 
txt002.png
txt003.png

我只想要2个分别名为'render'和'txt'的文件夹,并将文件移入其中



任何帮助都很棒。

解决方案

这是我的建议:

  @Echo Off 
::列出所有文件,然后一次调用:move_2_subfolder -function一个文件。
FOR %% a IN(* .png)请调用:move_2_subfolder %%〜a
转到:结束

:move_2_subfolder
::保存名称[无扩展名]的文件名更改为sub_folder_name -variable。
设置sub_folder_name =%〜n1
::去除数字。
设置sub_folder_name =%sub_folder_name:0 =%
设置sub_folder_name =%sub_folder_name:1 =%
设置sub_folder_name =%sub_folder_name:2 =%
设置sub_folder_name =%sub_folder_name:3 =%
设置sub_folder_name =%sub_folder_name:4 =%
设置sub_folder_name =%sub_folder_name:5 =%
设置sub_folder_name =%sub_folder_name:6 =%
设置sub_folder_name =%sub_folder_name :7 =%
设置sub_folder_name =%sub_folder_name:8 =%
设置sub_folder_name =%sub_folder_name:9 =%
::如有必要,创建文件夹。
如果不存在%sub_folder_name% md%sub_folder_name%\
::移动文件。
移动%〜1%sub_folder_name%\
::返回For命令。
转到:EOF

:End
如果已定义sub_folder_name设置sub_folder_name =
@Echo Off

这将不起作用:

 对于/ L %% a IN (0,1,9)DO设置sub_folder_name =%sub_folder_name:%%〜a =%* 

  ... 
对于/ L %% a IN(0,1,9)DO呼叫:strip_numbers %%〜a

:strip_numbers
设置sub_folder_name =%sub_folder_name:%〜1%
转到:EOF

出于某些原因, Set -命令不接受 %%〜a %〜1 变量。


I have this batch script I found online that takes a file name and makes a folder based on the name of the file and then moves the file into that folder:

for %%f in (*) do @(md "%%~nf"&move "%%f" "%%~nf")>nul 2>&1&1

I want to take similar files and move them into same folders, all multiple files have 001 002 003... ect at the end, so I just want to sort and move them based on everything besides the numbers on the end. I've did some digging and I think I need to use %%var:~start,end%% in that script, but not 100% sure how to configure it.

so if I have:

render001.png
render002.png
render003.png

and

txt001.png
txt002.png
txt003.png

I just want 2 folders named 'render' and 'txt' with the files moved into there

any help would be awesome.

解决方案

Here's my suggestion:

@Echo Off
:: List all files and call :move_2_subfolder -function one file at a time. 
FOR %%a IN (*.png) DO Call :move_2_subfolder "%%~a"
GoTo :End

:move_2_subfolder
:: Save the name of the file [without extension] to sub_folder_name -variable.
Set sub_folder_name=%~n1
:: Strip the numbers off. 
Set sub_folder_name=%sub_folder_name:0=%
Set sub_folder_name=%sub_folder_name:1=%
Set sub_folder_name=%sub_folder_name:2=%
Set sub_folder_name=%sub_folder_name:3=%
Set sub_folder_name=%sub_folder_name:4=%
Set sub_folder_name=%sub_folder_name:5=%
Set sub_folder_name=%sub_folder_name:6=%
Set sub_folder_name=%sub_folder_name:7=%
Set sub_folder_name=%sub_folder_name:8=%
Set sub_folder_name=%sub_folder_name:9=%
:: Create folder, if necessary.
If not exist "%sub_folder_name%" md "%sub_folder_name%\" 
:: Move the file.
Move "%~1" "%sub_folder_name%\"
:: Return to For -command. 
GoTo :EOF

:End
If Defined sub_folder_name Set sub_folder_name=
@Echo Off

This won't work:

For /L %%a IN (0,1,9) DO Set sub_folder_name=%sub_folder_name:%%~a=%*

Neither will this:

...
For /L %%a IN (0,1,9) DO Call :strip_numbers "%%~a"

:strip_numbers
Set sub_folder_name=%sub_folder_name:%~1%
GoTo :EOF

For some reason the Set -command doesn't accept the %%~a or %~1 variable.

这篇关于根据一系列文件批量创建文件夹并移至子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆