防止使用 cmd 覆盖文件(如果存在)

发布于 2024-11-25 14:56:55 字数 658 浏览 1 评论 0原文

我目前正在编写一个执行安装文件的 .bat 批处理文件。在运行安装文件之前,我会检查该目录是否存在,以避免重新安装应用程序。

我通过使用 If Not Exist filename 语句来执行此操作。如果安装的文件不存在,则执行安装文件。

由于某种原因,当我使用已安装的应用程序对其进行测试时,它仍在尝试重新安装该应用程序。

这是我的代码片段:

cd "C:\Documents and Settings\John\Start Menu\Programs\"
pause
If NOT exist "Software Folder"\ (
 start \\filer\repo\lab\"software"\"myapp"\setup.exe
 pause
) 

其中 SoftwareFolder"C:\Documents and Settings\John\Start Menu\Programs\" 的子目录。 我正在检查以查看如果它存在于我的 Programs 文件夹中。

我知道我的 start 命令没有任何问题。我感觉我的开始 CD 命令或其参数之一有问题。

非常感谢,伙计们!

I am currently writing a .bat batch file that executes an installation file. Before it runs the installation file I check to see if the directory exists to avoid re-installing the application.

I do this by using a If Not Exist filename statement. If the installed file doesn't exist, I then execute the installation file.

For some reason, when I test it with the application where it has been installed already, it is still trying to reinstall the application over it.

Here is a snippet of my code:

cd "C:\Documents and Settings\John\Start Menu\Programs\"
pause
If NOT exist "Software Folder"\ (
 start \\filer\repo\lab\"software"\"myapp"\setup.exe
 pause
) 

Where SoftwareFolder is a subdirectory of "C:\Documents and Settings\John\Start Menu\Programs\". I am checking to see if it exists in my Programs folder.

I know nothing is wrong with my start command. I have a feeling something is wrong with my beginning CD command or one of its parameters.

Thanks a lot, guys!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

呢古 2024-12-02 14:56:55

在 If Not Exist 代码中使用文件夹的完整路径。然后你甚至不再需要 CD:

If Not Exist "C:\Documents and Settings\John\Start Menu\Programs\SoftWareFolder\"

Use the FULL path to the folder in your If Not Exist code. Then you won't even have to CD anymore:

If Not Exist "C:\Documents and Settings\John\Start Menu\Programs\SoftWareFolder\"
时光暖心i 2024-12-02 14:56:55

我注意到一些问题,对于刚开始使用的人或缺乏经验的用户来说,了解这些问题可能会很有用。首先...

CD /D "C:\Documents and Settings\%username%\Start Menu\Programs\"

有两件事,一是 CD 后面的 /D 可能有助于确保更改目录,但这并不是真正必要的,第二,如果您要将其从一个用户传递到另一个用户,您必须添加,而不是您的名字,代码 %username%,这使得代码可以在任何计算机上使用,只要它们将您的 setup.exe 文件放在与您在计算机上相同的位置即可。当然,确保这一点更加困难。
另外...

start \\filer\repo\lab\"software"\"myapp"\setup.exe

这里的启动代码可以这样设置,但正确的语法是

start "\\filter\repo\lab\software\myapp\" setup.exe

这将运行:setup.exe,位于:\filter\repo\lab...etc.\

I noticed some issues with this that might be useful for someone just starting, or a somewhat inexperienced user, to know. First...

CD /D "C:\Documents and Settings\%username%\Start Menu\Programs\"

two things one is that a /D after the CD may prove to be useful in making sure the directory is changed but it's not really necessary, second, if you are going to pass this from user to user you have to add, instead of your name, the code %username%, this makes the code usable on any computer, as long as they have your setup.exe file in the same location as you do on your computer. of course making sure of that is more difficult.
also...

start \\filer\repo\lab\"software"\"myapp"\setup.exe

the start code here, can be set up like that, but the correct syntax is

start "\\filter\repo\lab\software\myapp\" setup.exe

This will run: setup.exe, located in: \filter\repo\lab...etc.\

匿名的好友 2024-12-02 14:56:55

正如 Escobar Ceaser 的回答一样,我建议在整个路径周围使用引号。这是将整个路径包装在“”中的常见方法,而不仅仅是路径中单独的目录名称。

我有一个类似的问题,它对我不起作用。但无法选择在路径中使用“”作为单独的目录名称,因为路径包含环境变量,而环境变量本身涵盖多个目录层次结构。结论是我错过了结束符 " 和 ( 之间的空格

正确的版本,括号前有空格,将是

If NOT exist "C:\Documents and Settings\John\Start Menu\Programs\Software Folder" (
 start "\\filer\repo\lab\software\myapp\setup.exe"
 pause
) 

As in the answer of Escobar Ceaser, I suggest to use quotes arround the whole path. It's the common way to wrap the whole path in "", not only separate directory names within the path.

I had a similar issue that it didn't work for me. But it was no option to use "" within the path for separate directory names because the path contained environment variables, which theirself cover more than one directory hierarchies. The conclusion was that I missed the space between the closing " and the (

The correct version, with the space before the bracket, would be

If NOT exist "C:\Documents and Settings\John\Start Menu\Programs\Software Folder" (
 start "\\filer\repo\lab\software\myapp\setup.exe"
 pause
) 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文