如何使用Windows批处理编程重命名与该文件夹中第一个文件的文件名相同的文件夹? [英] How to rename a folder the same as the filename of the first file inside that folder using Windows batch programming?

查看:125
本文介绍了如何使用Windows批处理编程重命名与该文件夹中第一个文件的文件名相同的文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组大约3,000个名为XYZ-1至XYZ-3000的文件夹.在这些文件夹内是一组图像文件和一个描述文本文件.如:

I have a group of about 3,000 folders named XYZ-1 through XYZ-3000. Inside those folders are groups of image files and a description text file. Such as:

  • Property123_0001.jpg
  • Property123_0002.jpg
  • Property123_0003.jpg
  • ...
  • Property123_0085.jpg
  • Property123_0086.jpg
  • Z_Description.txt

每个文件夹中.jpg文件的数量不同,但是始终只有一个描述文件.每个目录的图像文件都有不同的名称,例如`XYZ-1可能包含Property123,而XYZ-10可能包含Easement789.

The number of .jpg files differs in each folder, but there's always a single description file. Each directory's image files have a different name, for example `XYZ-1 might contain Property123, while XYZ-10 might contain Easement789.

我需要重命名包含.jpg文件"Property123"部分之后的文件的文件夹.理想情况下,我会删节结尾的数字和下划线,但是即使保留它们,也比XYZ-1名称更好.

I need to rename the folders containing the files after the "Property123" part of the .jpg files. Ideally I would truncate off the trailing digits and the underscore, but even if they were left on, it would be better than the XYZ-1 names.

我正在处理一个批处理文件来做到这一点,但是我的文件无法运行,我也不知道为什么.

I'm working on a batch file to do just that, but mine won't run and I can't figure out why.

这就是我所拥有的:

for /d %%D in (*) do (
   set "_dir=%%D"
   set /a "_first=1"
   cd "%%D"
   for %%F in (*) do (
      if %_first%==1 set "_name=%%~nF"
      set /a "_first=0"
   )
   cd ..
   ren %_dir% %_name%
)

不幸的是,当我运行它时,命令窗口立即关闭.我尝试在结尾处添加一个pause命令,但这并没有使窗口保持打开状态.文件/文件夹没有任何反应.

Unfortunately, when I run it, the command window just closes immediately. I tried putting a pause command at the end, but that didn't keep the window open. Nothing happens to the files/folders.

我已经从命令行尝试了语法,一切看起来都还不错.所以我不知道问题是什么.请给我一点帮助吗?

I've tried my syntax from a command line and everything looks OK. So I don't know what the problem is. Can I get a little help, please?

推荐答案

尝试一下:

@echo off
setlocal EnableDelayedExpansion
for /d %%D in (*) do (
   set "_dir=%%D"
   set "_name=%%D"
   set /a "_first=1"
   cd "%%D"
   for %%F in (*) do (
      if !_first!==1 set "_name=%%~nF"
      set /a "_first=0"
   )
   cd ..
   ren !_dir! !_name!
)

您需要在此处使用setlocal EnableDelayedExpansion,然后使用!而不是%的for循环.

You need to use setlocal EnableDelayedExpansion here, and use ! instead of % inside for loops.

我还添加了集"_name = %% D",因此,如果该文件夹中不包含文件,它将不会尝试将第二个文件夹更改为第一个文件夹名称

I also added set "_name=%%D" so it won't try to change a second folder to the first folders name if that folder doesn't contain files

还考虑了下划线部分:

@echo off
setlocal EnableDelayedExpansion
for /d %%D in (*) do (
   set "_dir=%%D"
   set "_name=%%D"
   set /a "_first=1"
   cd "%%D"
   for %%F in (*) do (
      if !_first!==1 (
        set "_name=%%~nF"
        set /a "_first=0"
        if NOT "!_name!"=="!_name:_=!" (
          set "subfix=!_name:*_=!"
          for /f "delims=" %%B in ("!subfix!") do set "_name=!_name:_%%B=!"
        )
      )
   )
   cd ..
   ren !_dir! !_name!
)

这篇关于如何使用Windows批处理编程重命名与该文件夹中第一个文件的文件名相同的文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆