Bat 脚本的远程路径不适用于 "SET Profiles=\%PC%\C$\Users"

Bat script's remote path not working with "SET Profiles=\\%PC%\C$\Users"

@ECHO OFF
:ProfilDeleter
SET /P PC=Enter Remote Ip/HostName (for Exit press M): 
if [%PC%]==[] cls && @echo you should fill && goto ProfilDeleter
if %PC%==m goto :Menu
if %PC%==M goto :Menu

SET /p NotToBeDeleted=Your UserName: 
cls
::HERE NOT WORKING
SET Profiles=\%PC%\C$\Users

PushD "%Profiles%"
if /I Not "%CD%"=="%Profiles%" (ECHO. Unable to find %Profiles% exiting)&Pause&goto Menu
FOR /F "Delims=" %%I in ('Dir /AD /B ^|FindStr /I /V /C:"%NotToBeDeleted%" /C:"ADMINI~1" /C:"Public" /C:"Default" /C:"Administrator"') DO RD /Q /S "%%I"
PopD
pause
:Menu
Exit

我做了批处理以删除远程用户配置文件文件夹 但是我不能 运行 用于远程 PC?脚本哪里有问题?

阅读Pushd - UNC Network paths:

When a UNC path is specified, PUSHD will create a temporary drive map and will then use that new drive.
The temporary drive letters are allocated in reverse alphabetical order, so if Z: is free it will be used first.

PushD "%Profiles%"之后:

  • "%Profiles%" 会保持 "\<PC or IP entered>\C$\Users"
  • "%CD%" 会解析为 "Z:\Users"

修改脚本逻辑如下(调试ECHOs):

@ECHO OFF
SETLOCAL EnableExtensions
:ProfilDeleter
SET /P "PC=Enter Remote Ip/HostName (for Exit press M): " 
if "%PC%"=="" (
  rem cls
  echo you should fill
  goto :ProfilDeleter
)
if /I "%PC%"=="m" goto :Menu

SET /p "NotToBeDeleted=Your UserName: "
if "%NotToBeDeleted%"=="" SET "NotToBeDeleted=%UserName%"  
rem cls
SET "Profiles=\%PC%\C$\Users"

echo variables are set as follows:
echo       Profiles "%Profiles%"
echo             PC "%PC%"
echo NotToBeDeleted "%NotToBeDeleted%"
if exist "%profiles%" (
    PushD "%Profiles%"
    SETLOCAL EnableDelayedExpansion
      echo CD after pushd "!CD!"
    ENDLOCAL
    rem next `FOR … DIR … |FINDSTR` command requires further debugging IMHO
    FOR /F "Delims=" %%I in ('
      Dir /AD /B ^|FindStr /I /V /C:"%NotToBeDeleted%" /C:"All users" /C:"Public" /C:"Default" /C:"Administrator"
    ') DO (
            rem remove `ECHO` from next line no sooner than debugged
            ECHO RD /Q /S "%%I"
          )
    PopD
) else (
    ECHO. Unable to find "%Profiles%" exiting
    Pause
    goto Menu
) 
:Menu
ENDLOCAL
goto :eof

阅读 Debugging your batch files 篇文章。