【问题标题】:.bat script to remove trailing whitespace.bat 脚本删除尾随空格
【发布时间】:2015-05-11 00:58:45
【问题描述】:

我对通过 Windows 编写脚本相当陌生。也许我什至需要脚本以外的东西。我在 linux 中使用 sed 和 csv 文件做了一些类似的事情,但我想制作一个 .bat 脚本文件(或类似的东西),它会接收一个 c 头文件并删除所有尾随空格。

编辑原因:(我的 Svn 存储库不允许尾随空格,并且在使用 C++ UI 表单时,每次添加新对象时都会添加带有尾随空格的 cmets,例如按钮)

编辑: 它发生在如下所示的 cmets 之后

        "// 
         // outputStringsTab
         // "

示例代码段:

#pragma region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      void InitializeComponent(void)
      {
         this->tabCollection = (gcnew System::Windows::Forms::TabControl());
         this->outputStringsTab = (gcnew System::Windows::Forms::TabPage());
         this->justificationComboBox = (gcnew System::Windows::Forms::ComboBox());
         this->stringSizeDropBox = (gcnew System::Windows::Forms::ComboBox());
         this->stringFontLabel = (gcnew System::Windows::Forms::Label());
         this->label7 = (gcnew System::Windows::Forms::Label());
         this->stringSizeLabel = (gcnew System::Windows::Forms::Label());
         this->stringBackgroundLabel = (gcnew System::Windows::Forms::Label());
         this->fontCheckListBox = (gcnew System::Windows::Forms::CheckedListBox());
         this->greenRadio = (gcnew System::Windows::Forms::RadioButton());
         this->blueRadio = (gcnew System::Windows::Forms::RadioButton());
         this->whiteRadio = (gcnew System::Windows::Forms::RadioButton());
         this->blackRadio = (gcnew System::Windows::Forms::RadioButton());
         this->label4 = (gcnew System::Windows::Forms::Label());
         this->label3 = (gcnew System::Windows::Forms::Label());
         this->label2 = (gcnew System::Windows::Forms::Label());
         this->label1 = (gcnew System::Windows::Forms::Label());
         this->shapePage = (gcnew System::Windows::Forms::TabPage());
         this->circleAngleGroupBox = (gcnew System::Windows::Forms::GroupBox());
         this->circleAngleLabel = (gcnew System::Windows::Forms::Label());
         this->circleAngleStartTextBox = (gcnew System::Windows::Forms::TextBox());
         this->circleAngleEndTextBox = (gcnew System::Windows::Forms::TextBox());
         this->circleAngleButton = (gcnew System::Windows::Forms::Button());
         this->startAngleLabel = (gcnew System::Windows::Forms::Label());
         this->degreesLabel2 = (gcnew System::Windows::Forms::Label());
         this->endAngleLabel = (gcnew System::Windows::Forms::Label());
         this->degreesLabel1 = (gcnew System::Windows::Forms::Label());
         this->lineSuppresionGroupBox = (gcnew System::Windows::Forms::GroupBox());
         this->squareLineLabel = (gcnew System::Windows::Forms::Label());
         this->noneSuppressRadio = (gcnew System::Windows::Forms::RadioButton());
         this->topSuppressRadio = (gcnew System::Windows::Forms::RadioButton());
         this->bottomSuppressRadio = (gcnew System::Windows::Forms::RadioButton());
         this->leftSuppressRadio = (gcnew System::Windows::Forms::RadioButton());
         this->rightSuppressRadio = (gcnew System::Windows::Forms::RadioButton());
         this->sizeGroupBox = (gcnew System::Windows::Forms::GroupBox());
         this->circleRadioButton = (gcnew System::Windows::Forms::RadioButton());
         this->shapesHeightWidthLabel = (gcnew System::Windows::Forms::Label());
         this->squareRadioButton = (gcnew System::Windows::Forms::RadioButton());
         this->heightTextBox = (gcnew System::Windows::Forms::TextBox());
         this->heightLabel = (gcnew System::Windows::Forms::Label());
         this->sizeSendButton = (gcnew System::Windows::Forms::Button());
         this->widthLabel = (gcnew System::Windows::Forms::Label());
         this->widthTextBox = (gcnew System::Windows::Forms::TextBox());
         this->gaugeTabPage = (gcnew System::Windows::Forms::TabPage());
         this->gaugeValueGroupBox = (gcnew System::Windows::Forms::GroupBox());
         this->barValueRadioButton = (gcnew System::Windows::Forms::RadioButton());
         this->label5 = (gcnew System::Windows::Forms::Label());
         this->meterValueRadioButton = (gcnew System::Windows::Forms::RadioButton());
         this->label6 = (gcnew System::Windows::Forms::Label());
         this->arcValueRadioButton = (gcnew System::Windows::Forms::RadioButton());
         this->gaugeValueTrackBar = (gcnew System::Windows::Forms::TrackBar());
         this->trackBarLabel = (gcnew System::Windows::Forms::Label());
         this->tabCollection->SuspendLayout();
         this->outputStringsTab->SuspendLayout();
         this->shapePage->SuspendLayout();
         this->circleAngleGroupBox->SuspendLayout();
         this->lineSuppresionGroupBox->SuspendLayout();
         this->sizeGroupBox->SuspendLayout();
         this->gaugeTabPage->SuspendLayout();
         this->gaugeValueGroupBox->SuspendLayout();
         (cli::safe_cast<System::ComponentModel::ISupportInitialize^>(this->gaugeValueTrackBar))->BeginInit();
         this->SuspendLayout();
         // 
         // tabCollection
         // 
         this->tabCollection->Controls->Add(this->outputStringsTab);
         this->tabCollection->Controls->Add(this->shapePage);
         this->tabCollection->Controls->Add(this->gaugeTabPage);
         this->tabCollection->Location = System::Drawing::Point(12, 12);
         this->tabCollection->Name = L"tabCollection";
         this->tabCollection->SelectedIndex = 0;
         this->tabCollection->Size = System::Drawing::Size(827, 396);
         this->tabCollection->TabIndex = 0;
         // 
         // outputStringsTab
         // 
         this->outputStringsTab->BackColor = System::Drawing::Color::White;
         this->outputStringsTab->Controls->Add(this->justificationComboBox);
         this->outputStringsTab->Controls->Add(this->stringSizeDropBox);
         this->outputStringsTab->Controls->Add(this->stringFontLabel);
         this->outputStringsTab->Controls->Add(this->label7);
         this->outputStringsTab->Controls->Add(this->stringSizeLabel);
         this->outputStringsTab->Controls->Add(this->stringBackgroundLabel);
         this->outputStringsTab->Controls->Add(this->fontCheckListBox);
         this->outputStringsTab->Controls->Add(this->greenRadio);
         this->outputStringsTab->Controls->Add(this->blueRadio);
         this->outputStringsTab->Controls->Add(this->whiteRadio);
         this->outputStringsTab->Controls->Add(this->blackRadio);
         this->outputStringsTab->Controls->Add(this->label4);
         this->outputStringsTab->Controls->Add(this->label3);
         this->outputStringsTab->Controls->Add(this->label2);
         this->outputStringsTab->Controls->Add(this->label1);
         this->outputStringsTab->Location = System::Drawing::Point(4, 22);
         this->outputStringsTab->Name = L"outputStringsTab";
         this->outputStringsTab->Padding = System::Windows::Forms::Padding(3);
         this->outputStringsTab->Size = System::Drawing::Size(819, 370);
         this->outputStringsTab->TabIndex = 0;
         this->outputStringsTab->Text = L"Output Strings";
         // 
         // justificationComboBox
         // 
         this->justificationComboBox->FormattingEnabled = true;
         this->justificationComboBox->Items->AddRange(gcnew cli::array< System::Object^  >(3) { L"Left", L"Middle", L"Right" });

...等等

【问题讨论】:

  • 为什么这是一个 C 或 C++ 问题?我建议删除 C 和 C++ 标记,因为您正在谈论使用 MSDOS 脚本语言的批处理文件。
  • 我的 Svn 存储库不允许尾随空格,并且在使用 C++ UI 表单时,它会在每次添加新对象时添加带有尾随空格的 cmets,例如按钮
  • 如果这是关于 C 或 C++ 的,您将向我们展示一个修剪尾随空白的 C 或 C++ 程序。
  • 刚刚添加 ;) 在这里询问的新手
  • 我实际上是在谈论 C,因为这是关于生成这个的 C windows 窗体。正如我在问题“a c 头文件”中所说的那样

标签: c++ c windows batch-file scripting


【解决方案1】:

Windows 批处理脚本不提供图灵完备的编程语言,主要用于顾名思义 - 将多个命令批处理在一起(否则您会从命令行执行)。现代 Windows 确实提供了大量对 MS-DOS 批处理文件的扩展,但它仍然不是满足您要求的合适工具。

Windows 现在包含PowerShell 作为基本命令行处理器和批处理文件的替代品;使用它可能会更好,但是如果您可能正在使用 C 和 C++,那么使用您已经熟悉的语言编写自己的实用程序就足够简单了?

【讨论】:

    【解决方案2】:

    修剪空间

    filter ts {l|r|lr|lc count}
    

    修剪一行中的前导和尾随空格。

    l - 删除前导空格。

    r - 删除尾随空格。

    lr - 删除前导和尾随空格。

    lc - 删除 &lt;count&gt; 前导空格。

    示例

    修复win.ini,不需要修复,发送到屏幕

    filter trimspace lc 5 <"%systemroot%\win.ini"
    

    vbs 脚本

    Set Arg = WScript.Arguments
    set WshShell = createObject("Wscript.Shell")
    Set Inp = WScript.Stdin
    Set Outp = Wscript.Stdout
    
    If LCase(Arg(1)) = "l" then 
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            outp.writeline LTrim(Line)
        Loop
    ElseIf LCase(Arg(1)) = "r" then
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            outp.writeline RTrim(Line)
        Loop
    ElseIf LCase(Arg(1)) = "lr" then
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            outp.writeline Trim(Line)
        Loop
    ElseIf LCase(Arg(1)) = "lc" then
        Count = Cint(LCase(Arg(2)))
        Do Until Inp.AtEndOfStream
            Line=Inp.readline
            If Left(Line,Count) = Space(Count) then
                outp.writeline Mid(Line, Count +1)
            Else
                outp.writeline LTrim(Line)
            End If
        Loop
    End If
    

    使用批处理文件来启动它

      cscript //nologo <pathtovbsscript\vbsscript.vbs> lc 5 <file.h
    

    一般用途

    过滤器用于命令提示符。 Filter.vbs 必须与 cscript.exe 一起运行。如果您只是输入 filter,它将运行一个批处理文件,该文件会自动执行此操作。

     filter subcommand [parameters]
    

    过滤器仅读取和写入标准输入和标准输出。这些仅在命令提示符下可用。

    filter <inputfile >outputfile
    filter <inputfile | other_command
    other_command | filter >outputfile
    other_command | filter | other_command
    

    【讨论】:

      猜你喜欢
      • 2015-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-05
      • 2015-07-06
      • 1970-01-01
      • 2016-09-10
      相关资源
      最近更新 更多