移动和重命名文件,保留扩展名,但在批处理文件中包含子目录 [英] Moving and renaming files, keeping extension but include sub directories in batch file
问题描述
请原谅我,如果这也不是问这些问题的地方,我是批处理和脚本的新手,而对于此类帖子则有些陌生...
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!
推荐答案
这个小脚本应该可以解决问题:
This little script should do the trick:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1 delims=" %%A IN ('DIR /B /S /A:D') DO (
SET /A FILE_COUNTER=1
FOR /F "tokens=1 delims=" %%B IN ('DIR /B /A:-D "%%A"') DO (
CALL :RENAME "%%A%%B" !FILE_COUNTER!
SET /A FILE_COUNTER=FILE_COUNTER+1
)
)
ENDLOCAL
GOTO :EOF
:RENAME
SET OLD_PATH="%~f1"
SET NEW_FILE_NAME="%2%~x1"
REN %OLD_NAME% %NEW_NAME%
GOTO :EOF
请谨慎使用,因为脚本不会要求您确认,因此请注意从何处开始!
Use it with care as the script will not ask for confirmation, so watch out where you start it from!
工作方式:
- 第一个
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
)
- the first
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
- in the first loops body a variable
FILE_COUNTER
is set to the value of1
- the second (inner)
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
- in the inner loop body the sub routine
:RENAME
is called with the full file name the currentFILE_COUNTER
value as its parameters - the
:RENAME
sub routine uses its parameters to form the new file name and issues a rename commandREN
- after the sub routine returns, the current
FILE_COUNTER
value is increased by one (SET /A FILE_COUNTER=FILE_COUNTER+1
)
这篇关于移动和重命名文件,保留扩展名,但在批处理文件中包含子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!