Windows批处理文件:在for循环设置变量 [英] windows batch files: setting variable in for loop

查看:1350
本文介绍了Windows批处理文件:在for循环设置变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我公司拥有一批具有相同的命名方案的文件。作为样品,四个文件被称为num_001_001.txt,num_002_001.txt,num_002_002.txt,num_002_003.txt

I have a number of files with the same naming scheme. As a sample, four files are called "num_001_001.txt", "num_002_001.txt", "num_002_002.txt", "num_002_003.txt"

的第一组数字的重新presents其中包,它从的,并且第二组数字只是用来彼此区分它们。因此,在这个例子中,我们有在包001一个文件,并在包三个文件002

The first set of numbers represents which "package" it's from, and the second set of numbers is simply used to distinguish them from one another. So in this example we have one file in package 001, and three files in package 002.

我写一个Windows Vista的批处理命令采取的所有文件,并将它们转移到它们自己的目录,每个目录重新presents不同的包。所以,我想移动所有的文件包001放入目录001和所有002到目录002

I am writing a windows vista batch command to take all of the files and move them into their own directories, where each directory represents a different package. So I want to move all the files for package 001 into directory "001" and all for 002 into directory "002"

我已经成功地写入一个脚本,将遍历所有txt文件,并呼应他们。我还写了一个纸条,将一个文件移动到另一个位置,以及创建目录,如果它不存在。

I have successfully written a script that will iterate over all of the txt files and echo them. I have also written a scrip that will move one file into another location, as well as creating the directory if it doesn't exist.

现在我的身影,我将需要使用子,所以我用的%VAR:〜开始,结束%语法来获得它们。作为测试,我写了这个验证我其实可以提取子,并有条件地创建一个目录

Now I figure that I will need to use substrings, so I used the %var:~start,end% syntax to get them. As a test, I wrote this to verify that I can actually extract the substring and create a directory conditionally


@echo off
set temp=num_001_001.txt
NOT IF exist %temp:~0,7%\
  mkdir %temp:~0,7%

和它的作品。大。

于是我加入了循环吧。

And it works. Great.
So then I added the for loop to it.

@echo off
FOR /R %%X IN (*.txt) DO (
  set temp=%%~nX
  echo directory %temp:~0,7%
)

但是,这是我的输出:

But this is my output:


directory num_002
directory num_002
directory num_002
directory num_002

有什么不对?难道Vista中不支持在每次迭代中重新分配变量?
这四个文件是在我的目录中,其中一人应该创建num_001。我把在不同的文件与003 004 005,所有的这是最后一个包的名字。我猜什么错,我怎么设置的东西。

What's wrong? Does vista not support re-assigning variables in each iteration? The four files are in my directory, and one of them should create num_001. I put in different files with 003 004 005 and all of it was the last package's name. I'm guessing something's wrong with how I'm setting things.

我有不同的解决方法来完成这项工作,但我很困惑,为什么这样一个简单的概念是行不通的。

I have different workarounds to get the job done but I'm baffled why such a simple concept wouldn't work.

推荐答案

您的问题是,该变量时批处理器读取指挥,在执行之前被替换。

Your problem is that the variable get replaced when the batch processor reads the for command, before it is executed.

试试这个:

SET temp=Hello, world!
CALL yourbatchfile.bat

,你会看到你好印刷的5倍。

And you'll see Hello printed 5 times.

溶液延迟扩展;你需要先启用它,然后用温度而不是%TEMP%:!

The solution is delayed expansion; you need to first enable it, and then use !temp! instead of %temp%:

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /R %%X IN (*.txt) DO (
  set temp=%%~nX
  echo directory !temp:~0,7!
)

请参阅这里了解更多详情。

这篇关于Windows批处理文件:在for循环设置变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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