文件名。在批处理文件 [英] Filenames with . in batch file
问题描述
我有一个Windows批处理文件,以前工作,但因为我改变笔记本电脑,它失败。
I have a Windows batch file that used to work but since I changed laptop, it fails.
我猜这是由我的文件路径但我没能找到解决这个问题的方法。
I guess it's caused by points (.) in my file path but I haven't been able to find a way around this issue.
我基本上传递一个文件名到我的批处理文件,让它处理它,但它失败,当它开始从文件中读取行:
I basically pass a file name to my batch file and let it process it but it fails when it starts reading lines from the file:
echo MBP File: %1
rem Check that the file is a MapBasic Project File
if /I "%~sx1" NEQ ".mbp" (
echo Error: file %~dpnx1 is not a MapBasic Project File ^(^*.mbp^)
goto :EOF
) else (
echo file %1 is a MapBasic Project File ^(^*.mbp^)
)
echo Looping MBP
for /f "usebackq skip=1 delims== tokens=2" %%j in (%1) do (
echo Checking input file from MBP
echo j: %%j
SET filemb=%~dp1%%j
....
输出如下所示:
file "D:\Dropbox (Some-Name)\3. MB_Kode\mbInfoSelHandler\mbcode\InfoSelHandler.mbp" is a MapBasic Project File (*.mbp)
Looping MBP
\3. was unexpected at this time.
正如你可以看到最后一个echo'ed文本是 Looping MBP
As you can see the last echo'ed text is Looping MBP
该文件包含以下行:
[LINK]
Application=..\InfoSelHandler.mbx
Module=Library\ARRAYLib.mbo
Module=Library\CONFIGFILELib.mbo
Module=Library\DEBUGLib.mbo
我假设这行有一个问题,但我不确定:
I'm assuming there's an issue in this line, but I'm not sure:
for /f "usebackq skip=1 delims== tokens=2" %%j in (%1) do (
任何提示?
推荐答案
更多解释为什么你应该总是使用引号。
A bit more explanation why you should always use quotes.
当路径包含括号或&符号时, b $ b C:\程式和档案(x86)\Tools& Help\bla.txt
When a path contains parenthesis or ampersands you are in trouble, like in
C:\Program and files (x86)\Tools&Help\bla.txt
简化代码
for /f "usebackq" %%j in (%1) do (
SET var=%~dp1
)
FOR / F
本身可以工作,只要%1
被引号包围,但是当它们缺失时它失败。
The FOR /F
itself works, as long as %1
is surrounded by quotes, but when they are missing it fails.
但是 SET var =%〜dp1
打破代码,因为它在FOR块被解析为
But the SET var=%~dp1
breaks the code, as it's expanded when the FOR block is parsed to
set var=C:\Program and files (x86)\Tools&Help\bla.txt
右括号 x86)
关闭FOR块, \Tools& Help \bla.txt
位于块外,语法错误。
The closing parenthesis of x86)
closes the FOR block and the \Tools&Help\bla.txt
are outside of the block and creates a syntax error.
for /f "usebackq" %%j in (%1) do (
SET var=C:\Program and files (x86)\Tools&Help\bla.txt
)
在您的情况下,您应该将代码修改为
In your case, you should modify the code to
for /f "usebackq skip=1 delims== tokens=2" %%j in ("%~1") do (
echo Checking input file from MBP
echo j: %%j
SET "filemb=%~dp1%%j"
)
这篇关于文件名。在批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!