简体   繁体   English

如何使用 Windows 批处理编程重命名与该文件夹内第一个文件的文件名相同的文件夹?

[英]How to rename a folder the same as the filename of the first file inside that folder using Windows batch programming?

I have a group of about 3,000 folders named XYZ-1 through XYZ-3000.我有一组大约 3,000 个文件夹,名为 XYZ-1 到 XYZ-3000。 Inside those folders are groups of image files and a description text file.在这些文件夹中是一组图像文件和一个描述文本文件。 Such as:如:

  • Property123_0001.jpg物业123_0001.jpg
  • Property123_0002.jpg物业123_0002.jpg
  • Property123_0003.jpg物业123_0003.jpg
  • ... ...
  • Property123_0085.jpg物业123_0085.jpg
  • Property123_0086.jpg物业123_0086.jpg
  • Z_Description.txt Z_Description.txt

The number of .jpg files differs in each folder, but there's always a single description file.每个文件夹中.jpg文件的数量不同,但始终只有一个描述文件。 Each directory's image files have a different name, for example `XYZ-1 might contain Property123, while XYZ-10 might contain Easement789.每个目录的图像文件都有不同的名称,例如`XYZ-1 可能包含 Property123,而 XYZ-10 可能包含 Easement789。

I need to rename the folders containing the files after the "Property123" part of the .jpg files.我需要在 .jpg 文件的“Property123”部分之后重命名包含文件的文件夹。 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.理想情况下,我会截断尾随数字和下划线,但即使保留它们,它也会比 XYZ-1 名称更好。

I'm working on a batch file to do just that, but mine won't run and I can't figure out why.我正在处理一个批处理文件来做到这一点,但我的无法运行,我不知道为什么。

Here's what I have:这是我所拥有的:

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%
)

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?我能得到一点帮助吗?

Try this:尝试这个:

@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!
)

You need to use setlocal EnableDelayedExpansion here, and use !您需要在此处使用 setlocal EnableDelayedExpansion,并使用 ! instead of % inside for loops.而不是 % 在 for 循环中。

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我还添加了 set "_name=%%D" 所以如果该文件夹不包含文件,它不会尝试将第二个文件夹更改为第一个文件夹名称

EDIT:编辑:

also takes into consideration the underscore part:还考虑了下划线部分:

@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!
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM