Board logo

标题: [文件操作] 批处理怎样根据已知的文件短路径获取完整的长路径? [打印本页]

作者: 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

  1. @echo off&setlocal enabledelayedexpansion
  2. set "acc=C:\PROGRA~1\COMMON~1\MICROS~1\DAO\dao360.dll"
  3. for /f "delims=" %%a in ("%acc%") do set "access=%%~da\"&set "file=%%~nxa"s
  4. for %%i in ("%access%" "%file%") do set "acc=!acc:%%~i=!"
  5. set "acc=%acc:\= %"
  6. for %%a in (%acc%) do (
  7. set "ac=%%a"
  8. call :lp
  9. )
  10. echo %access%%file%
  11. pause>nul&exit/b
  12. :lp
  13. for /f "delims=" %%a in ('dir/b "%access%%ac:~,6%*"') do (
  14. set "access=!access!%%a\"
  15. )
复制代码

作者: 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
  1. :GetFullpath
  2. @echo off&setlocal enabledelayedexpansion
  3. echo 短名: %~s1
  4. REM set "acc=C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT"
  5. set "acc=%~s1"
  6. for /f "delims=" %%a in ("%acc%") do set "access=%%~da\"&set "file=%%~nxa"s
  7. for %%i in ("%access%" "%file%") do set "acc=!acc:%%~i=!"
  8. set "acc=%acc:\= %"
  9. for %%a in (%acc%) do (
  10. set "ac=%%a"
  11. call :lp
  12. )
  13. echo %access%%file%
  14. pause>nul&exit/b
  15. :lp
  16. echo "%access%%ac:~,6%*"
  17. for /f "delims=" %%a in ('dir/b "%access%%ac:~,6%*"') do (
  18. set "access=!access!%%a\"
  19. set ac&pause
  20. )
  21. @goto :EOF
复制代码
  1. D:\Users\test\Desktop\1_2 3>1.bat C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT
  2. 短名: C:\Users\test\AppData\Roaming\MICROS~1\Windows\SendTo\>添加~1.BAT
  3. "C:\Users*"
  4. ac=Users
  5. acc=Users test AppData Roaming MICROS~1 Windows SendTo
  6. access=C:\Users\
  7. 请按任意键继续. . .
  8. "C:\Users\test*"
  9. ac=test
  10. acc=Users test AppData Roaming MICROS~1 Windows SendTo
  11. access=C:\Users\test\
  12. 请按任意键继续. . .
  13. "C:\Users\test\AppDat*"
  14. 找不到文件
  15. "C:\Users\test\Roamin*"
  16. 找不到文件
  17. "C:\Users\test\MICROS*"
  18. 找不到文件
  19. "C:\Users\test\Window*"
  20. 找不到文件
  21. "C:\Users\test\SendTo*"
  22. 找不到文件
  23. C:\Users\test\>添加~1.BAT
复制代码
6 个字符短名不够呀?




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2