我想运行一个 .bat 脚本,该脚本创建一个文件并使用与其所在文件夹相同的名称保存它。我希望它在地图中启动并循环遍历子地图。我从下面的代码开始,并在其中一个子图中运行它。那行得通,我得到一个带有子图名称的文件。
FOR /D %%I IN ("%CD%") DO SET _LAST_SEGMENT_=%%~nxI
ECHO Last segment = "%_LAST_SEGMENT_%"
echo.>%_LAST_SEGMENT_%.mp4
pause
所以现在我只需要把它放在一个 for 循环中。所以我做了下面的代码,这是行不通的。似乎%CD%
是指我的父地图(子文件夹),.bat
文件所在的位置,而不是我的子地图(video1、video2 等)。
Set Base=C:\Video\subfolders
FOR /R "%Base%" %%F IN (.) DO (
PushD "%%F"
FOR /D %%I IN ("%CD%") DO SET _LAST_SEGMENT_=%%~nxI
echo %CD%
ECHO Last segment = "%_LAST_SEGMENT_%"
echo.>%_LAST_SEGMENT_%.mp4
PopD
)
Pause
输出:
C:\Video\subfolders>Set Base=C:\Video\subfolders
C:\Video\subfolders>FOR /R "C:\Video\subfolders" %F IN (.) DO (
PushD "%F"
FOR / %I IN ("C:\Video\subfolders") DO SET _LAST_SEGMENT_=%~nxI
echo C:\Video\subfolders
ECHO Last segment = ""
echo. 1>.mp4
PopD
)
C:\Video\subfolders>(
PushD "C:\Video\subfolders\."
FOR / %I IN ("C:\Video\subfolders") DO SET _LAST_SEGMENT_=%~nxI
echo C:\Video\subfolders
ECHO Last segment = ""
echo. 1>.mp4
PopD
)
C:\Video\subfolders>SET _LAST_SEGMENT_=subfolders
C:\Video\subfolders
Last segment = ""
C:\Video\subfolders>(
PushD "C:\Video\subfolders\video1\."
FOR / %I IN ("C:\Video\subfolders") DO SET _LAST_SEGMENT_=%~nxI
echo C:\Video\subfolders
ECHO Last segment = ""
echo. 1>.mp4
PopD
)
C:\Video\subfolders\video1>SET _LAST_SEGMENT_=subfolders
C:\Video\subfolders
Last segment = ""
C:\Video\subfolders>(
PushD "C:\Video\subfolders\video2\."
FOR / %I IN ("C:\Video\subfolders") DO SET _LAST_SEGMENT_=%~nxI
echo C:\Video\subfolders
ECHO Last segment = ""
echo. 1>.mp4
PopD
)
C:\Video\subfolders\video2>SET _LAST_SEGMENT_=subfolders
C:\Video\subfolders
Last segment = ""
C:\Video\subfolders>(
PushD "C:\Video\subfolders\video3\."
FOR / %I IN ("C:\Video\subfolders") DO SET _LAST_SEGMENT_=%~nxI
echo C:\Video\subfolders
ECHO Last segment = ""
echo. 1>.mp4
PopD
)
C:\Video\subfolders\video3>SET _LAST_SEGMENT_=subfolders
C:\Video\subfolders
Last segment = ""
C:\Video\subfolders>Pause
Press any key to continue . . .
我看到下面解释使用的链接,setlocal enabledelayedexpansion
但我无法让它工作。
https://superuser.com/questions/503488/why-doesnt-cd-work-after-using-pushd-to-a-unc-path
任何意见,将不胜感激