.bat生成包含已删除文件名的txt文件

时间:2016-11-22 21:59:43

标签: batch-file

我希望通过在.abook文件上删除大量文件来生成.bat个文件。应将所述文件命名为已删除文件的文件夹名称。

<?xml version="1.0" encoding="iso-8859-1"?>
<Audiobook>
  <Header>
    <Version>1</Version>
    <Name>###Folder name of dropped files goes here###</Name>
  </Header>
  <FileList>
    <File>###First filename.extension goes here###</File>
    <File>###Second filename.extension goes here###</File>
  </FileList>
</Audiobook>

编辑:

这就是我现在所拥有的。它正在做我想要的,但我一直试图找出如何让它处理多个文件。

@ECHO OFF
for %%* in (.) do set CurrDirName=%%~nx*
echo ^<?xml version="1.0" encoding="iso-8859-1"?^>>%CurrDirName%.abook
echo ^<Audiobook^>>>%CurrDirName%.abook
echo   ^<Header^>>>%CurrDirName%.abook
echo    ^<Version^>1^</Version^>>>%CurrDirName%.abook
echo    ^<Name^>%CurrDirName%^</Name^>>>%CurrDirName%.abook
echo   ^</Header^>>>%CurrDirName%.abook
echo   ^<FileList^>>>%CurrDirName%.abook
FOR %%G IN (%*) DO (
echo    ^<File^>%~nx1^</File^>>>%CurrDirName%.abook
)

当我拖放两个文件(file1,file2)时,这是输出folderName.abook文件

<?xml version="1.0" encoding="iso-8859-1"?>
<Audiobook>
  <Header>
   <Version>1</Version>
   <Name>dev</Name>
  </Header>
  <FileList>
   <File>file1.txt</File>
   <File>file1.txt</File>

如何制作它以便输出所有文件而不仅仅是第一个并重复?

由于

1 个答案:

答案 0 :(得分:0)

在批处理文件中拖动多个文件时,您将获取已删除文件的列表作为以空格分隔的列表。试试这个来测试

@echo %*
@pause

确定集中删除文件的文件夹名称。像这样的东西

for %%* in %* do set CurrDirName=%%~nx*
echo %CurrDirName%

回显文件的第一部分

@echo off
echo ^<?xml version="1.0"?^>^<Audiobook^>^<Header^> ... </FileList>

你将能够替换那里的变量。

然后围绕文件的下一部分执行for循环,并回显列表中的每个文件扩展名。

简单。

完整的文件和路径

FOR %%G IN (%*) DO (
echo    ^<File^>%%G^</File^>>>%CurrDirName%.abook
)

仅限扩展

FOR %%G IN (%*) DO (
echo    ^<File^>%%~xG^</File^>>>%CurrDirName%.abook
)

参考

Variable with modifier  Description

%~G                     Expands %G which removes any surrounding 
                        quotation marks ("").
%~fG                    Expands %G to a fully qualified path name.
%~dG                    Expands %G to a drive letter only.
%~pG                    Expands %G to a path only.
%~nG                    Expands %G to a file name only.
%~xG                    Expands %G to a file extension only.
%~sG                    Expands path to contain short names only.
%~aG                    Expands %G to the file attributes of file.
%~tG                    Expands %G to the date and time of file.
%~zG                    Expands %G to the size of file.
%~$PATH:G               Searches the directories listed in the PATH environment 
                        variable and expands %G to the fully qualified name of 
                        the first one found. If the environment variable name is 
                        not defined or the file is not found by the search,
                        this modifier expands to the empty string.
相关问题