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

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

问题描述

我有许多具有相同命名方案的文件.作为示例,四个文件分别称为num_001_001.txt"、num_002_001.txt"、num_002_002.txt"、num_002_003.txt"

第一组数字代表它来自哪个包裹",第二组数字只是用来区分它们.所以在这个例子中,我们在包 001 中有一个文件,在包 002 中有三个文件.

我正在编写一个 windows vista 批处理命令来获取所有文件并将它们移动到它们自己的目录中,其中每个目录代表一个不同的包.所以我想将包 001 的所有文件移动到目录001"中,将 002 的所有文件移动到目录002"

我已经成功编写了一个脚本,该脚本将遍历所有 txt 文件并回显它们.我还编写了一个脚本,将一个文件移动到另一个位置,如果目录不存在,则创建该目录.

现在我认为我需要使用子字符串,所以我使用 %var:~start,end% 语法来获取它们.作为测试,我写这个是为了验证我实际上可以提取子字符串并有条件地创建目录

<前>@回声关闭设置 temp=num_001_001.txt如果不存在 %temp:~0,7%mkdir %temp:~0,7%

它有效.伟大的.
然后我给它添加了 for 循环.

@echo 关闭FOR/R %%X IN (*.txt) 做 (设置温度=%%~nX回声目录 %temp:~0,7%)

但这是我的输出:

<前>目录号_002目录号_002目录号_002目录号_002

怎么了?vista 不支持在每次迭代中重新分配变量吗?这四个文件在我的目录中,其中之一应该创建 num_001.我用 003 004 005 放入了不同的文件,所有这些都是最后一个包的名称.我猜我的设置方式有问题.

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

解决方案

你的问题是当批处理器在执行之前读取 for 命令时变量被替换.

试试这个:

SET temp=你好,世界!调用 yourbatchfile.bat

你会看到 Hello 打印了 5 次.

解决办法是延迟扩容;您需要先启用它,然后使用 !temp! 而不是 %temp%:

@echo offSETLOCAL 启用延迟扩展FOR/R %%X IN (*.txt) 做 (设置温度=%%~nXecho 目录 !temp:~0,7!)

请参阅此处了解更多详情.

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"

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.

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"

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.

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

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.

Try this:

SET temp=Hello, world!
CALL yourbatchfile.bat

And you'll see Hello printed 5 times.

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

See here for more details.

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

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