Windows 批量从装入的驱动器获取Unix路径

Windows 批量从装入的驱动器获取Unix路径,windows,unix,path,mount,Windows,Unix,Path,Mount,我只是想把这个传下去,因为我花了几天时间才弄明白。我不是一个批处理人员,更像是一个unix人员,但我有一个build.bat,它从安装的unix驱动器运行,并从Windows cmd提示符在Windows和unix上运行构建脚本。我曾经在bat文件中硬编码路径,但我希望能够将脚本放在Unix上的任何位置,并允许用户将源代码签出到自己的沙箱中,装载驱动器open cmd提示符,cd to scripts dir,然后运行构建。有许多进程从该脚本运行。因此,我必须从安装的驱动器导出路径。再一次-我相

我只是想把这个传下去,因为我花了几天时间才弄明白。我不是一个批处理人员,更像是一个unix人员,但我有一个build.bat,它从安装的unix驱动器运行,并从Windows cmd提示符在Windows和unix上运行构建脚本。我曾经在bat文件中硬编码路径,但我希望能够将脚本放在Unix上的任何位置,并允许用户将源代码签出到自己的沙箱中,装载驱动器open cmd提示符,cd to scripts dir,然后运行构建。有许多进程从该脚本运行。因此,我必须从安装的驱动器导出路径。再一次-我相信可能有更简单或更短的方法,但这对我来说很有效:

@echo off

set USERNAME=%1
set PASSWORD=%2

setlocal enabledelayedexpansion

:: All scripts are run from the same script directory on the mounted unix drive
:: 
:: Get the unix path to the scripts directory by using the following command
::
::    net use %~d0
:: 
:: %~d0  - drive letter where the script is running from
:: %~dp0 - complete path to this script
::
set WIN_PATH=%~dp0
set DRIVE_LETTER=%~d0

:: get "Remote Name" from net use command
FOR /F "tokens=*" %%i in ('net use %DRIVE_LETTER% ^| FINDSTR /I "^Remote"') DO SET NET_USE_OUTPUT=%%i

:: Get mounted unix drive from net use drive_letter, returns //ServerName/mount
FOR /F "tokens=3" %%A IN ("%NET_USE_OUTPUT%") DO SET MOUNTED_DRIVE=%%A

::  substitute drive_Letter for mounted_drive \\serverName\mount\path\to\scripts 
set WIN_MOUNTED_PATH=!WIN_PATH:%DRIVE_LETTER%=%MOUNTED_DRIVE%!

::  Change to win to Unix slashes //serverName/mount/path/to/scripts
set UNX_MOUNTED_PATH=%WIN_MOUNTED_PATH:\=/%

:: get SERVER_NAME 
FOR /F "tokens=1 delims=/" %%A IN ("%UNX_MOUNTED_PATH%") DO SET "SERVER_NAME=%%A"

:: Remove //serverName from full path - not needed on unix server
SET "SERVER_NAME_REMOVE=*//%SERVER_NAME%"

:: Remove serverName to get unix path to scripts directory
::  SCRIPTS = /path/to/scripts
CALL SET SCRIPTS=%%UNX_MOUNTED_PATH:!SERVER_NAME_REMOVE!=%%

:: Run script on Unix using plink
pink -pw %PASSWORD% %USERNAME%@%SERVER_NAME% "%SCRIPTS%/run.sh"

:: Run script on Windows
%WIN_PATH%run.bat

欢迎来到堆栈溢出。问一个问题然后自己回答是可以的,但是你可能应该把这篇文章编辑成问题部分,然后把你的答案贴在下面。