简体   繁体   中英

Filenames with . in batch file

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
    ....

The output looks like this:

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.

As you can see the last echo'ed text is Looping MBP

The file contains lines like these:

[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 (

Any hints?

A bit more explanation why you should always use quotes.

When a path contains parenthesis or ampersands you are in trouble, like in
C:\\Program and files (x86)\\Tools&Help\\bla.txt

When you use it wihout surround it propper with quotes you get a syntax error.

Simplified code

for /f "usebackq" %%j in (%1) do (
    SET var=%~dp1
)

The FOR /F itself works, as long as %1 is surrounded by quotes, but when they are missing it fails.

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

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 \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"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM