移动和重命名文件,保留扩展名但在批处理文件中包含子目录
如果这不是问这些问题的地方,请原谅我,我是批处理和脚本的新手,对此类帖子也有点陌生......
我有一个将接收文件和文件夹的文件夹,我想运行一个脚本查看目录并以数字方式重命名每个子文件夹中的所有文件,并在可能的情况下移动它们。
例如,我有如下所示的内容,
Recieved_File_Folder
|_folder1
| |_file1.txt
| |_file2.bmp
|_folder2
| |_file4.exe
| |_file5.bmp
|__file9.txt
|__file10.jpg
我希望能够查看每个目录并将其移动到类似的目录,请记住文件的名称将是随机的,并且我也希望保持扩展名完整。
Renamed_Folder
|_folder1
| |_1.txt
| |_2.bmp
|_folder2
| |_1.exe
| |_2.bmp
|__1.txt
|__2.jpg
我在这方面花了很多时间,但做得不太好,任何帮助将不胜感激!先感谢您!
Forgive me if this is nor the place to ask these questions, I am new to batch and scripts and a bit new to these kind of posts...
I have a folder that will receive files and folders, I want to run a script that looks at the directory and renames all files in each subfolder numerically, and moves them if possible.
For example I have something that looks like the following
Recieved_File_Folder
|_folder1
| |_file1.txt
| |_file2.bmp
|_folder2
| |_file4.exe
| |_file5.bmp
|__file9.txt
|__file10.jpg
I would like to be able to look in every directory and move it to something like this, keeping in mind the names of the files will be random and I want to keep the extension intact also.
Renamed_Folder
|_folder1
| |_1.txt
| |_2.bmp
|_folder2
| |_1.exe
| |_2.bmp
|__1.txt
|__2.jpg
I have spent alot of time on this and am not doing too well with it, any help would be very greatly appreciated!! Thank you in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个小脚本应该可以解决这个问题:
请小心使用它,因为脚本不会要求确认,因此请注意从哪里开始!
它是如何实现的工作:
FOR
循环以递归方式列出所有子目录,从当前目录开始(使用DIR /B /S /A:D
)并传递通过第一个循环体中的变量%%A
到循环体的完整路径FILE_COUNTER
设置为1
的FOR
-循环列出了外部循环传入的目录中的所有文件(使用DIR /B /A:-D "%%A"
)和通过内部循环体中的变量%%B
将文件的完整路径传递到其主体 使用的完整文件名调用子例程
值作为其参数,:RENAME
FILE_COUNTER:RENAME
子例程使用其参数形成新文件名,并在REN
,当前FILE_COUNTER
值加一 (SET /A FILE_COUNTER=FILE_COUNTER+1
)This little script should do the trick:
Use it with care as the script will not ask for confirmation, so watch out where you start it from!
How does it work:
FOR
-loop lists all sub directories recursively, starting with the current directory (usingDIR /B /S /A:D
) and passes the full path to the loop body via the variable%%A
FILE_COUNTER
is set to the value of1
FOR
-loop lists all files in the directory passed in by the outer loop (usingDIR /B /A:-D "%%A"
) and passes the file's full path to its body via the variable%%B
:RENAME
is called with the full file name the currentFILE_COUNTER
value as its parameters:RENAME
sub routine uses its parameters to form the new file name and issues a rename commandREN
FILE_COUNTER
value is increased by one (SET /A FILE_COUNTER=FILE_COUNTER+1
)