批处理文件:特定文件夹列表RAR文件,并写入到结果的文本文件 [英] batch file: list rar file in specific folder and write result into text file

查看:88
本文介绍了批处理文件:特定文件夹列表RAR文件,并写入到结果的文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹,并包含一些RAR文件和子文件夹。这些子文件夹包含RAR文件或子文件夹(递归结构)。我想要写一个批处理文件,列出该文件夹树(完整路径)所有的RAR文件。结果被写入到文本文件中。

例如:

特定文件夹中:

   - 任务
--Quest - 1.rar
--Quest - 2.rar
--Quest - 子 - 3.rar
--Quest - CON - 4.rar
--Quest - 数学 - ams.doc

和在运行的Result.txt批处理文件后的结果:

   -  \\任务\\ 1.rar
- \\任务\\ 2.rar
- \\任务\\子\\ 3.rar
- \\任务\\刀豆\\ 4.rar


解决方案

我不能添加为注释,因此它张贴作为回答。

命令目录的第三个参数是路径+文件规范。因此,整个参数应该用双引号括起来,而不是人的路。

这是正确的,一个环境变量的值是一个可以包含已双引号的字符串。在任何地方使用命令的参数这里面的环境变量,它不可避免地发生了双引号现在参数字符串中。

大多数编译器的启动code处理这种情况现在的自变量中除去双引号是正确的。但是,正确的行为将是批处理文件首先使用参数里面的环境变量之前将删除环境变量字符串双引号,而是把整个参数为双引号。

解析C命令行参数解释的命令行分析器与Microsoft Visual C / C ++,并没有使用自定义的开机code创建的应用程序。不幸的是没有什么说明与没能逃脱双引号发生争吵字符串中。

正如我曾经想知道什么是由操作系统传递给应用程序,应用程序如何解析命令行,我写了一个小C code,各种编译器编译它,并进行了一些测试,这些控制台在各种Windows版本的应用程序。

下面是这个小控制台应用程序的code:

 的#include<&stdio.h中GT; //像printf()
#包括LT&;&CONIO.H GT; //为残培()INT主(INT ARGC,CHAR *的argv [])
{
   INT i编号;
   炭sSpace [2] =;   的printf(该计划的论据是:\\ n \\ n);
   如果(ARGC小于10)* sSpace ='\\ 0';
   对于(i编号= 0; i编号< ARGC; i编号++)
   {
      的printf(参数%S%D:%S \\ n,i编号小于10 sSpace:,i编号的argv [i编号]);
   }
   的printf(\\ n preSS任意键退出...);
   残培();
   的printf(\\ n \\ n);
   返回(0);
}

我是在参数0最感兴趣的我的测试 - 应用程序的名字,因为我想知道是否可以直接用来获取INI文件的名称。这是不可能的!

我编这个小code有一个非常古老的Turbo C编译器生成一个16位DOS应用程序,与DJGPP造成了16位头一个32位控制台应用程序,并用Visual C产生一个​​真正的32比特控制台应用程序。

编译控制台应用程序有名字ArgCheck.exe,位于C:\\温度这是在开幕的Windows XP SP3的x86命令提示符窗口的当前工作目录

所使用的命令行是: ArgCheck.exe%WINDIR%\\ *。exe文件

ArgCheck.exe的使用Visual C编译的输出:

 参数0:C:\\ TEMP \\ ArgCheck.exe
参数1:C:\\ WINDOWS \\ * TXT

ArgCheck.exe与DJGPP编译输出:

 参数0:C:\\ TEMP \\ ARGCHECK.EXE
参数1:C:\\ WINDOWS \\ * TXT

ArgCheck.exe与Turbo C的编译输出:

 参数0:C:\\ TEMP \\ ARGCHECK.EXE
参数1:C:\\ WINDOWS
说法二:\\ *。TXT

正如你所看到的,用Turbo C的启动code也省略了双引号,但是在参数中的双引号是PTED作为参数结束,结果是相互$ P $ 2个参数,而不是仅1

结论:
应该经常进行测试,如果任何地方存在的参数而不是在双引号只在开始和参数字符串的结束应用程序的启动code如何解析命令行参数。

I have a folder and contains some rar files and subfolders. These subfolders contain rar file or subfolder (recursive structure). I want to write a batch file that list all the rar files in this folder tree (full path). The result is written into a text file.

Example:

Specific folder:

--Quest
--Quest--1.rar
--Quest--2.rar
--Quest--Sub--3.rar
--Quest--Con--4.rar
--Quest--Math--ams.doc

And the result after running batch file in result.txt:

--\Quest\1.rar
--\Quest\2.rar
--\Quest\Sub\3.rar
--\Quest\Con\4.rar

解决方案

I can't add this as comment and therefore post it as answer.

The third argument of command dir is path+file specification. Therefore the entire argument should be surrounded by double quotes and not just the path.

It is right that the value of an environment variable is a string which can contain already double quotes. When using this environment variable anywhere inside an argument of a command, it happens inevitably that the double quotes are now inside the argument string.

The startup code of most compilers handle this case now correct by removing the double quotes within the argument. But the correct behavior would be that the batch file removes the double quotes from the environment variable string first before using the environment variable inside an argument and instead put the entire argument into double quotes.

Parsing C Command-Line Arguments explains the command line parser of applications created with Microsoft Visual C/C++ and no customized startup code used. Unfortunately there is no description what happens with not escaped double quotes inside an argument string.

As I once wanted to know what is passed by the operating system to an application and how the application parses the command line, I wrote a little C code, compiled it with various compilers and ran some tests with those console applications on various Windows versions.

Here is the code of this little console application:

#include <stdio.h>  // for printf()
#include <conio.h>  // for getch()

int main (int argc, char *argv[])
{
   int  iNumber;
   char sSpace[2] = " ";

   printf("The arguments of the program are:\n\n");
   if(argc < 10) *sSpace = '\0';
   for(iNumber = 0; iNumber < argc; iNumber++)
   {
      printf("Argument %s%d: %s\n",iNumber < 10 ? sSpace : "",iNumber,argv[iNumber]);
   }
   printf("\nPress any key to exit ...");
   getch();
   printf("\n\n");
   return(0);
}

I was most interested on my tests in argument 0 - the name of the application as I wanted to know if it can be used directly to get name of an INI file. This is not possible!

I compiled this little code with a very old Turbo C compiler producing a 16-bit DOS application, with DJGPP resulting in a 32-bit console application with a 16-bit header and with Visual C producing a real 32-bit console application.

The compiled console application has name ArgCheck.exe and is located in C:\Temp which is the current working directory in a command prompt window opened on Windows XP SP3 x86.

The command line used was: ArgCheck.exe "%WINDIR%"\*.exe

The output of ArgCheck.exe compiled with Visual C:

Argument 0: C:\Temp\ArgCheck.exe
Argument 1: C:\WINDOWS\*.txt

The output of ArgCheck.exe compiled with DJGPP:

Argument 0: C:\TEMP\ARGCHECK.EXE
Argument 1: C:\WINDOWS\*.txt

The output of ArgCheck.exe compiled with Turbo C:

Argument 0: C:\TEMP\ARGCHECK.EXE
Argument 1: C:\WINDOWS
Argument 2: \*.txt

As you can see, the startup code of Turbo C omits also the double quotes, but the double quote within the argument was interpreted as end of argument and the result are 2 arguments instead of only 1.

Conclusion: It should be always tested how the startup code of an application parses the command line arguments if double quotes exist anywhere within an argument instead of only at begin and end of the argument string.

这篇关于批处理文件:特定文件夹列表RAR文件,并写入到结果的文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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