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\*"
)
Related
Im making a small script but i reached a head scratch
I want the batch file to look for example.exe or Folder name in all drives then cd to it and create a txt file inside it? is that even possible :P?
cd /d D:
dir example.exe /s /p
lets say its found and the dir is D:/Example.exe
so i want the batch to do this,
if example.exe is found cd to it directory then
REM. >> "D:/logs.txt
is that possible? –
what do i put after "if exist" for the batch file to automatically switch to the found file directory
#echo on
cd /d D:
dir example.exe /s /p if exist (whether its in D:/Folder/folder or D:/Folder go to directory)
echo >test.txt
pause
Finally Solution by #Stephan best working answer
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
To catch the output of a command, use for.
dir /p does not make sense here (it's to pause if the output is longer than the screen). You want /b (bare format; filename only / drive/path/filename when used with /s).
%%~dpA gives drive:\path\ only.
break>filename to create an empty file (REM. does also work, but I prefer break)
#echo off
setlocal enabledelayedexpansion
for /f "delims=:" %%a in ('wmic logicaldisk where "size>0" get caption^|find ":"') do (
for /f "delims=" %%A in ('dir /s /b %%a:\example.exe 2^>nul') do (
ECHO break>"%%~dpAtest.txt"
)
)
this puts an empty test.txt to every folder in every available drive where there is an example.exe. If there already should be a test.txt, it gets overwritten by an empty file.
NOTE: this only echoes the break command to the screen (for security reason). If the output is like you want it, remove the ECHO.
Updated answer, since I got your question after this update:
In order to process all the files you found:
FOR /F "delims=#" %%f IN ('dir example.exe /s /b ') DO (
echo example.exe was found at path %%~pf
)
Now you can modify your ECHO statement:
ECHO some text > C:\logs.txt
If you want to append to the file instead of replacing, you have to use two brackets:
ECHO some text >> C:\logs.txt
You can enter a path after > . You don't need to CD before.
You can also use the variable for CD:
cd %%~pf
If you want some other part of %%f, you can do FOR /? on your command line or see the reference
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
I wan to write a batch script that can delete files from a specific folder for a bunch of users on that same computer. Not sure how this could work but I have this below of the idea of how it could work. I want to remote share in a pc, then navigate to all folders in the users folder then delete a text file in the specified path. How do I make this work?
#echo on
cd \\computername\C$
for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del \\computername\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
or
#echo on
psexec #pclist.txt -s cmd for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del \\computername\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
In both examples, you are looping over the C$ share but not using that in your del command.
Try this:
#echo on
for /f "usebackq" %%m in (`dir /b \\computername\C$\users`) do (
del "\\computername\C$\Users\%%m\AppData\Local\folder\folder\Log\*.txt"
)
PAUSE
Since most native cmd commands are not capable of handling UNC paths properly, I would go for the following (assuming that the given UNC path is accessible):
#echo on
pushd "\\Computername\C$\Users"
for /F "usebackq" %%M in (`dir /B "."`) do (
del ".\%%~M\AppData\Local\folder\folder\Log\*.txt"
)
popd
pause
pushd creates a temporary drive if a UNC path is given and makes its root the current directory. popd restores the previous state.
I am trying to create a batch file that will delete images with specific names. The images will have names such as
house-200x300.jpg
car-125x250.jpg
So what I need ideally is a regular expression to target files which end in -(Num1)x(Num2).jpg
Also, the images are in various folders and sub folders so I need to do this recursively from the parent folder.
Thanks
del /S *-???x???.jpg
Perhaps you may want to change del by dir /B command at first just to check that there is not any file that have not the specified file name format, but that will be selected by this wild-card.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.jpg" ^|findstr /i /e /r /c:"-[0-9][0-9]*x[0-9][0-9]*\.jpg"'
) DO (
ECHO DEL "%%a"
)
GOTO :EOF
This should do the job - targeting only those filenames ending with -numXnum.jpg
You'd need to set your own sourcedir
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.
Have you considered using powershell?
Regular expressions in batch files get very bad very fast as the syntax is limited.
My experience is limited, so there may very well be a better solution than this:
#echo off
FOR /F "delims=?" %%i IN ('dir /B /S ^| findstr /R "[^\.]*[0-9][0-9][0-9]x[0-9][0-9][0-9].jpg"') DO (
del /s "%%i" >nul 2>&1
)
I'm redirecting the output as I think that FOR starts to get confused about the output of del.
I need a batch script to copy files from a random subfolder of a specific directory into a destination directory.
For example, there will be a number of files in their own subdirectory, for example
.../source/a/file.txt
.../source/b/file.txt
So there a number of these files, and I would like to randomly select one of them and have it copied into the new directory
.../destination/file.txt
So the file.txt in the destination is just being overwritten with other files that have the same name but different content.
I'm new to batch scripting and I can't quite figure out how to select each file from a random subfolder. I'd also like it to repeat every 30 seconds until I terminate the script, but I think it should be easy enough to just make a second script that calls this .bat file every 30 seconds once I get it going.
Thanks!
This can do what you request. Just set your source directory, destination directory, and your file name filter.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
pushd "...\source\"
:: Enumerate Files. Method 1
set "xCount=0"
for /r %%A in (file.txt) do if exist "%%~A" set /a "xCount+=1"
echo %xCount%
:: Select a Random File.
set /a "xIndex=%Random% %% %xCount%"
echo %xIndex%
:: Find an Copy that File. Method 1
set "xTally=0"
for /r %%A in (file.txt) do if exist "%%~A" (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
:End
popd
endlocal
pause
Type xcopy /? to see all of its options.
Here are some alternate loop methodologies for the file enumeration.
:: Enumerate Files. Method 2
set "xCount=0"
for /f %%A in ('dir *.txt /a:-d /s ^| find "File(s)"') do set "xCount=%%~A"
echo %xCount%
:: Find an Copy that File. Method 2
set "xTally=0"
for /f "delims=" %%A in ('dir *.txt /a:-d /b /s') do (
if "!xTally!" EQU "%xIndex%" (
xcopy "%%~fA" "...\destination\file.txt" /Y
goto End
)
set /a "xTally+=1"
)
Enjoy :)
#echo off
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd \Fullpath\source
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
set /A i+=1
set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\file.txt" "\Fullpath\destination" /Y
rem And return to original directory
popd
The features of batch script is
It copies all files from source to destination folder with similar structure(even retains empty folders).
Can retain N number of days recent files in Archive folder(source) remaining files will be moved to backup folder(destination).
Can be scheduled to N number of days to N number of years.
Can be used in any Source to destination folder backup.
Source folder can add N number of folder any time and delete folder or files, but Destination folder always adds the folder and never deletes any folder or file.
#echo off
Set "sourcefolder=E:\Interfaces"
Set "destinationfolder=E:\BackupInterface"
If Exist %sourcefolder% (
For /F %%* In ('Dir /b /aD "%sourcefolder%" 2^>nul') do (If Not Exist "%destinationfolder%\%%*" ( RD /S /Q "%destinationfolder%\%%*")
xcopy /e /v /i /y /q "%sourcefolder%\%%*" "%destinationfolder%\%%*"
forfiles /p "%sourcefolder%\%%*" /s /d -30 /c "cmd /c del /Q /S #file" )
) Else (echo.Source folder could not be found)
:end of batch
echo.&echo.finished!