批处理文件以从CSV读取文件名并重命名文件夹中的文件 [英] Batch file to read file names from CSV and rename files in a folder

查看:110
本文介绍了批处理文件以从CSV读取文件名并重命名文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在文件中有一组代码,我想用该代码重命名文件夹中的文件.就像第一个文件的第一个代码,然后是第二个文件的第二个代码一样.

I have a set of codes in a file, i want to rename the files in a folder with that code. Like first code for the first file then second code for the second file.

for /f "delims=" %%x in (code.csv) do call :rename "%%x" 
pause
GOTO :EOF

:rename
REN "*.jpg" "a/%1.jpg"

这是我写的内容,但这就像为所有文件运行第一个代码,并得到一个存在的重复文件名或文件找不到错误.即使文件正在重命名,但是由于一些不必要的循环,我仍然花费大量时间.

This is what I have written but this is like running the first code for all the files and am getting a duplicate file name that exists or the file can not found error. Even though the files are being renames but it takes lots of time which I believe due to some unnecessary looping.

任何使它变得完美的帮助都将得到感谢

Any help to make it perfect will be appritiated

推荐答案

您需要一种并行读取目录和 csv 的方法.有一个技巧可以做到:

You need a way to read the directory and the csv in parallel. There is a trick to do that:

@ECHO OFF
setlocal enabledelayedexpansion
<code.csv (
  for /f "delims=" %%x in ('dir /b *.jpg') do (
    set "NewName="
    set /p NewName=
    if not defined NewName goto :eof
    ECHO ren "%%x" "!NewName!"
  )
)

注意:确认 ECHO 命令确实符合您想要真正启用 ren 命令的作用后,即可将其删除.

NOTE: remove the ECHO command after verifying it does what you want to actually enable the ren command.

< code.csv(...) for 循环内的 set/p 中提供一行一行.在读取之前将其清除,因此当 csv 中没有更多名称时, NewName 变量将变得不确定.未定义时(没有更多名称),退出 for 循环.

<code.csv ( ... ) provides one line after the other to the set /p within the for loop. Clear it before reading, so the NewName variable gets undefined, when there are no more names from the csv. Exit the for loop, when it is undefined (no more names).

当不再有要重命名的文件或 csv 文件中没有其他新名称时,循环停止.

The loop stops, when there are either no more files to be renamed or no more new names from the csv file.

请参见 dir/?-尤其是/o 开关,了解如何对文件的名称,日期或大小进行排序(如果需要).

See dir /? - especially the /o switch for how to sort the files (if needed) for names, dates or sizes.

另一种可能性是构建一个包含 csv 内容或文件的数组,但是我认为这对于此任务来说是一个过大的选择.

Another possibility is to build an array of either the csv content or the files but I think that's an overkill for this task.

这篇关于批处理文件以从CSV读取文件名并重命名文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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