I am trying to set up a batch program that will go to the different computers on my network (from .txt file) and then delete files from the users on the that PC, and then empty the recycle bin. I've got the second part working so I can delete files from multiple users on a PC, but I can't get it to look at other PC's. I was hoping someone might point out what I'm missing here. Here is what I have so far:
#ECHO off
Setlocal EnableDelayedExpansion
FOR /F "delims=" %%i IN (test.txt) DO (
for /f %%a in ('dir /B /AD C:\Users') do (
REM for /f "tokens=*" %%a in (userlist.txt) do (
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Lotus\Notes\Data\workspace\logs"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Google\Chrome\User Data\Default\*"
if exist "C:\Users\%%a\" del /S /Q "C:\Users\%%a\AppData\Local\Microsoft\Windows\Temporary Internet Files"
)
RD %systemdrive%\$Recycle.Bin /S /Q
)
pause
Anybody got any pointers?
You need to use UNC paths.
\\server\sharename\folder\file.ext
You can get a list of computers with
for /f "skip=3" %A in ('net view ^| findstr /C:"\\"') do Echo %A
All computers have a share for admins only called C$ that is the C drive (and D$ etc). $ makes it hidden. So
for the local computer via UNC
del /S /Q "\\127.0.0.1\C$\User\username\AppData\Local\Lotus\Notes\Data\workspace\logs"
Also there is no point to If Exist, just delete it - it will work or not. If you care if it worked or not you test the return value If errorlevel 1 echo error. You are causing extra disk access and network traffic. In programming we do and test not test and do.
You can also run a batch on the other computer.
wmic /node:"#Computerlist.txt" process call create "c:\\batchfile.bat"
Note \\ in paths. C:\\batcfile.bat is C: on the remote computer. This allows you to only use one for loop in your batchfile. You copy the batch file with copy. Net View can generate the computer list although you have to remove \\
for /f "tokens=1 delims=\" %A in ('net view ^| findstr /C:"\\"') do Echo %A
Related
The point is that I have to create a script for my school that deletes all the temporary ones from Mozilla Firefox, with a batch script in Windows 10.
The most complex thing about this is that the mozilla path has a folder that has a different "Prefix" on each computer, in my case it is
wvbrz602.default-release.
(Ruta completa sería: C:\Users\dmarmol\AppData\Local\Mozilla\Firefox\Profiles\wvbrz602.default-release)
What I have done has been a loop that iterates for each user and stores it in the variable in which it iterates "%%a" In this way I obtain each user, once each user is obtained, I do a dir obtaining the prefix of the containing folder of the Firefox profile of each user in a .txt document (Since the prefix is different for each user)
After that prefix obtained in the .txt of each user I add it to a variable (The problem is that, apparently they don't like it)
And finally I make a del of the temporary of each user.
This is my code:
#ECHO on
SETLOCAL EnableDelayedExpansion
rem TASKKILL /F /IM mozilla.exe /T
for /f %%a in ('dir c:\Users /b') do>%%a.txt (
dir /b "C:\Users\%%a\AppData\Local\Mozilla\Firefox\Profiles\*.default-release"
set /p contenedorFor=<"%%a".txt
del C:\Users\%%a\AppData\Local\Mozilla\Firefox\Profiles\%contenedorFor%\cache2
)
exit /b 0
I show a snapshot of the exit the code.
You can simply test if the file exists. Here, the result will be echo'ed only where you need to replace echo with rmdir or del accordingly:
#echo off
for /d %%i in ("%systemdrive%\Users\*") do (
for /f "delims=" %%a in ('dir /b /a:d "%%~i\AppData\Local\Mozilla\Firefox\Profiles\" 2^>nul ^|findstr "default-release"') do del /Q "%%~i\AppData\Local\Mozilla\Firefox\Profiles\%%~nxa\Cache2\*"
)
I'm trying to delete all directories based off a naming convention using a batch file in windows. I don't want to delete all directories, only one's that match a pattern.
I'm doing this on Windows Server, so I don't know if that matters. I can get this to work on my personal desktop, but when I attempt on the Windows Server computer it doesn't work.
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
For /D /r %%i in ("*mail*") DO rd /Q /S %%i
pause
echo Done
When this is run, it iterates through all the directories and lists them. But then after it lists them all it says the following:
"The system cannot find the file specified."
"The system cannot find the path specified."
I find this odd since it literally lists every path and then says it can't find it. I'm sure I'm missing something small. Any help is appreciated.
Solved:
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do #rd /S /Q "%%I"
pause
echo Done
Echo Deleting Folders
cd C:\Users\srvFIPITSTOPPAPP1\AppData\Roaming\Enfocus\Switch Server\temp
pause
for /F "delims=" %%I in ('dir /S /B /A:D "*mail*" ^| sort /R') do #rd /S /Q "%%I"
pause
echo Done
I need to remove the same three files, lets call them:
1 (internet shortcut)
1_2.txt
1_2.html
These files have been creaetd 1,000s of times in different folders on the same set of networks drives.
I need a bat script that will delete them, permanently (without using Recycle Bin) and do it silently.
Any takes?
Thanks
Pappaslim.
well... you could try this.... but use dir not del the first time to be sure you are deleting the right stuff...
for %%j in (C E F) do (for %%l in (1.lnk 1_2.txt 1_2.html) do (for /f %%i in ('dir /s /b %%j:\%%l') do (del /q %%i)))
where C E and F are your drives, and 1.lnk 1_2.txt and 1_2.html are your files.
pushd "start_directory_of_subtree"
del /s "1 (internet shortcut)" "1_2.txt" "1_2.html" >nul
popd
Use with extreme caution.
#echo off
FOR /R [C:\] %%G IN (1.lnk) DO DEL /Q %%G
FOR /R [C:\] %%G IN (1_2.txt) DO DEL /Q %%G
FOR /R [C:\] %%G IN (1_2.html) DO DEL /Q %%G
PAUSE
You should be cautious in using this.
NOTES:
"C:\" can be replaced with the drive letter of your network drive.
"1.lnk" can be replaced with whatever file extension is associated with your shortcut. I assumed .lnk but since you didn't list the file extension in your question I will let you fill it in.
I'm trying to write a .bat file to delete all files from a directory that look like "r" concatenated with a number of indeterminate length with a .sas7bdat ending.
e.g.: r2343.sas7bdat, r2309483.sas7bdat, etc.
The problem is, that I also have a file called "ranker_interface.sas7bdat", so I can't just do:
del "C:\temp\r*.sas7bdat"
I've tried Googling this up and down, but I haven't been able to figure it out. Is there any way of excluding a particular value from a wildcard?
for /L %%a in (0,1,9) do ECHO del r%%a*.sas7bdat
would possibly be easiest.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO DEL to DEL to actually delete the files.
If you are running this directly from the prompt rather than as a line in a batch file, change all %%a to %a.
This is the old school MSDOS way of getting around this:
attrib +h "C:\temp\ranker*.sas7bdat"
del "C:\temp\r*.sas7bdat"
attrib -h "C:\temp\ranker*.sas7bdat"
The attrib command hides the files you don't want deleted from the DEL command,
and then unhides them again.
To list down all files from folder that are like 'r*.sas7bdat', exclude the one from the list called ranker_interface.sas7bdat and delete the rest you can use this command from within batch file:
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat ^| find /v "ranker_interface.sas7bdat"') do del /f /q %%a
It is written the way to be executed from the folder directly.
This will delete all 'r*.sas7bdat' but the one specified by find /v - exclusion, that can eventually be followed by another find /v exclusion, so something like:
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat ^| find /v "ranker_interface.sas7bdat" ^| find /v "rxxxxx.sas7bdat"') do del /f /q %%a
Not the best way if you want to exclude more than one file. If you want to exclude more than one file, then you can put your excluded filenames into singel file and isntead of directly running del command further for followed by if statements can be done and compare the file if can be found in exclusion list file, that contain file name on each row. Batch can look like this then:
#echo off
for /f "tokens=*" %%a in ('dir /b r*.sas7bdat') do (
for /f "tokens=*" %%x in ('type exclusion.txt ^| find /c "%%a"') do (
if "%%x" EQU "0" (del /f /q %%a)
)
)
This will delete all files in your directory that are like 'r*.sas7bdat', except those listed down in file 'exclusion.txt'
Hope this helps
I need to replace every icon (AutoCAD 2010.LNK) found on the computer with another .LNK using batch.
The icon\ shortcut as we well know can be found anywhere and as many times as the user likes.
How can I achieve this?
first, read HELP FOR
and then try this in a command line
FOR /F "tokens=*" %a in ('dir /B /S "AUTOCAD 2010.LNK"') do ECHO COPY new.lnk %a
experiment with from various locations and test carefully
then create a bat file with the following contents. Note the change of %a into %%a and the removal of the 'echo'
#echo off
PUSHD C:\
FOR /F "tokens=*" %%a in ('dir /B /S "AUTOCAD 2010.LNK"') do COPY new.lnk %%a
POPD
#ECHO OFF
SET "linklist=%USERPROFILE%\linklist.txt"
SET "replacement=D:\path\to\replacement.lnk"
ECHO Searching...
DIR /B /S "C:\AutoCAD 2010.LNK" >%linklist%
DIR /B /S "D:\AutoCAD 2010.LNK" >>%linklist%
:: add similar rows for every drive letter you want to be included
ECHO Replacing...
FOR /F "tokens=*" %%f IN (%linklist%) DO COPY %replacement% %%f
ECHO Finished.
A couple of notes:
Your replacement shortcut file must be named differently (like AutoCAD 2010.LNK.new, for example).
In Windows Vista/7 you will probably be prohibited from overwriting files in certain folders unless you are running the script with elevated rights.