![](/img/trans.png)
[英]Windows Command: how do I push current directory using chdir for popping later?
[英]How can I verify directory in batch file after using chdir
好。 我有此批處理文件,該文件旨在從可能存在或可能不存在的文件夾中刪除每個文件和文件夾。 基本結構如下:
@echo off
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo %CD%
echo %0
echo %~dp0
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
我希望能夠確保批處理腳本實際上已將路徑更改為MyDirectory並刪除了正確的文件中的回顯原因(我們之前曾發生過一次類似事件,因為我在腳本中沒有這樣做請勿更改路徑,並從我們的docs文件夾之一刪除所有內容)。
回顯將返回批處理文件本身的名稱,或者返回運行批處理文件的路徑,而不是“ c:\\ MyDirectory \\”。 因此,就我而言,它來自c:\\ Testing \\(我創建的一個虛擬目錄,以避免再次出現麻煩)。
有沒有辦法從批處理腳本中獲取當前處於活動狀態的目錄,以便我可以驗證要清空的目錄?
嘗試這個:
@echo off
setlocal EnableDelayedExpansion
If EXIST c:\MyDirectory\(
chdir c:\MyDirectory\
echo !CD!
pause
rem ... This removes everything from the folder....
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
)
由於您使用的是if語句,因此應使用setlocal EnableDelayedExpansion
並使用!
而不是%
表示變量。
要核對目錄的內容,請使用以下shell腳本(批處理文件):
@echo off
setlocal enableextensions
if {%1}=={} goto :HELP
if {%1}=={/?} goto :HELP
goto :START
:HELP
echo Usage: %~n0 directory-name
echo.
echo Empties the contents of the specified directory,
echo WITHOUT CONFIRMATION. USE EXTREME CAUTION!
goto :DONE
:START
pushd %1 || goto :DONE
rd /q /s . 2> NUL
popd
:DONE
endlocal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.