I want to delete couple of folders using batch file.
But problem is that these folders are present on some other machine.
But I can take the remote access to this machine on my machine.
Please help me with this issue. I don't have any idea about this.
A less comprehensive, but more simple answer...
del /F \\myserver\w_root$\users\selah\selah.txt
del /F \\myserver\w_root$\users\selah\status.txt
As long as you can mount the remote folder with a user who has delete authority, you can simply do something like this:
rem Set up the remote path - assuming it is sharable
set MY_DIR=\\10.1.1.1\some\path
rem Mount the remote path
net use %MY_DIR% %MY_PASSWORD% /USER:%MY_USER%
rem Delete a file
if exist "%MY_DIR%\MyFile" del /F "%MY_DIR%\MyFile" >nul
rem Unmount the remote path
net use %MY_DIR% /delete >nul
If you want to remove folders, you'll just need to make sure the mount point is at least one level above where you'll be deleting.
Related
I need to delete a folder from C:\Users for any user profile within the Users directory.
I’m needing help putting together a windows batch script to accomplish this.
The folder stores a user config file for an application that needs to be deleted.
Thanks in advance!
Here's an idea:
#Echo Off
(Set UPD=C:\Users)
(Set UDS=SomeDir\TheDir)
For /F "Delims=" %%A In ('Dir/B/AD-R-H "%UPD%"') Do (
If Exist "%UPD%\%%A\%UDS%\" RD/S/Q "%UPD%\%%A\%UDS%")
Just put the path and folder spec within the parentheses on line three
I want to copy specific file from one machine to 5 other machines, so I have server list file contains the ip addresses of each machine like:
10.10.1.3
10.10.1.4
10.10.1.5
10.10.1.6
10.10.1.7
in my batch file:
SET File=C:\Files\servers.txt
SET User=user
SET Password=pass
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
FOR /F %%A IN (%File%) DO (
START /WAIT NET USE b: \\%%A\C$\Temp /user:%User% %Password%
COPY C:\Logs\L1.log b:\L1.log /Y
IF EXIST b:\ (
NET USE b: /DELETE /Y
)
)
the problem is in the first server I get error message The system cannot find the drive specified but for the others servers everything works great.
I think it's something with the NET USE of course maybe the map network is deleted before finish copy?
Is there any way in a batch file to loop some servers and for each one of the servers open map network copy files wait till copy is completed and move on to next server?
EDIT:
I have an update for this problem:
the source machine and the target machine are both in different domains.
I have a user define as admin in both of the machines.
The machines knows each other (I can open the target folder in the source machine like \server\C$\temp and I can paste anything I want there)
I tried to copy files without using net use and just copy from C:\file.log \server\c$\temp\file.log for each server (I have 5) and for 3 servers it worked and the other two I had an error: Logon failure: unknown user name or bad password
FOR /F %%A IN (%File%) DO (
COPY C:\temp\file.log \\server\c$\temp\file.log /Y
)
What can be the problem?
Please help?
Thank you in advanced.
The best way would be if the user/password is the same on every machine (credientals shared throughout the domain)
In that case, no need for NET USE, just copy to UNC path directly
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\C$\Temp\L1.log
I understood you have a problem on 2 machines out of 5. You cannot access C$ directly if you are not administrator of the machines.
Check:
domain (echo %USERDOMAIN% on the remote machine): All machines must be on the same domain for it to work
whether you are administrator or not on the remote machine: you cannot mount drives with the C$ trick if you are not administrator.
There is an alternate solution for you if this doesn't work out:
Share C:\TEMP (read/write) as the same name ex: sharedtemp on the 5 machines (or ask an admin to do that)
Then adapt the script like this:
FOR /F %%A IN (C:\Files\servers.txt) DO COPY /Y C:\Logs\L1.log \\%%A\sharedtemp\L1.log
Even if you are not an administrator, you'll be able to access the share that way.
When using START command, you need to either specify new window name, or leave it blank in double quotes, if you want it to run in the same open CMD window. However, you don't need to use START in this particular case. Try adding timeout for the 1st or all server connections in the list:
IF EXIST b:\null NET USE b: /DELETE
:: more code here
NET USE b: \\%%A\C$\Temp /user:%User% %Password%
if "%%A"=="10.10.1.3" timeout 5 >nul
This batch script is supposed to map to PC and rename the files in that folder using the date & time stamp and copy them to another location on a different PC. It should then delete all the files in that source folder except for a file that called "LBBS.log". It all works fine except for the delete part. It isn't deleting anything in the folder and it is actually deleting the batch file itself. When I run it, it copies over fine but then it deletes itself. Can someone please tell me what I need to change for this to work. What am I missing? Its on a windows 7 environment. Thanks in advance.
net use x: \\MTLLBBS023\C$
set "stamp=%date:~4,2%%date:~7,2%%date:~10,4%%time:~0,2%%time:~3,2%%time:~6,2%"
set "source=MTLLBBS023"
xcopy /S /E /I x:\logs E:\Data\Logs\MTLLBBS023\%source%-%stamp%.*
cd x:\Logs
for %%i in (*) do if not %%i == LBBS.log del %%i
net use x: /delete
The problem is that your script and target directories are located on separate drives.
When you cd to another directory, the command will fail if you try to move to another drive without using the /d option.
Instead of cd x:\logs, you should say cd /d x:\logs - this will change the drive and directory.
Alternatively, instead of the net use and net use delete commands, you can simply pushd \\MTLLBBS023\C$ to go to the network drive (this also automatically creates a temporary network drive) and then popd at the end of the script to leave the directory and remove the mapped drive. This way, you don't need to cd at all.
I currently use this code to copy shortcuts from a folder on a server to C:\Users\Desktop:
if not exist "%1" md "%1"
copy /y "%~dp0PlaceShortcutsHere\*.*" "%1"
This copies any shortcuts I place in the folder to the desktop.
I now need a way to remove these, baring in mind that the shortcuts in the source folder can and will change over time.
Is there a way to compare the shortcuts in the desktop and on the server and only delete the ones that are present in both folders, and only from the desktop of the computer?
These shortcuts are not all of the shortcuts on the desktop of the machines, there are others as well, hence wanting to only delete the ones present in both locations. I will also need this to be adaptive as the shortcuts present on the server will be added to or removed as needed.
This is to be deployed out through SCCM 2007/12 but I want to test it locally first.
And yes, using a GP would be easier but the GP we use has stopped working so I need a backup way of deploying shortcuts.
Took some time but I've found an answer myself. Posting this if it is useful to anyone in the future.
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Breaking it down:
dir "%~dp0PlaceShortcutsHere\*" /a /b /-p /o:gen>>"%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
The above created a text file with the names of the shortcuts in it for referencing by the next line.
Pushd \\<Server>\<Share>
for /F "delims=" %%G in (ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt) DO Del "C:\Users\Public\Desktop\%%G"
The above maps the server/share that the files are in temporarily, then uses the "For /F" loop to get the file names and then delete them.
del "%~dp0ShortcutList_%date:~-4,4%%date:~-7,2%%date:~-10,2%.txt"
Finally, this line deleted the text file that was created.
This is useful for when the contents of the folder is always changing, it will create an up to date list of the files and then delete the list afterwards, preventing confusion.
%date:~-4,4%%date:~-7,2%%date:~-10,2%
Lastly, this little line will input the current date into the filename of the text documet, so should the final delete line not work you can see the date on it. At a guess I would also think that putting it in paths will make the script only choose the files from the current day, not any others, but I have not tested this.
Please correct me if I'm wrong on anything.
I've been working to clean up a messy Active Directory as well as a network file system in the same state and I understand the concept of mapping users network drives and currently use a combination of batch and vbs files to do so. However, I need to start fresh and was wondering if there was any way to detect and delete the users shortcuts on their desktop associated with the previous network drives. (Yes - I understand how to delete all of the network drives, but: How do I detect and delete the shortcuts on the desktop associated with them?)
I've already written and custom tailored my own scripts to map drives and place shortcuts. I just need to get rid of any old shortcuts. I can't afford to delete all of the .ink files on the desktop, either. Only any associated with preexisting network drives.
I'm working in an XP / Server 2003 client/server environment.
Another question: If a script runs every time a user logs on through the domain and adds the same network shares over and over again without first deleting them, (even though it would be the same script every time) would it / does it - do any harm? <- I didn't research this one a whole lot yet because i've been crawling through Google and took a peak see through Stack to try and find a solution for the first question/issue.
Any help / advice would be greatly appreciated. Thanks!
Let's say there was before in logon batch
%SystemRoot%\System32\net.exe use Z: \\OldServer\OldShare
and the users created shortcuts to files / directories on this share on their desktop or in a subdirectory of the desktop.
A shortcut file contains always both – the path to the file with drive letter as well as the UNC path with server and share names.
A simple batch file to find in the desktop directory and all its subdirectories *.lnk files containing \\OldServer\OldShare and delete all found shortcut files is:
#echo off
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "\\\\OldServer\\OldShare" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo Deleting shortcut file "%%I"
del "%%I"
)
For details about /I /M /S run in a command prompt window findstr /?
As it can be seen each backslash in the find string must be escaped with one more backslash.
It is also possible to search for "Z:\\" instead of "\\\\OldServer\\OldShare"
But be careful with deletion of a *.lnk file just based on drive letter because users could have mapped a different share to this drive letter. Those users would not be happy if their shortcuts are deleted, too.
The batch file could ask the user for confirmation before deleting every found shortcut containing the drive letter of not anymore existing drive:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Searching for shortcuts to drive Z: ...
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "Z:\\" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
echo/
echo Shortcut "%%~nI" might be not valid anymore.
echo/
setlocal EnableDelayedExpansion
set Confirm=
set /P "Confirm=Delete the shortcut (y/n)? "
if /I "!Confirm!" == "y" (endlocal & del /F "%%I") else endlocal
)
endlocal
It is no problem if a network drive mapping using a command like
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
is done in logon batch file on every logon. An error message is output by net use if drive letter Z: is used already, for example if the network drive mapping was done persistent and the user started the computer first without a network connection, then plugged-in the network cable and next after a few seconds entered user name and password to logon to Windows and on domain server of the company.
It is possible to use
%SystemRoot%\System32\net.exe use Z: /delete 2>nul
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
to first delete an already existing network drive mapping before mapping the share to drive letter Z:. I do not recommend to use wildcard * instead of Z: as that would delete also all network drive mappings created by the user.
For computers not only used in the company network, it is often better to make the drive mapping not persistent by using
%SystemRoot%\System32\net.exe use /persistent:no
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
%SystemRoot%\System32\net.exe use /persistent:yes
Windows does not save in this case in Windows registry under key HKEY_CURRENT_USER\Network that \\MyServer\MyShare should be mapped to Z: and therefore the network drive mapping exists only for current user session. The network drive mapping is removed automatically once Windows is restarted or the user logs off.