2015-11-10 50 views
-1

我需要找到一種使用批處理文件的方式來查找和重命名文件夾。有問題的文件夾位於不同的用戶配置文件中,所以我無法弄清楚我可以使用哪種通配符或變量。重命名具有不同名稱的文件夾或將其刪除?

在更詳細的方法,我有一個文件夾,意外部署到大量的機器。該文件夾已部署到每個用戶Start Menu,並且不包含任何內容。所以我的服務檯正在接到有關這個新的「測試」文件夾的電話,這個文件夾裏面沒有任何內容或空白的文本文件。雖然有些用戶被告知要刪除它,但其他用戶只是希望我們對此做點事情。因爲Start Menu位於每個用戶配置文件中,所以我需要一種在所有用戶配置文件Start Menu位置遞歸搜索的方法,然後查找並重命名或刪除該文件夾。

無論位置如何,問題中的所有文件夾都以相同的5個字符開始,但以()中的用戶名結尾。我嘗試使用*,但是我的命令是用引號括起來的,所以它完全採用了這種方式,而不是查找xxxxx *,其中*與配置文件不同。

這是可能的,還是我困惑,即使是最好的?

這是我正在努力工作的副本。前兩行確定它是XP還是Win7,並根據IF EXIST的結果告訴它從哪裏開始。然後它試圖找到名爲TEST的任何文件夾/目錄,位於每個用戶配置文件Start Menu中。

IF EXIST "C:\Documents and Settings\" PUSHD "C:\Documents and Settings\" 
IF EXIST "C:\Users\" PUSHD "C:\Users\" 
FOR /F "tokens=*" %%G in ('dir /a:d-s-h /b') do (
    IF EXIST "%%G\Start Menu\Programs\TEST" rmdir /q /s "%%G\Start Menu\Programs\TEST" 
    IF EXIST "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST" rmdir /q /s "%%G\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\TEST" 
) 
POPD 

問題是,完整的文件夾名稱是TEST (user name)。我試過放入Test *,但它不起作用,因爲我相信它是以引用的名稱詳細而不是使用*作爲通配符。因此,每個配置文件中的文件夾名稱都不相同,我需要代碼才能鍵入單詞Test,因爲這通常不適用於任何其他應用程序,並且不會涉及括號內的任何名稱。然後它需要使用RMDIR簡單地刪除該文件夾。

沒有VB腳本,這裏沒有批准。批處理文件將使用與文件夾相同的方法從服務器推送,但我們希望它檢查所有用戶配置文件,因爲許多受影響的計算機沒有一個特定用戶。推送會將文件夾放入Start Menu中,用於在推送時已登錄的任何用戶,並且推送重複自己30分鐘,直到我們注意到有人打電話給我們的幫助臺時發生了什麼,有關名爲TEST的新文件夾顯示在開始菜單上。該修復程序將在一夜之間運行,因此不會有用戶登錄。

+0

首先讓我知道所有用戶的一種方法是在 'AllUsersStartMenu' 創建文件夾,然後所有的用戶可以訪問。其他方式是爲每個用戶創建一個文件夾。所以你如何創建。那麼我們會走得更遠,一個建議使用VBS而不是蝙蝠。 –

+1

1.請通過編輯您的問題分享您迄今爲止所嘗試的內容! 2.提供一個示例文件夾名稱; 3.這個批處理腳本應該在哪裏執行 - 在每個客戶機上還是在一臺主機上? 4.帶'*'/'?'的路徑放入''「''不會*導致通配符從字面上被帶走... – aschipfl

+0

再一次:請分享您嘗試過的所有其他細節和**編輯您的題**!! – aschipfl

回答

0

下面是該任務的註釋批次代碼:

@echo off 
rem Make the parent directory of the profile directory of current user 
rem the current directory if this directory exists on current machine. 
if exist "%USERPROFILE%\..\" pushd "%USERPROFILE%\..\" & goto RemoveDirectories 

rem Make the standard user profiles directory on English Windows XP 
rem the current directory if this directory exists on current machine. 
if exist "C:\Documents and Settings" pushd "C:\Documents and Settings" & goto RemoveDirectories 

rem Make the standard user profiles directory on Windows Vista or later 
rem Windows in any language if this directory exists on current machine. 
if exist "C:\Users" pushd "C:\Users" & goto RemoveDirectories 

goto :EOF 

:RemoveDirectories 
rem Find in all subdirectories a directory starting with "Test", 
rem having next a space character, then an opening parenthesis, 
rem any other string (user name) and last a closing parenthesis. 

for /F "tokens=*" %%G in ('dir /A:D /B /S "Test (*)" 2^>nul') do rmdir /Q /S "%%~G" 2>nul 

rem Restore the previous current directory. 
popd 

相較於你的方法的重要區別是在命令DIR選項/SC:\Documents and Settings所有子目錄獲得所需目錄或C:\Users

如果在目錄樹中的任何地方有一個目錄Test (*),那麼這可能是至關重要的,而不是開始菜單。在刪除目錄之前,您可以評估要刪除的目錄是否包含路徑Start Menu,以確保刪除正確的目錄。但是,只有在所有機器都運行英文Windows的情況下才有效。

@echo off 
rem Make the parent directory of the profile directory of current user 
rem the current directory if this directory exists on current machine. 
if exist "%USERPROFILE%\..\" pushd "%USERPROFILE%\..\" & goto RemoveDirectories 

rem Make the standard user profiles directory on English Windows XP 
rem the current directory if this directory exists on current machine. 
if exist "C:\Documents and Settings" pushd "C:\Documents and Settings" & goto RemoveDirectories 

rem Make the standard user profiles directory on Windows Vista or later 
rem Windows in any language if this directory exists on current machine. 
if exist "C:\Users" pushd "C:\Users" & goto RemoveDirectories 

goto :EOF 

:RemoveDirectories 
rem Find in all subdirectories a directory starting with "Test", 
rem having next a space character, then an opening parenthesis, 
rem any other string (user name) and last a closing parenthesis. 
rem It is tested for safety if the directory to delete is really 
rem in the programs directory of the start menu directory. 

setlocal EnableDelayedExpansion 
for /F "tokens=*" %%G in ('dir /A:D /B /S "Test (*)" 2^>nul') do (
    set "TestDirectory=%%~G" 
    if not "!TestDirectory:\Start Menu\Programs\Test (=!" == "!TestDirectory!" rmdir /Q /S "%%~G" 2>nul 
) 
endlocal 

rem Restore the previous current directory. 
popd 

兩個批處理文件中使用的標準錯誤重定向到設備NUL抑制由於沒有在所有的發現(2^>nul的命令DIR)目錄Test (*)錯誤消息或目錄刪除無法刪除(2>nul命令rmdir),例如當它是任何其他進程的當前目錄運行時。

爲了解所使用的命令及其工作方式,請打開命令提示符窗口,在其中執行以下命令,並仔細閱讀爲每個命令顯示的所有幫助頁面。

  • dir /?
  • echo /?
  • endlocal /?
  • for /?
  • goto /?
  • if /?
  • popd /?
  • pushd /?
  • rem /?
  • rmdir /?
  • set /?
  • setlocal /?
+1

謝謝你清理我的問題。並且還爲您的代碼工作。星期一,我今天嘗試了它。 – ricka182

相關問題