用于从网络上的用户删除文件的批处理文件

Batch file to delete files from users on network

我想要批处理文件询问序列号和用户名,并从用户配置文件中删除两个特定文件夹。我做了这个,但它似乎想从我所在的文件夹 运行 中删除 *.*

@echo off

set /p serial="Enter Serial: "

set /p username="Enter Username: "

del *.* \%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations
del *.* 
\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations
pause

c$为管理员隐藏资源,不登录无法访问

如果您想连接到远程计算机上的网络共享,请使用: 净使用 * \servername\sharename

del命令的语法不允许将文件掩码与要删除的文件所在的路径分开,因此需要使用

del /q "\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*"
del /q "\%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*"

添加的引号将防止包含空格的路径出现问题,/q 开关将避免删除指定文件夹中所有文件的安全确认。

首先,删除 @echo 直到您的代码正常工作,这样您就可以看到发生了什么,然后添加一些 echo 以查看存储在您正在使用的变量中的内容。之后尝试使用 DIR 而不是 DEL,以便它列出符合您条件的文件。

REM    @echo off

set /p serial="Enter Serial: "
Echo %serial%
pause

set /p username="Enter Username: "
echo %username%
pause
dir \%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\AutomaticDestinations\*.*
dir \%serial%\C$\users\%username%\appdata\roaming\Microsoft\Windows\Recent\CustomDestinations\*.*
pause

尝试下一种方法,基本检查所有用户的输入:

@ECHO OFF
SETLOCAL EnableExtensions

:inputServer
set "serial="
set /p "serial=Enter Serial: "
if not defined serial goto :endlocal
pushd "\%serial%\C$\"
if errorlevel 1 (
  echo wrong server name "\%serial%"
  goto :inputServer
)

:inputUser
set "_username="
set /p "_username=Enter Username: "
if not defined _username goto :endstack
cd "\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
if errorlevel 1 (
  echo wrong user name "%_username%" or path
  echo "\%serial%\C$\users\%_username%\appdata\roaming\Microsoft\Windows\Recent"
  goto :inputUser
)

dir AutomaticDestinations\*.*
dir CustomDestinations\*.*

:endstack
popd

:endlocal
pause

在调试完成后立即将dir替换为del /Q

Resource for PUSHD - POPD pair:

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.