如何将文件名安全追加到Windows文件夹路径参数? [英] How to safely append a file name to a Windows folder path argument?

查看:138
本文介绍了如何将文件名安全追加到Windows文件夹路径参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假如我有一个批处理脚本,预计参数的文件夹路径%1 。我想一个文件名附加到路径和使用中的命令。有一个简单的方式的方式,在所有情况下可靠做到这一点?

Suppose I have a batch script that expects a folder path in argument %1. I want to append a file name to the path and use that in a command. Is there a simple way to do this in a way that is reliable in all situations?

我不想 PUSHD%1 和根本无视路径之后。假设我的逻辑,需要将当前目录保持不变。

I do not want to PUSHD %1 and simply ignore the path afterward. Assume my logic requires the current directory to remain unchanged.

问题是%1 参数可能会或可能不会有终止反斜杠 - 即 C:\\路径 C:\\路径\\

The issue is the %1 argument may or may not have a terminating backslash - i.e. c:\path or c:\path\.

通常情况下,我可以简单地使用%〜1 \\ file.ext,因为Windows将C:\\路径\\ file.extC:\\路径\\\\ file.ext。相同

Typically I can simply use "%~1\file.ext" because Windows treats "c:\path\file.ext" and "c:\path\\file.ext" identically.

但有一个讨厌的情况下 - C:\\

But there is one nasty case - c:\

以下命令将产生此错误:文件名,目录名或卷标语法不正确

The following commands produce this error: The filename, directory name, or volume label syntax is incorrect.

dir "c:\\file.ext"
del "c:\\file.ext"

奇怪的是,这些命令只是正常工作:

The odd thing is, these commands work just fine:

if exist "c:\\file.ext" echo OK
echo text>"c:\\file.ext"
copy "c:\\file.ext" "somePath"
xcopy "c:\\file.ext" "somePath"

有另外一个问题的情况下 - C:,这意味着在驱动器C的当前目录,如果我追加一个反斜杠:,会给错误的结果。 C:file.ext是不一样的C:\\ file.ext。

There is another problematic case - c:, meaning the current directory on drive C:, will give the wrong result if I append a backslash. "c:file.ext" is not the same as "c:\file.ext".

有没有一种简单的方法来安全地附加一个文件名到任何一个文件夹的路径参数?

我不关心UNC路径,因为我不经常使用他们,我相信有不玩好与UNC路径某些命令。

I am not concerned with UNC paths, as I do not use them very often, and I believe there are some commands that do not play nice with UNC paths.

注:这是我张贴配对的问题和答案的情况。这个问题一直在我的身边了数年的刺,我只是最近发现,我认为别人可能有兴趣在一个简单的解决方案。

推荐答案

编辑 - 答案改成解决MC ND的评论

EDIT - answer changed to address MC ND's comment

解决方案是如此简单:-) %〜F1 \\ file.ext

The solution is so simple :-) "%~f1.\file.ext"

的Windows文件名不能以结束<空间> ,让所有的Windows命令简单地忽略尾随 <空间方式> 文件/文件夹名称

Windows file names cannot end with . or <space>, so all Windows commands simply ignore trailing . and <space> in file/folder names.

和一个以下反斜杠重新presents该位置的当前目录。

And a single . following a backslash represents the current directory at that location.

假设当前工作目录是 C:\\测试,然后下面的表格显示不同的值是如何被解决:

Assume the current working directory is c:\test, then the table below show how various values get resolved:

"%~1"        | "%~f1.\file.ext"       | equivalent to
-------------+------------------------+------------------------
"c:\folder\" | "c:\folder\.\file.ext" | "c:\folder\file.ext"
             |                        |
"c:\folder"  | "c:\folder.\file.ext"  | "c:\folder\file.ext"
             |                        |
"c:\"        | "c:\.\file.ext"        | "c:\file.ext"
             |                        |
"c:"         | "c:\test.\file.ext"    | "c:\test\file.ext"
             |                        |
"."          | "c:\test.\file.ext"    | "c:\test\file.ext"
             |                        |
".."         | "c:\.\file.ext"        | "c:\file.ext"
             |                        |
<no value>   | "c:\test.\file.ext"    | "c:\test\file.ext"  I hadn't thought of this case
                                                            until I saw MC ND's answer

您可以使用for循环很容易地减少除权pression到规范化等价形式:

You can easily reduce the expression into the canonical equivalent form using a FOR loop:

for %%A in ("%~f1.\file.ext") do echo "%%~fA"

哈利·约翰斯顿在他的评论中指出,该解决方案无法使用UNC路径根( \\\\服务器\\共享名)或长路径(工作\\\\。\\ C:\\?somePath )。请参见 MC ND的回答如果您需要支持那些情形。该哈利·约翰斯顿答案是简单,但它不支持任何给定参数的情况下。

As Harry Johnston states in his comment, this solution does not work with UNC root paths (\\server\shareName) or long paths (\\?\c:\somePath). See MC ND's answer if you need to support either of those cases. The Harry Johnston answer is simpler, but it does not support the case of no argument given.

这篇关于如何将文件名安全追加到Windows文件夹路径参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆