标题: [文件操作] 批处理怎样根据已知的文件短路径获取完整的长路径? [打印本页]
作者: dishuo 时间: 2008-8-18 19:33 标题: 批处理怎样根据已知的文件短路径获取完整的长路径?
本帖最后由 pcl_test 于 2016-8-11 23:50 编辑
求这样的批处理:传递给他一个短文件名,返回对应的长文件名。
即:call 长文件名.bat C:\PROGRA~1\COMMON~1\MICROS~1\DAO\dao360.dll
返回正确的长文件名:
"C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll"
环境:windows XP
要求:尽量不使用第三方软件,只使用xp自带的程序。
我的实现方法:
@echo off
if "%1"=="" goto callmyself
set longpath=%~d1
set s=%~dpfxs1
set s=%s:\= %
call :getfullname %s%
echo "%longpath%"
goto :eof
:callmyself
call %0 C:\PROGRA~1\COMMON~1\MICROS~1\DAO\dao360.dll
goto :eof
:getfullname
if "%2"=="" goto :eof
if not "%3"=="" (
for /f "tokens=*" %%i in ('dir /ad /b "%longpath%\%2*"') do set longpath=%longpath%\%%i
shift
goto getfullname
) else (
for /f "tokens=*" %%i in ('dir /b "%longpath%\%2*"') do set longpath=%longpath%\%%i
goto :eof
)
[ 本帖最后由 dishuo 于 2008-8-18 19:50 编辑 ]
作者: pusofalse 时间: 2008-8-18 19:58
- @echo off&setlocal enabledelayedexpansion
- set "acc=C:\PROGRA~1\COMMON~1\MICROS~1\DAO\dao360.dll"
- for /f "delims=" %%a in ("%acc%") do set "access=%%~da\"&set "file=%%~nxa"s
- for %%i in ("%access%" "%file%") do set "acc=!acc:%%~i=!"
- set "acc=%acc:\= %"
- for %%a in (%acc%) do (
- set "ac=%%a"
- call :lp
- )
- echo %access%%file%
- pause>nul&exit/b
- :lp
- for /f "delims=" %%a in ('dir/b "%access%%ac:~,6%*"') do (
- set "access=!access!%%a\"
- )
复制代码
作者: dishuo 时间: 2008-8-18 21:40
受教了!
学习之后修改如下,目前测试没有发现问题.
@echo off&setlocal enabledelayedexpansion
cls
if "%1"=="" goto callmyself
call :getfullname %1
echo "%filepath%"
echo "%filename%"
echo "%fileext%"
pause
:getfullname
::需要 setlocal enabledelayedexpansion
::使用 %filepath%\%filename%.%fileext%
set "p=%~s1"
set "filepath=%~d1"
set "filename=%~nxs1"
set "fileext=%~x1"
for %%i in (%filepath% %filename%) do set "p=!p:%%i=!"
set "p=%p:\= %"
for %%i in (%p%) do for /f "tokens=*" %%j in ('dir /ad /b "!filepath!\%%i*"') do set "filepath=!filepath!\%%j"
for /f "tokens=*" %%i in ('dir /b "%filepath%\%filename%*"') do set "filename=%%i"
set "filename=!filename:%~x1=!
set "fileext=%fileext:.=%"
exit /b
:callmyself
call %0 C:\PROGRA~1\COMMON~1\MICROS~1\DAO\dao360.dll
[ 本帖最后由 dishuo 于 2008-8-18 22:24 编辑 ]
作者: dejoy 时间: 2010-8-29 09:50
2楼结果正确,运行结果如下:
C:\Program Files\Common Files\Microsoft Shared\DAO\dao360.dll
3楼结果不正确,运行结果如下:
"C:\Program Files\Common Files\Microsoft Shared\DAO"
"dao360"
"dll"
请按任意键继续. . .
作者: tiandyoin 时间: 2024-10-26 15:21
本帖最后由 tiandyoin 于 2024-10-26 15:24 编辑
回复 2# pusofalse - :GetFullpath
- @echo off&setlocal enabledelayedexpansion
- echo 短名: %~s1
- REM set "acc=C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT"
- set "acc=%~s1"
- for /f "delims=" %%a in ("%acc%") do set "access=%%~da\"&set "file=%%~nxa"s
- for %%i in ("%access%" "%file%") do set "acc=!acc:%%~i=!"
- set "acc=%acc:\= %"
- for %%a in (%acc%) do (
- set "ac=%%a"
- call :lp
- )
- echo %access%%file%
- pause>nul&exit/b
- :lp
- echo "%access%%ac:~,6%*"
- for /f "delims=" %%a in ('dir/b "%access%%ac:~,6%*"') do (
- set "access=!access!%%a\"
- set ac&pause
- )
- @goto :EOF
复制代码
- D:\Users\test\Desktop\1_2 3>1.bat C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT
- 短名: C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT
- "C:\Users*"
- ac=Users
- acc=Users test AppData Roaming MICROS~1 Windows SendTo
- access=C:\Users\
- 请按任意键继续. . .
- "C:\Users\test*"
- ac=test
- acc=Users test AppData Roaming MICROS~1 Windows SendTo
- access=C:\Users\test\
- 请按任意键继续. . .
- "C:\Users\test\AppDat*"
- 找不到文件
- "C:\Users\test\Roamin*"
- 找不到文件
- "C:\Users\test\MICROS*"
- 找不到文件
- "C:\Users\test\Window*"
- 找不到文件
- "C:\Users\test\SendTo*"
- 找不到文件
- C:\Users\test\>添加~1.BAT
复制代码
6 个字符短名不够呀?
欢迎光临 批处理之家 (http://bathome.net./) |
Powered by Discuz! 7.2 |