从CMD文件中获得的字符串并将其设置为一个变量CD使用 [英] CMD get string from file and SET it as a variable to use in cd

查看:741
本文介绍了从CMD文件中获得的字符串并将其设置为一个变量CD使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的批处理文件,我试着写一做我的工作的一部分(我知道偷懒右)
到目前为止,我有以下...

I'm new to batch files and I'm trying to write one to do part of my work (i know lazy right) So far I have the following...

SET skip=1

REM for all the directories indicated to contain core repositories
FOR /F "skip=%skip% delims=" %%i IN (C:\Repos.txt) DO ( 
SET TgtDir =%%i
echo %TgtDir% >> C:\result.txt
)

Repos.txt的内容是:

60000

C:\\ somedir \\ someotherdir \\

C:\\ A \\ B \\ C \\

基本上我想这个剧本要经过一个文件,忽略这将用于以后的延迟设置的第一行,并提取每行则(理想情况下)把它传递给一个cd命令,但现在我只是想把它存入变量TgtDir。

Basically I want this script to go through a file, ignoring the first line which will be used for a delay setting later, and extract each line then (ideally) pass it to a cd command but for now I'm just trying to get it into the variable TgtDir.

当我运行此脚本在C输出:\\的Result.txt是:

ECHO处于开启状态。

ECHO处于开启状态。

When i run this script the output in C:\result.txt is:
ECHO is on.
ECHO is on.

任何帮助吗?

推荐答案

您会想看看 EnableDelayedExpansion 选项​​批处理文件。从以上链接:

You'll want to look at the EnableDelayedExpansion option for batch files. From the aforementioned link:

延迟的变量扩充与for循环工作时是非常有用。通常情况下,环整个被评为即使它跨越了一个批处理脚本的多行一个命令。

Delayed variable expansion is often useful when working with FOR Loops. Normally, an entire FOR loop is evaluated as a single command even if it spans multiple lines of a batch script.

所以,你的脚本最终将看起来像这样:

So your script would end up looking like this:

@echo off
setlocal enabledelayedexpansion
SET skip=1

REM for all the directories indicated to contain core repositories
FOR /F "skip=%skip% delims=" %%i IN (C:\Repos.txt) DO (
    SET TgtDir=%%i
    echo !TgtDir! >> C:\result.txt
)

作为替代方案,只需要使用 %%我变量在你的内循环,而不是创建一个新的变量。

As an alternative, just use the %%i variable in your inner loop, rather than creating a new variable.

这篇关于从CMD文件中获得的字符串并将其设置为一个变量CD使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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