如何摆脱扩展的`forfiles`变量中的括号双引号?

时间:2015-09-10 02:00:49

标签: batch-file for-loop cmd quotes

forfiles命令建立了几个变量,由前导@表示,它将有关当前迭代项的数据返回给循环体。

与迭代项的路径和名称相关的所有变量都返回""中包含的值。这些是:@file@fname@ext@path@relpath

那么:你怎么能摆脱封闭的双引号?

例如,以下代码返回给定根目录中文本文件的相对路径:

forfiles /P "C:\root" /M "*.txt" /C "cmd /C echo @relpath"

假设C:\root包含两个文件file1.txtfile2.txt,输出将为:

".\file1.txt"
".\file2.txt"

但是,我想要的文件列表没有周围的""

我正在使用Windows 7 64位。

1 个答案:

答案 0 :(得分:4)

一种方法是在for %I中嵌套forfiles循环并使用%~I扩展 - 在命令提示符窗口中使用此代码:

forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %I in (@relpath) do echo %~I"

要在批处理文件中使用该代码,您必须将% - 符号加倍:

forfiles /P "C:\root" /M "*.txt" /C "cmd /Q /C for %%I in (@relpath) do echo %%~I"

返回的文件列表将是(依赖原始问题中的示例文件):

.\file1.txt
.\file2.txt

另一种变体是将另一个forfiles嵌套在初始的forfiles主体中,因为/C在给定字符串中删除(非转义)双引号,如forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\"" 之后的命令行:

forfiles

或者(双重内部forfiles /P "C:\root" /M "*.txt" /C "forfiles forfiles /P @path\.. /M @file /C \"cmd /C echo @relpath\"" 是故意的,这可以解决一个错误 - 请参阅this post):

forfiles

内部@relpath将枚举一个项目,即外部循环传递的项目。由于.\file1.txt .\file2.txt 在执行内循环时已经展开,因此引号会被删除,因为它们不会被转义。

所以返回的文件列表看起来像(再次从原始问题中获取样本文件):

forfiles

行之间的额外换行符由forfiles生成。您可以使用重定向来避免这种情况(取消echo输出,但在控制台窗口中仅显示> nul forfiles /P "C:\root" /M "*.txt" /C "cmd /C forfiles /P @path\.. /M @file /C 0x22cmd /C > con echo @relpath0x22" 输出):

{% extends "base.html" %}
{% load static %}
{% block stylesheets %}
<!--<html ng-app="ui.bootstrap.demo">-->
<link href="{% static 'bootstrap-v3.3.2/css/bootstrap.min.css' %}" rel="stylesheet" xmlns="http://www.w3.org/1999/html">
<link href="{% static 'bootstrap-v3.3.2/css/bootstrap-theme.min.css' %}" rel="stylesheet">
<link href="{% static 'css/screen.css' %}" rel="stylesheet">
<link href="{% static 'css/project.css' %}" rel="stylesheet">
<link href="{% static 'css/jquery-ui.css' %}" rel="stylesheet">
<link href="{% static 'css/ss.css' %}" rel="stylesheet">
<link href="{% static 'css/check_binning.css' %}" rel="stylesheet">


{% endblock %}

{% block mainbody %}
{%  include 'orc_navigation_section.html' %}

<br><br>
<h1> <b>Check Binning Setup</b></h1><br>
<div class="well">
        <div class="well" ng-controller="LayerCtrl">
            <table>
                <tr>
                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">Fab</span>
                            <select class="form-control" name="fab">
                                {% for f in fab %}
                                <option value="{{f}}">{{f}}</option>
                                {% endfor %}
                            </select>
                        </div>
                    </td>

                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">Technode</span>
                            <select class="form-control" ng-model="selected_technode" ng-options="l.value as l.label for l in technodes"></select>
                        </div>
                    </td>

                    <td>
                        <div class="input-group">
                            <span class="input-group-addon">Layer</span>
                            <!--<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">-->
                            {%verbatim%}
                            <!--<select class="form-control"  name="layer" >-->
                            <!--<option ng-repeat="l in layer_list" value="{{l}}">{{l}}</option>-->
                            <!--</select>-->
                            <select class="form-control" ng-model="selected_layer" ng-options="l as l for l in layerlist"></select>
                            {%endverbatim%}
                        </div>
                    </td>
                </tr>
            </table>
        </div>

        <div><h3>ORC binning setup history:</h3></div>
        <br/><br/>

        <!--Check fields and Radio Buttons-->
        <div ng-controller="AlertDemoCtrl">

            <table class="table">

                {% verbatim %}

                <tr>

                    <td class="input-group" ng-repeat="(k,v) in alerts">

                        <span class="input-group-addon" ng-hide="v.hide">Check {{ k }}</span>
                    <span ng-hide="v.hide">
                    <input type="text" class="form-control" placeholder="Add Check here" ng-model="v.input">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <b>Functional check</b>&nbsp;&nbsp;&nbsp;<input type="radio" name="{{ k }}" value="fc" ng-model="v.props">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <b>CFM issue</b>&nbsp;&nbsp;&nbsp;<input type="radio" name="{{ k }}" value="cfm" ng-model="v.props">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <b>Defect risk</b>&nbsp;&nbsp;&nbsp;<input type="radio" name="{{ k }}" value="dr" ng-model="v.props">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <a type="reset" ng-click="reset()">
                            <span class="glyphicon glyphicon-repeat"></span>
                        </a>&nbsp;&nbsp;&nbsp;

                        <a href="" ng-click="remove(v)">
                            <span class="glyphicon glyphicon-trash"></span></a>
                        </span>
                        {% endverbatim %}
                <tr>
                    <td>
                        <button type="button" class='btn btn-info' ng-click="addAlert()">
                            <span class="glyphicon glyphicon-plus"></span>
                            &nbsp;Add Check</button>
                        <button type="reset" ng-click="reset()" class="btn btn-danger">
                            <span class="glyphicon glyphicon-repeat"></span>
                            &nbsp;Reset All</button>
                    </td>
                </tr>
            </table>
        </div>
        <table class="table">
            <!--IMPORT FILE-->
            <tr>
                <td>
                    <div style="position:relative;">
                        <a class='btn btn-primary' href='javascript:;' disabled>
                            Choose File...
                            <input type="file" style='position:absolute;z-index:2;top:0;left:0;
                        filter: alpha(opacity=0);-ms-filter:"progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
                        opacity:0;background-color:transparent;color:transparent;' name="file_source" size="40"  onchange='$("#upload-file-info").html($(this).val());'>
                        </a>
                        &nbsp;
                        <span class='label label-info' id="upload-file-info"></span>
                        <button ng-click="uploadAttachFile()" class="btn btn-small btn-default" id="uploadfile" name="uploadfile" disabled>
                            <span class="glyphicon glyphicon-cloud-upload"></span>&nbsp;Import File</button>
                    </div>
                </td>
            </tr>

            <!--ADD COMMENTS-->

            <tr>
                <td>
                    Add Comments*<div class="span5"><textarea name="bugnote_text" placeholder="Add comments here (max=600 characters)" rows="3" class="span10 ng-pristine
                ng-valid ng-valid-maxlength" ng-maxlength="600"></textarea></div>
                </td>
                <td>
                    Add ECN*<input type="text" class="form-control" placeholder="Add ECN here">

                </td>
            </tr>
            <form ng-submit="submit()" ng-controller="ExampleController">
                <tr>
                    <td>
                        <button type="submit" id="submit" class="btn btn-small btn-success"><span class="glyphicon glyphicon-pencil"></span>&nbsp;Update check DB</button>
                        <button class="btn btn-small btn-grey"><span class="glyphicon glyphicon-remove"></span>&nbsp;Cancel</button>
                    </td>

                </tr>
            </form>
            <!--<tr><td><div ng-controller="ModalDemoCtrl">-->
            <!--<script type="text/ng-template" id="orc_enable.html">-->
            <!--</script>-->
            <!--<button type="button" class="btn btn-default" ng-click="open('lg')">Pop-up</button>-->
            <!--</div></td></tr>-->
        </table>
</div>

<script>
  angular.module('submitExample', [])
    .controller('ExampleController', ['$scope', function($scope) {
      $scope.list = [];
      $scope.text = 'hello';
      $scope.submit = function() {
        if ($scope.text) {
          $scope.list.push(this.text);
          $scope.text = '';
        }
      };
    }]);
</script>

{% endblock %}