目录的批处理脚本或更好的方法
考虑为我的应用程序创建一个简单的批处理文件。我的应用程序在运行时需要一些目录就位。
我想到的第一种方法就是制作一个批处理脚本:
@ECHO OFF
IF NOT EXIST C:\App GOTO :CREATE ELSE GOTO :DONTCREATE
:CREATE
MKDIR C:\App\Code
ECHO DIRECTORY CREATED
:DONTCREATE
ECHO IT WAS ALREADY THERE
1)这不会按我的预期运行。 :CREATE
和 :DONTCREATE
似乎都在运行?那么我该如何正确地做“如果”呢?
输出:
A subdirectory or file C:\App\Code already exists.
DIRECTORY CREATED
IT WAS ALREADY THERE
所以它同时输入了 true 和 false 语句?
2) 该应用程序是一个 C# WPF 应用程序。对于我在这里尝试做的事情(如果它们尚不存在,则创建几个目录)-我应该以其他方式执行此操作吗?也许在应用程序运行时?
编辑:好的,很高兴只用 C# 代码来做 - 但有人可以解释我的批处理的问题吗?
Looking at creating a simple batch file for my app. My app needs some directories to be in place as it runs.
The first method I thought was just make a batch script:
@ECHO OFF
IF NOT EXIST C:\App GOTO :CREATE ELSE GOTO :DONTCREATE
:CREATE
MKDIR C:\App\Code
ECHO DIRECTORY CREATED
:DONTCREATE
ECHO IT WAS ALREADY THERE
1) This doesn't run as I would expect. Both :CREATE
and :DONTCREATE
seem to run regardless? How do I do an If properly then?
Output:
A subdirectory or file C:\App\Code already exists.
DIRECTORY CREATED
IT WAS ALREADY THERE
So it enters both true and false statements?
2) The app is a C# WPF app. For what I am trying to do here (create a couple of directories if they don't already exist) - should I do it some other way? Perhaps in the application as it runs?
edit: Ok, happy to just do in C# code - but can anyone explain the problem with my batch?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题可能是您将 GOTO 目标视为方法起点。它们只是文件中的标签。这意味着
执行
后继续执行脚本,直到
到达文件末尾。如果您想在 :CREATE 完成后转到其他地方,则必须添加另一个 GOTO。通常的情况是在末尾告诉它 GOTO :EOF(内置标签),如下所示:
the problem is perhaps that you think of the GOTO targets as method start points. They are just labels in the file. This means that after
execution picks up with
and then continues on down the script right through
until the end of the file is reached. You have to add another GOTO if you want to go somewhere else after :CREATE finishes. The usual case is to tell it to GOTO :EOF (a built-in label) at the end, as follows:
您可以直接在 C# 中执行所有目录操作,如果这样更容易的话:
请参阅此页面以获取更多信息:http://msdn.microsoft.com/en-us/library/wa70yfe2%28v=VS.100%29.aspx
You can do all the directory stuff directly in C#, if that would be easier:
See this page for more info: http://msdn.microsoft.com/en-us/library/wa70yfe2%28v=VS.100%29.aspx
最简单的答案可能是您已经想到的 - 在应用程序运行时从应用程序创建目录。
DirectoryInfo.Create() 是您需要的方法。
Simplest answer is probably what you've already thought of - to create the directory from the application as it runs.
DirectoryInfo.Create() is the method you'll need.