批处理脚本 - 创建.XML文件 - 基于文件夹中的每个.DWG文件的名称

时间:2022-10-26 02:32:18

I'd like to create a Batch file that would search a folder and create .XML files based on the .DWG file names. It would also add the file name to (2) difference locations within the .XML (see below).

我想创建一个Batch文件,它将搜索文件夹并根据.DWG文件名创建.XML文件。它还会将文件名添加到(2).XML中的差异位置(见下文)。

Process:

  1. A folder contains (100) .DWG files numbered A1.dwg ~ A100.dwg
  2. 文件夹包含编号为A1.dwg~A100.dwg的(100).DWG文件

  3. The Batch file would find these file names and create the corresponding .XML file
  4. 批处理文件将找到这些文件名并创建相应的.XML文件

  5. Within the .XML file (2) lines would be edited based on the file name
  6. 在.XML文件(2)中,将根据文件名编辑行

XML Contents for file "A26":

文件“A26”的XML内容:

<?xml version="1.0" encoding="UTF-8"?>
<File Name="A26"><Version>1</Version><Description>A26</Description><Region><Cell Level="1" Division="1"/><Cell Level="1" Division="1"/></Region></File>

XML Contents for file "A42":

文件“A42”的XML内容:

<?xml version="1.0" encoding="UTF-8"?>
<File Name="A42"><Version>1</Version><Description>A42</Description><Region><Cell Level="2" Division="1"/><Cell Level="2" Division="1"/></Region></File>

XML Contents for file "A74":

文件“A74”的XML内容:

<?xml version="1.0" encoding="UTF-8"?>
<File Name="A74"><Version>1</Version><Description>A74</Description><Region><Cell Level="3" Division="1"/><Cell Level="3" Division="1"/></Region></File>

I was able to put together the following based on another inquiry, however, it's not working for me... anything you can offer would be much appreciated.

我能够根据另一个询问汇总以下内容,但是,这对我不起作用......你能提供的任何东西都会非常感激。

BTW, "targetdir=C:\USC VILLAGE" is the location I'm trying to output these files, but I'd like to be able to edit this path and create these files anywhere if possible.

BTW,“targetdir = C:\ USC VILLAGE”是我正在尝试输出这些文件的位置,但我希望能够编辑此路径并在可能的情况下在任何地方创建这些文件。

@ECHO OFF
SETLOCAL
SET "targetdir=C:\USC VILLAGE"
SET /a filecount=0
SET "and_subdirs="
:again
PUSHD "%targetdir%"
FOR %and_subdirs% %%a IN (
  "*.dwg"
 ) DO (
  SET /a filecount+=1
  >"%%~dpna.xml" (
   ECHO(^<?xml version="1.0" encoding="UTF-8"?>
   ECHO(         <File Name="%%~na">
   ECHO(         <Version>1</Version>
   ECHO(         <Description>%%~nxa"</Description>
   ECHO(         </File^>
  )
  )
  )
)
popd
IF NOT DEFINED and_subdirs IF %filecount%==0 SET "and_subdirs=/r"& GOTO again
ECHO(%filecount% files found
IF DEFINED and_subdirs ECHO(Subdirectories were scanned

GOTO :EOF

Thanks, DOrtega

2 个解决方案

#1


0  

I know this is not quite what you ask for, but I'm going to offer it anyway. It's perl, rather than batch because:

我知道这不是你要求的,但无论如何我都会提供它。这是perl,而不是批处理,因为:

  • Perl will run on Windows too.
  • Perl也将在Windows上运行。

  • Perl handles XML a lot better than batch ever can.
  • Perl处理XML比批处理要好得多。

To get this to run 'from scratch you would need to:

要从头开始运行,您需要:

  • Install perl (from somewhere like: http://www.activestate.com/activeperl/downloads )
  • 安装perl(来自某个地方:http://www.activestate.com/activeperl/downloads)

  • install XML::Twig - if you're running Activeperl via ppm install XML-Twig.
  • 安装XML :: Twig - 如果您通过ppm安装XML-Twig运行Activeperl。

I'm unclear how you figured out cell level and division, so have used placeholder values. It should be easy to modify this accordingly.

我不清楚你是如何计算出细胞水平和分裂的,所以使用了占位符值。相应地修改它应该很容易。

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;
use File::Find;

#read search and output dirs from command line, but use defaults if not present. 
my ( $search_dir, $output_dir ) = @_;
$search_dir = 'C:\\temp\\dwg' unless defined $search_dir;
$output_dir = 'C:\\temp'      unless defined $output_dir;

sub process_file {
    return unless m/\.dwg$/;
    print "Processing $File::Find::name\n";
    my $base_name = $_;
    #extract the 'A24' bit. 
    $base_name =~ s#\.dwg$##gi;  
    print "Using $base_name\n";

    #start assembling XML:
    my $xml = XML::Twig->new( pretty_print => 'indented' );

    #create a 'File' roote element.
    $xml->set_root(
        XML::Twig::Elt->new( 'File', { "Name", $File::Find::name } ) );

    #insert Version and description into root element.
    $xml->root->insert_new_elt( 'Version',     1 );
    $xml->root->insert_new_elt( 'Description', $base_name );

    #create a region element, with subelements 'Cell'
    my $region = $xml->root->insert_new_elt( 'last_child', 'Region' );
    $region->insert_new_elt(
        'Cell',
        {   'Level'    => 1,
            'Division' => 1,
        }
    );
    $xml->print;

    open( my $output_file, ">", "$output_dir/$base_name.xml" ) or warn $!;
    print {$output_file} $xml->sprint;
    close($output_file);
}


find( \&process_file, $search_dir );

#2


0  

Your script could work if you will escape all < (less than) and > (greater than) characters with ^ Escape character properly within all echo commands (not in the >"%%~dpna.xml" ( line) as follows:

如果您将在所有echo命令(而不是在“%% ~dpna.xml”(行)中)正确地使用^ Escape字符转义所有<(小于)和>(大于)字符,则脚本可以正常工作,如下所示:

  >"%%~dpna.xml" (
    ECHO(^<?xml version="1.0" encoding="UTF-8"?^>
    ECHO(^<File Name="%%~na"^>
    ECHO(    ^<Version^>1^</Version^>
    ECHO(    ^<Description^>%%~nxa^</Description^>
    ECHO(^</File^>
   )

However, I doubt the output file could be forced to UTF-8 encoding with CLI batch (read more in this CHCP article at least).

但是,我怀疑输出文件可以强制使用CLI批处理进行UTF-8编码(至少在这篇CHCP文章中阅读更多内容)。

I'd follow Sobrique's recommendation: switch to another, xml properly handling tool...

我遵循Sobrique的建议:切换到另一个,xml正确处理工具......

#1


0  

I know this is not quite what you ask for, but I'm going to offer it anyway. It's perl, rather than batch because:

我知道这不是你要求的,但无论如何我都会提供它。这是perl,而不是批处理,因为:

  • Perl will run on Windows too.
  • Perl也将在Windows上运行。

  • Perl handles XML a lot better than batch ever can.
  • Perl处理XML比批处理要好得多。

To get this to run 'from scratch you would need to:

要从头开始运行,您需要:

  • Install perl (from somewhere like: http://www.activestate.com/activeperl/downloads )
  • 安装perl(来自某个地方:http://www.activestate.com/activeperl/downloads)

  • install XML::Twig - if you're running Activeperl via ppm install XML-Twig.
  • 安装XML :: Twig - 如果您通过ppm安装XML-Twig运行Activeperl。

I'm unclear how you figured out cell level and division, so have used placeholder values. It should be easy to modify this accordingly.

我不清楚你是如何计算出细胞水平和分裂的,所以使用了占位符值。相应地修改它应该很容易。

#!/usr/bin/perl

use strict;
use warnings;

use XML::Twig;
use File::Find;

#read search and output dirs from command line, but use defaults if not present. 
my ( $search_dir, $output_dir ) = @_;
$search_dir = 'C:\\temp\\dwg' unless defined $search_dir;
$output_dir = 'C:\\temp'      unless defined $output_dir;

sub process_file {
    return unless m/\.dwg$/;
    print "Processing $File::Find::name\n";
    my $base_name = $_;
    #extract the 'A24' bit. 
    $base_name =~ s#\.dwg$##gi;  
    print "Using $base_name\n";

    #start assembling XML:
    my $xml = XML::Twig->new( pretty_print => 'indented' );

    #create a 'File' roote element.
    $xml->set_root(
        XML::Twig::Elt->new( 'File', { "Name", $File::Find::name } ) );

    #insert Version and description into root element.
    $xml->root->insert_new_elt( 'Version',     1 );
    $xml->root->insert_new_elt( 'Description', $base_name );

    #create a region element, with subelements 'Cell'
    my $region = $xml->root->insert_new_elt( 'last_child', 'Region' );
    $region->insert_new_elt(
        'Cell',
        {   'Level'    => 1,
            'Division' => 1,
        }
    );
    $xml->print;

    open( my $output_file, ">", "$output_dir/$base_name.xml" ) or warn $!;
    print {$output_file} $xml->sprint;
    close($output_file);
}


find( \&process_file, $search_dir );

#2


0  

Your script could work if you will escape all < (less than) and > (greater than) characters with ^ Escape character properly within all echo commands (not in the >"%%~dpna.xml" ( line) as follows:

如果您将在所有echo命令(而不是在“%% ~dpna.xml”(行)中)正确地使用^ Escape字符转义所有<(小于)和>(大于)字符,则脚本可以正常工作,如下所示:

  >"%%~dpna.xml" (
    ECHO(^<?xml version="1.0" encoding="UTF-8"?^>
    ECHO(^<File Name="%%~na"^>
    ECHO(    ^<Version^>1^</Version^>
    ECHO(    ^<Description^>%%~nxa^</Description^>
    ECHO(^</File^>
   )

However, I doubt the output file could be forced to UTF-8 encoding with CLI batch (read more in this CHCP article at least).

但是,我怀疑输出文件可以强制使用CLI批处理进行UTF-8编码(至少在这篇CHCP文章中阅读更多内容)。

I'd follow Sobrique's recommendation: switch to another, xml properly handling tool...

我遵循Sobrique的建议:切换到另一个,xml正确处理工具......