代码之家  ›  专栏  ›  技术社区  ›  m_pGladiator

如何从批处理文件创建空文本文件?

  •  218
  • m_pGladiator  · 技术社区  · 17 年前

    11 回复  |  直到 7 年前
        1
  •  384
  •   ephemient    17 年前
    copy NUL EmptyFile.txt

    DOS在每个目录中都有一些特殊的文件(实际上是设备), NUL 相当于UNIX的 /dev/null :这是一个神奇的文件,它总是空的,并且扔掉你写的任何东西。这里有一个 list 其他一些; CON

    为了避免任何输出,您可以使用

    copy /y NUL EmptyFile.txt >NUL

    /y copy 从问一个你看不到输出何时开始的问题 努尔 .

        2
  •  246
  •   TheSmurf    17 年前
    echo. 2>EmptyFile.txt
    
        3
  •  195
  •   Kevin K    14 年前
    type NUL > EmptyFile.txt
    

    NUL ,就像上一篇文章一样,它的旁边看起来很漂亮 ECHO OutputLineFromLoop >> Emptyfile.txt 这通常会在批处理文件中出现。

        4
  •  71
  •   Community Mohan Dere    9 年前

    生成一个0字节的文件 一种非常清晰、向后兼容的方式:

    type nul >EmptyFile.txt
    

    anonymous Danny Backett ,可能是其他人,我的灵感来自 JdeBP's work

    以另一种方式创建一个0字节的文件 ,它是向后兼容的:

    REM. >EmptyFile.txt
    

    创意途径: Johannes

    第三种方式的0字节文件 向后看:

    echo. 2>EmptyFile.txt
    

    创意途径: TheSmurf

    系统化的0字节文件 probably 从Windows 2000开始提供:

    fsutil file createnew EmptyFile.txt 0
    

    创意途径: Emm

    ATTRIB -R filename.ext>NUL
    (CD.>filename.ext)2>NUL
    

    创意途径: copyitright

    一条新线 0x0D 0x0A 在里面 hex notation ,或写成 \r\n ):

    echo.>AlmostEmptyFile.txt
    

    注: 间隔 echo , . >

    创意途径: How can you echo a newline in batch files?


    编辑 看来 任何无效的 命令 重定向 对文件的复制将创建一个空文件。嘿,一个特征!

    TheInvisibleFeature <nul >EmptyFile.txt
    

    0字节文件:无效命令/具有随机名称

    %RANDOM%-%TIME:~6,5% <nul >EmptyFile.txt
    

    维娅:太好了 source 洪惠恩随机

    编辑2 安德烈M points out 通过无效命令实现这一点可能是最有趣/最刺激的方法

    0字节的文件:无效命令/古怪的方式

    *>EmptyFile.txt
    

    创意途径: Andriy M

    :

    break > file.txt
    

    创意途径: foxidrive comment 属于 Double Gras !

        5
  •  26
  •   Johannes    16 年前

    REM.>空文件

        6
  •  9
  •   script'n'code    8 年前

    ATTRIB -R filename.ext
    CD .>filename.ext
    

    如果不存在文件,只需执行以下操作:

    CD .>filename.ext
    

    (根据DodgyCodeException的评论更新/更改代码)

    ATTRIB -R filename.ext>NUL
    (CD .>filename.ext)2>NUL
    
        7
  •  7
  •   display-name-is-missing    12 年前
    fsutil file createnew file.cmd 0
    
        8
  •  7
  •   Nam G VU    11 年前

    书中还有一个要补充的内容——简短而甜美。

    break>file.txt
    break>"file with spaces in name.txt"
    
        9
  •  2
  •   Kevin K    14 年前

    TYPE 命令代替 COPY . 试试这个:

    TYPE File1.txt>File2.txt
    

    哪里 File1.txt 是空的。

        10
  •  1
  •   Sunil Hari    12 年前

    你也可以使用 SET byte 文件如下

    set x=x > EmptyFile.txt
    

    或者,如果不想创建额外的变量,请重新分配现有变量,如

    set PROMPT=%PROMPT% > EmptyFile.txt
    

    或者像这样:

    set "PROMPT=%PROMPT%" > EmptyFile.txt
    
        11
  •  0
  •   HaxAddict1337    6 年前

    有无限的方法。

    break
    cls
    color
    goto
    pushd
    popd
    prompt
    title
    

    奇怪的命令:

    CD.
    REM.
    @echo off
    cmd /c
    START >FILE
    

    过时的 print 命令生成一个空白文件:

    print /d:EMPTY_TEXT_FILE nul
    
        12
  •  -3
  •   Batchman    12 年前

    echo. > Filename.txt

        13
  •  -4
  •   Wolfpack'08 bstar55    6 年前

    重要:

    Set-Content "your_ignore_file.txt" .gitignore -Encoding utf8 这是区分大小写的 并强制utf8编码!