简体   繁体   English

创建批处理文件以修改目录中的文件名

[英]Creating a batch file to modify the filenames in a directory

Im trying to build a batch file. 我试图建立一个批处理文件。

I have 30 csv files in a folder 我在一个文件夹中有30个csv文件

My intention is 我的意图是

  1. Get each file name (Example is 097_021216_192332.csv) 获取每个文件名(示例为097_021216_192332.csv)
  2. Extract the first 3 digits 提取前3位数字
  3. Compare it with a lookup table ( lookup1.bat) against which i have marked another string 将它与查找表(lookup1.bat)进行比较,我已经标记了另一个字符串

EG: lookup1.bat EG:lookup1.bat

@echo 107=B_05-
@echo 097=B_06-
@echo 149=B_07-
@echo 109=B_08-
@echo 101=B_09-
@echo 105=B_10-
@echo 098=B_11-    

So here i will get "B_06-" 所以在这里我会得到“B_06-”

  1. Modify the file name with this "B_06-" prefix and rename the file 使用此“B_06-”前缀修改文件名并重命名该文件

Here is my code , i have only basic ideas about looping and im struggling a lot.Thanks for any help. 这是我的代码,我只有关于循环的基本想法和我很多努力。感谢任何帮助。

@echo on

setlocal enabledelayedexpansion


for %%a in ("*.csv") do ( 
    set FileName=%%~na
    goto:stepa
    goto:eof

)
:stepa
for /f "tokens=1 delims=_" %%a in ("%FileName%") do ( 
    set A=%%a 
    echo %A%
    )

@SET MN=%A%
@FOR /F "tokens=1,2 delims==" %%i IN ('lookup1.bat') DO @IF %%i EQU %MN% SET MW=%%j

@ECHO The board number corresponding to %MN% is %MW%.

set "str=%MW%%FileName%"
echo "%str%"    

Ren "!FileName!" "!str!"

:eof

You have a series of problems with the structure of your program. 您的程序结构存在一系列问题。 If you want to call a subroutine from inside a for command the right way is using call , not goto , and the goto :eof command must be placed after the for ends. 如果你想从里面调用子程序 for命令以正确的方式是使用call ,而不是gotogoto :eof命令必须放置 for末端。 However, this code is simple enough so it don't requires a subroutine. 但是,这段代码很简单,因此不需要子程序。

The table lookup method is more efficient (and also simpler, IMHO) if you use an array to load the table just once, and then directly access its elements via an index . 如果您使用数组只加载一次表,然后通过索引直接访问其元素,则表查找方法更有效(也更简单,恕我直言)。

@echo off
setlocal EnableDelayedExpansion

rem Create the lookup file with lookup1.bat file,
rem this program create lines like this one:
rem 097=B_06-
call lookup1.bat > lookup1.txt

rem Load the lookup table from the file,
rem this method create _array elements_ like this one:
rem set "table[097]=B_06-"
for /F "tokens=1,2 delims==" %%a in (lookup1.txt) do set "table[%%a]=%%b"

rem Process the files in current folder,
rem file name format is this:
rem ###_restOfFileName.csv
for /F "tokens=1* delims=_" %%a in ('dir /A:-D /B *.csv') do (
   @ECHO The board number corresponding to %%a is !table[%%a]!.
   ren "%%a_%%b" "!table[%%a]!%%b"
)

You may test if the board number is not defined for a file via an additional if not defined table[%%a] ... command placed inside the for . 您可以通过放置在for的附加if not defined table[%%a] ...命令来测试是否未为文件定义板号。

You may directly create the lookup1.txt file with your text editor; 您可以使用文本编辑器直接创建lookup1.txt文件; just eliminate all these @echo parts from lookup1.bat and change the extension. lookup1.bat删除所有这些@echo部分并更改扩展名。

You may review a detailed explanation on array use in Batch files at this answer . 您可以在此答案中查看有关批处理文件中阵列使用的详细说明。

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

相关问题 隐藏类型命令批处理文件中的文件名 - Hide filenames in type command batch file 使用批处理文件列出csv上的文件名和文件夹路径 - List filenames and folder paths on csv using batch file CSV文件未在目录中创建 - csv file not creating in a directory 批量查找文件,将文件复制到另一个目录 - Batch to find file, copy file to another directory 通过.csv文件中的批处理脚本创建序列生成器 - Creating sequence generator by batch script in .csv file 如何使用批处理来修改 CSV 文件中的某些列? - How do you use a batch to modify certain columns in a CSV file? 如何从文件名以日期结尾的目录中仅加载最新文件? - How to load only the most recent file from a directory where the filenames endswith the date? 如何从文件名以日期开头的目录中仅加载最新文件? - How to load only the most recent file from a directory where the filenames startswith the date? 如何将用于创建单个图像的随机选择图像的文件名附加到 csv 文件? - How to append the filenames of randomly selected images used in creating a single image to a csv file? 批处理文件,该文件将带有日期的动态文件名复制到另一个目录 - Batch file that copies a dynamic file name to a different directory with the date
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM