[英]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! 请谨慎使用,因为脚本不会要求您确认,因此请注意从何处开始!
How does it work: 它是如何工作的:
FOR
-loop lists all sub directories recursively, starting with the current directory (using DIR /B /S /A:D
) and passes the full path to the loop body via the variable %%A
FOR
循环递归列出所有子目录,从当前目录开始(使用DIR /B /S /A:D
),并通过变量%%A
将完整路径传递到循环主体 FILE_COUNTER
is set to the value of 1
FILE_COUNTER
设置为值1
FOR
-loop lists all files in the directory passed in by the outer loop (using DIR /B /A:-D "%%A"
) and passes the file's full path to its body via the variable %%B
FOR
-loop列出外部循环(使用DIR /B /A:-D "%%A"
)传入的目录中的所有文件,并通过变量%%B
将文件的完整路径传递给它的主体%%B
:RENAME
is called with the full file name the current FILE_COUNTER
value as its parameters :RENAME
以完整文件名调用当前FILE_COUNTER
值作为其参数 :RENAME
sub routine uses its parameters to form the new file name and issues a rename command REN
:RENAME
子例程使用其参数来形成新文件名,并发出重命名命令REN
FILE_COUNTER
value is increased by one ( SET /A FILE_COUNTER=FILE_COUNTER+1
) FILE_COUNTER
值加1( SET /A FILE_COUNTER=FILE_COUNTER+1
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.