我试图在我的代码库中搜索所有 jscript 目录,然后使用以下批处理脚本获取这些目录中的相关文件列表...
@echo off
setlocal enableextensions enabledelayedexpansion
FOR /F "tokens=*" %%G IN ('DIR /B /AD /S jscripts') DO (
CD %%G
CD dev
SET "currentDir=!cd!"
IF NOT "!currentDir:~-1!"=="\" SET "currentDir=!currentDir!\"
FOR /r %%F IN (*.js) DO (
SET "relativePath=%%F"
SET "relativePath=!relativePath:%currentDir%=!"
ECHO !relativePath!
)
)
这一切都按预期工作,直到达到......
SET "relativePath=!relativePath:%currentDir%=!"
我可以弄清楚我需要用什么格式写这个... ...
c:\dir\jscript\dev\file.js
...成
file.js
请帮忙!
其他信息
目录设置如下
dir
jscripts
dev
file.js
file2.js
live
file.js
file2.js
dir2
jscripts
dev
file.js
file2.js
live
file.js
file2.js
我想找到所有 jscripts 目录,将CD放入其中,获取所有JS文件的列表相对于dev 目录
答案 0 :(得分:3)
要从包含完整路径的变量中提取文件名和扩展名,请使用{/ 1}},如
%~nx0
从this answer to a similar question
引用以下内容set G=c:\dir\jscript\file.js echo %~nxG
这是来自" for /?"的复制粘贴命令提示符。希望它 帮助
相关
Top 10 DOS Batch tips (Yes, DOS Batch...)显示 batchparams.bat(链接到源代码作为要点):
%~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file The modifiers can be combined to get compound results: %~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only
答案 1 :(得分:1)
@echo off
setlocal enableextensions enabledelayedexpansion
rem Where to start
pushd "c:\wherever\global2_root\"
rem Search .....\dev directories
for /f "tokens=*" %%d in ('dir /ad /s /b ^| findstr /e "\\dev"') do (
rem change to that directory
pushd "%%~fd"
echo Now in !cd!
rem process files inside directory
for %%f in (*.js) do (
echo %%f
)
rem return to previous directory
popd
)
rem return to initial directory
popd
rem cleanup
endlocal