如何为Windows中的目录中的每个其他文件创建文本文件?

时间:2015-02-08 10:49:45

标签: windows file batch-file file-io

在这里使用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个文件的文件夹来说耗时且不太理想。我要问的是,有没有办法使用批处理脚本或类似工具自动创建文件?如果有人能够提供使用的代码片段?

提前致谢

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

3 个答案:

答案 0 :(得分:0)

creator.bat:

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

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

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

答案 1 :(得分:0)

@echo off
for %%f in (*.*) do if /i "%%~xf" neq ".txt" echo whatever > %%f.txt

这会将文本whatever放入每个新的.txt文件中。

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

答案 2 :(得分:0)

@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"
    )

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

相关问题