How to safely append a file name to a Windows folder path argument?

后端 未结 3 852
無奈伤痛
無奈伤痛 2020-12-31 21:26

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 w

3条回答
  •  一个人的身影
    2020-12-31 21:34

    I don't see a way to make it work for all the cases. Your solution will handle all the local paths cases, but to handle UNC or long paths it seems that it is necessary to first correct the path and then append the file data

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for /f "delims=" %%a in ("%~f1."
        ) do for /f "delims=" %%b in ("%%~fa\file.ext"
        ) do echo "%%~fb"
    

    This "should" handle local, network or long paths (the reason for the for /f is the ?), both absolute and relative, and the posibility of no giving a path in the command line.

提交回复
热议问题