用于重命名文件夹中文件的批处理命令 [英] Batch Command for Renaming files in folders
问题描述
只是想知道是否有人可以帮助我.通常,我很想找到一些代码,我需要一个代码来操作它,但是如果我必须从头开始的话,那太可怕了.
Just wondering if anyone can help me. Normally I'm pretty got at finding some code I need an to manipulate it but horrible if I have to start from scratch.
我是一名老师,希望所有文件都带有一致的标签,就像我必须将它们发送出去一样.
I'm a teacher and want to have all the files labelled consistently as I have to send them off.
我想知道有没有一种编写批处理文件的方法来执行以下操作:
What I want to know is there a way to write a batch file to do the following:
我有一个主题文件夹,在每个主题文件夹中都有学生的名字,他们在一个文件夹中做的小册子,然后有一个.doc或.docx文件.
I have a subject folder and in each subject folder I have the students name, booklet they are doing in a folder and then there is the file either a .doc or .docx.
我希望批处理文件将文件重命名为Subject_Book#_Name.doc或.docx的文件
I want the batch file to rename the file to what ever it was to Subject_Book#_Name.doc or .docx
我的文档/主题/学生/图书1/ .doc
My Documents/Subject/Student/Book 1/.doc
\Math A\Jim Book\Book 1 Addition\Addition Booklet.docx
\Math A\Jim Book\Book 2 Subtraction\Subtraction Booklet.docx
Into
\Math A\Jim Book\Book 1 Addition\Math A_Addition Booklet_Jim Book.docx
\Math A\Jim Book\Book 2 Subtraction\Math A_Subtraction Booklet_Jim Book.docx
\Math A\Frank Sims\Book 1 Addition\Addition Booklet.docx
\Math A\Frank Sims\Book 2 Subtraction\Subtraction Booklet.docx
Into
\Math A\Frank Sims\Book 1 Addition\Math A_Addition Booklet_Frank Sims.docx
\Math A\Frank Sims\Book 2 Subtraction\Math A_Subtraction Booklet_Frank Sims.docx
现在,我不介意是否必须将bat文件复制到每个文件夹中,如果某个出色的人可以帮助我并将其保存在主主题文件夹中.
Now I don't mind if I have to copy the bat file into each folder of if some amazing person can help me out and have it in the main subject folder.
我确实在其他地方看到过类似的东西,但这仍然需要很多时间.
I did see something like this somewhere else, but this would still take a lot of time.
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause```
推荐答案
正如@jarmod在评论中提到的那样:在运行此操作之前,请保留原始文件的安全副本!您需要备份以防万一.
如果从主目录中运行它,则应该这样做.它将遍历子目录中的所有匹配文件,然后复制到当前(主)目录中.
This should do it if you run it from within your main directory. It'll iterate over all matching files in the subdirectories and copy into the current (main) directory.
@echo off
Setlocal enabledelayedexpansion
Set "Pattern=\"
Set "Replace=_"
set "RemovePath=!CD:%Pattern%=%Replace%!_"
For /r %%# in ("*.doc?") Do (
Set "File=%%~f#"
set "trimfile=!File:%Pattern%=%Replace%!"
set "trimfile=!trimfile:%RemovePath%=% %!"
REM If you remove "echo" from the next line, the copy will happen
echo copy "!File!" "!trimfile!"
)
Pause
它的作用:
- 设置一些替换模式.即,您要用下划线
_
替换反斜杠\
. - 捕获当前目录,以便稍后可以将其替换为文件名.
- 使用完整路径遍历所有匹配的文件,并将路径中的
\
替换为_
. - 从结果字符串中删除路径,因为我们只想使用下一级目录名称来启动新文件名.
- 回显删除
echo
关键字后将运行的命令.*
- Sets a few patterns for replacement. I.e., you want to replace backslash
\
with underscore_
. - Capture the current directory so we can replace it in the filename later.
- Iterate over all matching files using the full path, and replace the
\
in the path with_
. - Remove the path from the resulting string, since we only want to start the new filename w/the next-level directory name.
- Echo the command that'll run once you remove the
echo
keyword.*
注意:最重要的行是"disabled"与 echo
命令一起安全运行.如果您觉得合适,请删除单词 echo
,然后进行 copy
操作.
Note: The most important line is "disabled" with an echo
command for you to run safely. If it looks good to you, remove the word echo
, and the copy
operation will happen.
这篇关于用于重命名文件夹中文件的批处理命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!