移动和重命名文件,保留扩展名但在批处理文件中包含子目录

发布于 2024-09-03 16:11:32 字数 594 浏览 1 评论 0原文

如果这不是问这些问题的地方,请原谅我,我是批处理和脚本的新手,对此类帖子也有点陌生......

我有一个将接收文件和文件夹的文件夹,我想运行一个脚本查看目录并以数字方式重命名每个子文件夹中的所有文件,并在可能的情况下移动它们。

例如,我有如下所示的内容,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神经暖 2024-09-10 16:11:32

这个小脚本应该可以解决这个问题:

@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

请小心使用它,因为脚本不会要求确认,因此请注意从哪里开始!

它是如何实现的工作:

  • 第一个 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:

@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:

  • the first 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
  • in the first loops body a variable FILE_COUNTER is set to the value of 1
  • the second (inner) 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
  • in the inner loop body the sub routine :RENAME is called with the full file name the current FILE_COUNTER value as its parameters
  • the :RENAME sub routine uses its parameters to form the new file name and issues a rename command REN
  • after the sub routine returns, the current FILE_COUNTER value is increased by one (SET /A FILE_COUNTER=FILE_COUNTER+1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文