简体   繁体   中英

Batch file to delete files from users on network

I want the batch file to ask for Serial number and username and delete two specific folders from users profile. I made this but it seems to want to delete *.* from folder I am running it from.

@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$ is administrator hiding ressources, you can't access to it without login

If you want to connect to network share on the remote computer, use this: net use * \\servername\\sharename

The syntax of the del command does not allow to separate the file mask from the path where the files to delete are stored, so you need to use

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

The added quotes will prevent problems with paths that include spaces and the /q switch will avoid the security confirmation to delete the all the files in the indicated folder.

First, remove the @echo off untill your code is working so you can see what's happening, then add some echo to see what is stored in the variable that you're using. After that try with DIR instead of DEL so it list the file that match your criteria.

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

Try next approach with basic checking all user's input:

@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

Replace dir with del /Q no sooner than debugged.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM