【问题标题】:Have a text file created AND have it say it was created if it doesn't exist in batch创建一个文本文件并让它说如果它不存在批量创建它
【发布时间】:2016-03-17 13:11:27
【问题描述】:

所以,我想编写一行简单的代码,使批处理文件创建一个文本文档,同时回显“Achievement Get: Start the game”

if not exist "achievements\Start the Game.txt" echo Achievement Get: Start the Game! >> "achievements\Start the Game.txt"

(批处理文件已经将CD设置为文件夹位置,所以不是因为我没有完整的位置”

玩弄它,它确实会创建文件,但批处理文件从来没有说“成就获取:开始游戏!”相反,它在运行时创建的文本文件中表示。

简短版本:如果文件尚不存在,我希望批处理文件在文件夹中创建一个文本文件,但我也希望它说文件已创建。

提前致谢。

【问题讨论】:

    标签: batch-file if-statement echo


    【解决方案1】:

    您可以检查文件是否退出:

    #!/bin/bash
    file="achievements\Start the Game.txt"
    if [ -f "$file" ]
    then
        echo "$file found."
    else
        echo "$file not found."
    fi
    

    所以这可以写成

    #!/bin/bash
    file="/etc/hosts"
    if [ -f "$file" ]
    then
        ...do what you will
    
    else
        echo "Achievement Get: Start the Game!" > $file
    fi
    

    编辑:注意这是 Linux

    【讨论】:

      【解决方案2】:
      if not exist "achievements\Start the Game.txt" copy nul "achievements\Start the Game.txt">nul&echo Achievement Get: Start the Game!
      

      你没有说你想在文件中放什么。这将使它的长度为零。

      >nul 抑制 file copied 消息。

      & 分隔要级联的命令。

      使用也有效

      if not exist "achievements\Start the Game.txt" (
       copy nul "achievements\Start the Game.txt">nul
       echo Achievement Get: Start the Game!
      )
      

      【讨论】:

        【解决方案3】:

        我做了一些研究并想出了这个

         @echo off
         if not exist "\achievements\Start the Game.txt" goto a
          EXISTS ALREADY
          PAUSE 
          EXIT
                :A
                cd "\achivements"
        
                echo. 2>  "Start the Game.txt"
                echo Achievement Get: Start the game !
                pause
        

        应该有效

        【讨论】:

          猜你喜欢
          • 2022-12-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-07-20
          • 2013-11-05
          相关资源
          最近更新 更多