【问题标题】:Creating a file where content is in a batch code创建内容在批处理代码中的文件
【发布时间】:2012-09-26 21:04:35
【问题描述】:

我想从我正在运行的批处理文件中创建另一个文件输出,这意味着原始内容在其中。在这个批处理文件中,它会执行一段时间或 for 循环,直到找到“结束”或我在内容末尾添加的任何唯一单词。

我不想简单地复制文件(包含重要内容),因为这会暴露信息。然后通过批处理调用文件来运行它。

code:
-start of batch
-check if the file a.txt exist del a.txt
-starts to create the a.txt file

>start of content:
>"the quick brown fox jump over the lazy dog"
>lynx: unique word
>end of content

-writes the content to a.txt using for loop or while until it finds the unique word
-runs the a.txt file
-deletes the a.txt file
-exit

希望有人可以提供帮助。谢谢! 或者,如果有人可以提出更好的方法来做到这一点。我会很感激的

【问题讨论】:

  • 为什么需要新建批处理文件?为什么不简单地将代码作为现有批处理文件中的函数调用?

标签: file for-loop batch-file while-loop creation


【解决方案1】:

带有 for 命令的行本身需要引号。

For 循环可以正常工作,直到它们遇到包含引号的行,然后一切都变成 custard...

你不能从这里到达那里。

也许试试 vbscript?

【讨论】:

  • @user1701414 - 实际上,报价没什么大不了的,特别是如果它们是一致的。什么会让一切都转向侧面的是重定向和管道字符,如 |
【解决方案2】:
@echo off
setlocal enabledelayedexpansion
if exist a.txt del a.txt

copy nul a.txt > nul                      & :: Not needed, but in your specs

set line[1]=This is what you asked for.
set line[2]=Not really the best method.
set line[3]=But it will work.
set line[4]=
set line[5]=linux
set line[6]=[end]

for /l %%i in (1,1,1000) do (
  if     "!line[%%i]!"=="linux" goto :end
  if     "!line[%%i]!"=="[end]" goto :end
  if     "!line[%%i]!"=="" echo.>>a.txt
  if NOT "!line[%%i]!"=="" echo !line[%%i]!>>a.txt
)

:end
:: Broken Out Of Loop

ren a.txt a.bat
call a.bat
del a.bat

使用line[n] 方法相当浪费内存,我建议将数据从一个文件移动到另一个文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-25
    • 2017-08-02
    • 1970-01-01
    • 2017-06-18
    • 2013-08-31
    • 1970-01-01
    • 2014-11-28
    相关资源
    最近更新 更多