简体   繁体   English

要从filename和make目录中读取的批处理文件

[英]Batch file to read from filename and make directory

I have .txt files (1 or more) in a directory that I want my batch file to read their filename, get 20 characters starting from the 4th and creating a new directory in the folder. 我在一个目录中有.txt文件(1个或多个),我希望我的批处理文件读取它们的文件名,从第4个开始获取20个字符并在文件夹中创建一个新目录。 Here is my code: 这是我的代码:

for /f %%i in ('dir /b *.TXT') do (
    set filename1=%%i
    set folder1=%filename1:~4,20%
    mkdir %folder1%
)

When I run this program the 1st time, I get a syntax not correct error for the line 3rd line (set folder1=....) and no folder is created. 当我第一次运行这个程序时,我得到第3行(设置folder1 = ....)的语法不正确错误,并且没有创建文件夹。 I tried running it a 2nd time and 2 folders were created (one named "~4" and one named "20"). 我尝试第二次运行它并创建了2个文件夹(一个名为“~4”,一个名为“20”)。 On the 3rd run, the folder was correctly created! 在第3次运行时,文件夹已正确创建! If I close the command prompt and open it again, it also needs to run 3 times before it creates the folder. 如果我关闭命令提示符并再次打开它,它还需要在创建文件夹之前运行3次。

I've also tried using "for /r . $$i in (*.TXT) with no luck. As I understand, the problem is that the line with "set folder1=..." does not get the proper filename. I've also tried using %%~i or %%~ni, I've tried outputing the filename (which seems to always get the correct string) to a text file and then reading from that file, again with no luck. I don't know what else to try. Is it because %%i stores the file itself and not a string with the filename? 我也尝试过使用“for / r。$$ i in(* .TXT)”没有运气。据我所知,问题是带有“set folder1 = ...”的行没有得到正确的文件名。我也尝试过使用%% ~i或%% ~ni,我尝试将文件名(似乎总是得到正确的字符串)输出到文本文件,然后从该文件中读取,再次没有运气。我不知道还有什么可以尝试。是因为%% i存储文件本身而不是文件名的字符串?

A sample file is named "REG_18004247K_20120208_A.TXT" and I want a folder to be created with the name "18004247K_20120208_A". 示例文件名为“REG_18004247K_20120208_A.TXT”,我想要创建一个名为“18004247K_20120208_A”的文件夹。

I am not at all familiar with batch programming (I'm only working with batch files for like 2 weeks) and I'm guessing the problem might be something really simple. 我对批处理编程一点都不熟悉(我只用了2周的批处理文件),我猜这个问题可能很简单。 Any help would be appreciated. 任何帮助,将不胜感激。

Kyriacos Kyriacos

%%i does stores a string with a filename. %%i确实存储了一个带文件名的字符串。 It is actually the only "variable" that works as expected here. 它实际上是唯一能够按预期工作的“变量”。

The key problem is that you are using environment variable substitution (of filename1 and of folder1 ) inside a loop, hoping that it will be expanded in each iteration of the loop. 关键问题是你在循环中使用环境变量替换( filename1folder1 ),希望它将在循环的每次迭代中展开。

However, environment variables are expanded before execution of the whole loop begins. 但是,在开始执行整个循环之前,会扩展环境变量。

  1. So, upon the first batch execution, filename1 is not defined and you get an error, folder1 is not set , and you probably also see a folder called %folder1% created. 因此,在第一次批处理执行时,未定义filename1并且您收到错误, folder1 is not set ,并且您可能还会看到名为%folder1%的文件夹已创建。
  2. However, the scripts defines filename1 at this time, and the second execution gets further to define folder1 correctly (although line 4 was already expanded using the incorrect value and fun happens). 但是,脚本此时定义了filename1 ,第二次执行进一步正确定义了folder1 (尽管第4行已经使用不正确的值进行了扩展,并且发生了乐趣)。
  3. The third execution finally gets to see the right value of folder1 ; 第三次执行终于可以看到folder1的正确值; but this would obviously not work for more than one file, as the loop logic is dysfunctional. 但这显然不适用于多个文件,因为循环逻辑功能失调。

This older answer explains this problem with great other examples and special cases. 这个较旧的答案通过很多其他示例和特殊情况解释了这个问题。

Your batch can be fixed by setlocal enabledelayedexpansion at the beginning of the script, initializing the variables to empty strings before the loop, and switching to the !...! 您的批处理可以通过脚本开头的setlocal enabledelayedexpansion来修复,在循环之前将变量初始化为空字符串,然后切换到!...! syntax when expanding them: !filename1:~4,20! 扩展它们时的语法: !filename1:~4,20! , and !folder1! ,和!folder1! .

setlocal enabledelayedexpansion
for /r %%i in (*) do (
    set filename1=%%i
    set folder1=!filename1:~4,20!
    mkdir !folder1!
)

Because ... delayed expansion takes some time to store actual variable. 因为...延迟扩展需要一些时间来存储实际变量。
!...! rather than %...% echoes fast allocating variables to DOS. 而不是%...%回应快速分配变量到DOS。

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

相关问题 从Java文件中读取Windows文件名 - Read windows filename from file in Java 如果我在日志文件中找到字符串“found”,如何编写批处理脚本来循环目录中的日志文件并生成“filename.found”? - How to write a batch script to loop through logfiles in directory and generate a “filename.found” if i find the string “found” in the log file? 要读取的批处理文件 - Batch File To Read 批处理:确定从文件读取的行中是否存在子字符串 - Batch: determine if a substring exist in a line read from a file 将读取的文件放在目录中 - Put the read file in directory 批处理文件将“ This.is.a.FIlename.mp3”重命名为“ tiaf.mp3” - batch file renaming “This.is.a.FIlename.mp3” to “tiaf.mp3” 从目录中获取文件的文件名,然后按照字符串操作(例如BATCH脚本中的“字符串合并”)进行更改 - Grab the filenames of the file from the directory and change as per string manipulation like String Concatination in BATCH script BATCH CMD - 使用特殊字符读取INI文件 - BATCH CMD - Read INI file with specialcharacter 使用DOS批处理,读取管道分隔文件中的第二行,并从第三列中提取日期 - Using DOS batch, read 2nd line in a pipe delimited file and extract date from 3rd column 如何从SaveFileDialog.FileName中仅获取目录名称 - How to get only directory name from SaveFileDialog.FileName
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM