目录的批处理脚本或更好的方法

发布于 2024-08-28 19:21:23 字数 683 浏览 10 评论 0原文

考虑为我的应用程序创建一个简单的批处理文件。我的应用程序在运行时需要一些目录就位。

我想到的第一种方法就是制作一个批处理脚本:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隔纱相望 2024-09-04 19:21:23

问题可能是您将 GOTO 目标视为方法起点。它们只是文件中的标签。这意味着

IF NOT EXIST C:\App GOTO :CREATE ELSE GOTO :DONTCREATE

执行

:CREATE

后继续执行脚本,直到

:DONTCREATE

到达文件末尾。如果您想在 :CREATE 完成后转到其他地方,则必须添加另一个 GOTO。通常的情况是在末尾告诉它 GOTO :EOF(内置标签),如下所示:

@ECHO OFF
IF EXIST C:\App GOTO :DONTCREATE

:CREATE
MKDIR C:\App\Code
ECHO DIRECTORY CREATED
GOTO :EOF

:DONTCREATE
ECHO IT WAS ALREADY THERE

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

IF NOT EXIST C:\App GOTO :CREATE ELSE GOTO :DONTCREATE

execution picks up with

:CREATE

and then continues on down the script right through

:DONTCREATE

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:

@ECHO OFF
IF EXIST C:\App GOTO :DONTCREATE

:CREATE
MKDIR C:\App\Code
ECHO DIRECTORY CREATED
GOTO :EOF

:DONTCREATE
ECHO IT WAS ALREADY THERE
谷夏 2024-09-04 19:21:23

您可以直接在 C# 中执行所有目录操作,如果这样更容易的话:

if (!Directory.Exists(@"c:\app\code")
{
     Directory.CreateDirectory(@"c:\app\code");
}
else
{
     Console.WriteLine("Directory already exists!");
}

请参阅此页面以获取更多信息: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:

if (!Directory.Exists(@"c:\app\code")
{
     Directory.CreateDirectory(@"c:\app\code");
}
else
{
     Console.WriteLine("Directory already exists!");
}

See this page for more info: http://msdn.microsoft.com/en-us/library/wa70yfe2%28v=VS.100%29.aspx

别低头,皇冠会掉 2024-09-04 19:21:23

最简单的答案可能是您已经想到的 - 在应用程序运行时从应用程序创建目录。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文