批处理文件 - 如何做所有特定变量的质量变化使用循环 [英] Batch files - How to do mass change of all specific variables using for loop

查看:169
本文介绍了批处理文件 - 如何做所有特定变量的质量变化使用循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下code发生在一个批处理文件的所有命令行参数。在我来说,我有大约30命令行参数,它们是1,2或3。我拿他们那么所有的数字要重新分配给其他字符。我希望每个变种,如果是1,将其更改为/ *,如果它是一个2,将其更改为* /,如果它是一个3,将其更改为#

第一部分的伟大工程,它的重新分配,我不能得到语法的第二部分。

  SETLOCAL ENABLEDELAYEDEXPANSION
集数= 1
FOR %% I IN(*%)DO(
  设置无功!算!= %%我
  设置VAR算= VAR算:!!!!!1 = / *! < - 不工作
  设置VAR算= VAR算:!!!!!2 = * /! < - 不工作
  设置VAR算= VAR算:!!!!!3 =#! < - 不工作
  设置/计数=!算!+1


解决方案

我认为杰布有简单的解决方案,但也有其他的选择。

1),你可以指望的当前值转移到FOR变量。这是我倾向于做。

  SETLOCAL ENABLEDELAYEDEXPANSION
集数= 1
FOR %% I IN(*%)DO(
  对于%% N的(!算!)做(
    设置VAR %% N = %%我
    设置!VAR %% N = VAR %% N:1 = / *!
    设置VAR %% N = VAR %% N:!2 = * /!
    设置VAR %% N = VAR %% N:!3 =#!
  )
  设置/计数+ = 1

2)您可以用CALL延迟外变量的扩张,而是因为正常扩张并不如延迟扩展为安全的,我不喜欢这个选项。

  SETLOCAL ENABLEDELAYEDEXPANSION
集数= 1
FOR %% I IN(*%)DO(
  设置VAR!算!= %%我
  呼叫建立变种数= %% VAR算:!!!!1 = / * %%
  呼叫建立变种数= %% VAR算:!!!!2 = * / %%
  呼叫建立变种数= %% VAR算:!!!!3 =#%%
  设置/计数+ = 1

The following code takes in all command line parameters of a batch file. In my case I have about 30 command line parameters and they are all numbers of 1, 2, or 3. I take them in then want to reassign them to other characters. I want each var, if it is a 1, change it to /*, if it's a 2, change it to */, if it's a 3, change it to #.

The first part works great, it's the second part of reassigning that I can't get the syntax for.

SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
  set var!count!=%%i
  set var!count!=!var!count!:1=/*!  <--don't work
  set var!count!=!var!count!:2=*/!  <--don't work
  set var!count!=!var!count!:3=#!   <--don't work
  set /a count=!count!+1
)

解决方案

I think jeb has the simplest solution, but there are other options.

1) You can transfer the current value of count into a FOR variable. This is how I tend to do it.

SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
  for %%N in (!count!) do (
    set "var%%N=%%i"
    set "var%%N=!var%%N:1=/*!"
    set "var%%N=!var%%N:2=*/!"
    set "var%%N=!var%%N:3=#!"
  )
  set /a count+=1
)

2) You can use CALL to delay expansion of the outer variable, but I don't like this option because the normal expansion is not as safe as delayed expansion.

SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
  set "var!count!=%%i"
  call set "var!count!=%%var!count!:1=/*%%"
  call set "var!count!=%%var!count!:2=*/%%"
  call set "var!count!=%%var!count!:3=#%%"
  set /a count+=1
)

这篇关于批处理文件 - 如何做所有特定变量的质量变化使用循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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