【问题标题】:How do I create a text file for every other file in a directory in windows?如何为 windows 目录中的每个其他文件创建一个文本文件?
【发布时间】:2015-02-08 10:49:44
【问题描述】:

在这里使用 Windows 完成新手。

我有一个程序,要求我为文件夹中的每个文件都有一个文本文件,其中包含有关该文件的信息。文本文件还必须与它们描述的文件具有相同的文件名,例如在这样的文件夹中:

file1.abc
file2.abc
file3.abc
file4.abc

我需要具备以下条件:

file1.abc
file1.abc.txt
file2.abc
file2.abc.txt
file3.abc
file3.abc.txt
file4.abc
file4.abc.txt

到目前为止,我一直在手动完成这一切,对于包含超过 50 到 100 个文件的文件夹来说,这既耗时又不理想。我要问的是,有没有办法使用批处理脚本或类似的方法来自动创建文件?是否有人能够提供代码 sn-p 使用?

提前致谢

P.S 如果有帮助,我使用的是 Windows 7 64 位。

【问题讨论】:

    标签: windows file batch-file file-io


    【解决方案1】:

    creator.bat:

    @echo off
    for %%f in (*.abc) do echo %%f > %%f.txt
    

    如果您的文件名包含空格,它会稍微复杂一些:

    @echo off
    for %%f in (*.abc) do echo %%f > "%%~f.txt"
    

    【讨论】:

      【解决方案2】:
      @echo off
      for %%f in (*.*) do if /i "%%~xf" neq ".txt" echo whatever > %%f.txt
      

      这会将文本 whatever 放置在创建的每个新 .txt 文件中。

      已具有扩展名 .txt 的文件除外。

      【讨论】:

        【解决方案3】:
        @echo off
            setlocal enableextensions disabledelayedexpansion
        
            for %%a in (*) do if not exist "%%a.txt" (
                set "create=1"
                if "%%~xa"==".txt" for %%b in ("%%~na") do if not "%%~xb"=="" set "create="
                if defined create type nul > "%%a.txt"
            )
        

        这应该只在它们不存在时创建所有必需的文件,而不是覆盖以前生成的文件并避免生成未生成的文件。

        【讨论】:

          猜你喜欢
          • 2021-03-26
          • 1970-01-01
          • 1970-01-01
          • 2015-01-18
          • 2015-08-29
          • 2019-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多