I found this post in regards to wildcards in directories. However, my problem is that I have multiple varying directory names between my static directories. For example:
O:\123456 Client Name\Spring\Shoot 1 12345\01 MHP 01\PlCache\GreenScreen\
O:\121212 Someone Else\Spring\Shoot 1 21212\01 MHP 02\PlCache\GreenScreen\
The above link only allows for one wildcard directory instead of muliples.
Within these GreenScreen folders, I have .png files I wish to delete. How would I write a .bat file that deletes *.png within O:\ *\GreenScreen\ ?
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:"
FOR /f "tokens=1*delims=" %%a IN (
'dir /s /b /a-d "%sourcedir%\*.png" '
) DO (
SET "targetpath=%%~pa"
IF "!targetpath:~-13!"=="\GreenScreen\" ECHO DEL "%%a"
)
GOTO :EOF
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.
I've changed to starting directory to U: to suit my system.
Here's a simpler option - it also echos the del commands to the screen until you remove the echo keyword.
#echo off
for /d /r "o:\" %%a in (GreenScreen*) do if /i "%%~nxa"=="GreenScreen" echo del "%%a\*.png"
Related
I want to write a batch file that creates a folder (if it does not exist) and copies a certain file into that folder. So far so good.
The problem is that one folder in the path varies slightly from time to time, so a wildcard becomes necessary.
The following code works just fine but obviously misses to create the folder (Reports). So if the folder is not there, it simply does nothing.
for /r "c:\Users\%USERNAME%\AppData\Local\Packages" &&G in ("LocalState\acn\Reports") do #if exist %%G xcopy /s /i /y c:\temp\Reporting "%%G"
The full path is:
c:\Users\FSchneider\AppData\Local\Packages\“WILDCARD"\LocalState\acn\Reports\
Any idea?
Add /d switch in for to indicate you're looking for a directory, not a file
Add * and omit quotes in the wildcard to indicate it's actually a wildcard
No need for if exist now
for /d /r "%LocalAppData%\Packages" %%G in (LocalState\acn.*) do xcopy /s /i /y c:\temp\Reporting "%%G\Reports"
Next script could help.
#ECHO OFF
SETLOCAL enableextensions
set "_fldrtop=%USERPROFILE%\AppData\Local\Packages"
set "_fldrsub=LocalState\acn"
if not "%~1"=="" set "_fldrsub=%~1" :: my testing data, remove this line
set "_fldrlow=Reports"
if not "%~2"=="" set "_fldrlow=%~2" :: my testing data, remove this line
for /F "delims=" %%G in ('dir /B /AD "%_fldrtop%"') do (
if exist "%_fldrtop%\%%G\%_fldrsub%\" (
if exist "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\" (
echo echo "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
) else (
echo md "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
rem echo xcopy /s /i /y c:\temp\Reporting "%_fldrtop%\%%G\%_fldrsub%\%_fldrlow%\"
)
)
Output:
==>D:\bat\SO\31672436.bat
==>D:\bat\SO\31672436.bat "LocalState\Cache"
md "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\Reports\"
==>D:\bat\SO\31672436.bat "LocalState\Cache" 2
echo "C:\Users\UName\AppData\Local\Packages\winstore_cw5\LocalState\Cache\2\"
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'm very fond of listening to my music and I've recently found a need to organize my folders. I have my Music folder and inside it, I have folders in the format Genre\Artist\Album. I want to create a Favorites folder for every sub-folder in the Music folder, but I already have some Favorites folders created so:
I'm trying to create a batch script that adds a Favorites folder to every sub-folder with the exception of Favorites and Discography Info folders.
I know that something along the lines of this:
for /r "%windir%\Users\%username%\My Music" %%s in (.) do md "Favorites" "%%s"
...must be used but I don't know the necessary commands to create folders in multiple folders.
How do I create exceptions in the mass folder-creating process?
How do I extend the range of the command above?
#echo off
setlocal enableextensions disabledelayedexpansion
set "root=%windir%\Users\%username%\My Music"
for /r "%root%" %%s in (.) do if not exist "%%~fs\Favorites" (
if /i not "%%~nxs"=="Discography" if /i not "%%~nxs"=="Favorites" (
if /i not "%%~fs"=="%root%" echo md "%%~fs\Favorites"
)
)
endlocal
Adapt as needed. When output to console is correct, remove the echo command before md to create the directories
My batch scripting is a bit rusty but you can filter out strings using the findstr command. Depending on your needs, this should be good enough. It will add the folder to and path that doesn't contain the words Favorites or Discography:
set targetdir="%windir%\Users\%username%\My Music"
dir %targetdir% /ad /b /s > %temp%\#contents.txt
for /f "delims=" %%d in ('findstr /i /v "\<Favorites\> \<Discography\>" "%temp%\#contents.txt"') do md %%d\Favorites
#ECHO OFF
SETLOCAL
SET "targetdir=c:\destdir"
for /f "delims=" %%s in ('dir /b /s /ad "%targetdir%"') do SET tdir=%%s\&CALL :test&IF DEFINED tdir ECHO md "%%s\Favorites"
GOTO :EOF
:test
SET $test=%tdir:\Discography\=%
SET $test=%$test:\Favorites\=%
IF NOT "%tdir%"=="%$test%" SET "tdir="
GOTO :eof
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MD to MD to actually create the directories.
Note that when an attempt is made to create a directory that already exists, an error message will be displayed. This is harmless but ugly. You can cause the error message to be suppressed by appending 2>nul to each MD ... line.
I need to delete all .jpg and .txt files (for example) in dir1 and dir2.
What I tried was:
#echo off
FOR %%p IN (C:\testFolder D:\testFolder) DO FOR %%t IN (*.jpg *.txt) DO del /s %%p\%%t
In some directories it worked; in others it didn't.
For example, this didn't do anything:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
What I'm I missing in the second snippet? Why didn't it work?
You can use wildcards with the del command, and /S to do it recursively.
del /S *.jpg
Addendum
#BmyGuest asked why a downvoted answer (del /s c:\*.blaawbg) was any different than my answer.
There's a huge difference between running del /S *.jpg and del /S C:\*.jpg. The first command is executed from the current location, whereas the second is executed on the whole drive.
In the scenario where you delete jpg files using the second command, some applications might stop working, and you'll end up losing all your family pictures. This is utterly annoying, but your computer will still be able to run.
However, if you are working on some project, and want to delete all your dll files in myProject\dll, and run the following batch file:
#echo off
REM This short script will only remove dlls from my project... or will it?
cd \myProject\dll
del /S /Q C:\*.dll
Then you end up removing all dll files form your C:\ drive. All of your applications stop working, your computer becomes useless, and at the next reboot you are teleported in the fourth dimension where you will be stuck for eternity.
The lesson here is not to run such command directly at the root of a drive (or in any other location that might be dangerous, such as %windir%) if you can avoid it. Always run them as locally as possible.
Addendum 2
The wildcard method will try to match all file names, in their 8.3 format, and their "long name" format. For example, *.dll will match project.dll and project.dllold, which can be surprising. See this answer on SU for more detailed information.
You can use this to delete ALL Files Inside a Folder and Subfolders:
DEL "C:\Folder\*.*" /S /Q
Or use this to Delete Certain File Types Only:
DEL "C:\Folder\*.mp4" /S /Q
DEL "C:\Folder\*.dat" /S /Q
I wrote a batch script a while ago that allows you to pick a file extension to delete. The script will look in the folder it is in and all subfolders for any file with that extension and delete it.
#ECHO OFF
CLS
SET found=0
ECHO Enter the file extension you want to delete...
SET /p ext="> "
IF EXIST *.%ext% ( rem Check if there are any in the current folder :)
DEL *.%ext%
SET found=1
)
FOR /D /R %%G IN ("*") DO ( rem Iterate through all subfolders
IF EXIST %%G CD %%G
IF EXIST *.%ext% (
DEL *.%ext%
SET found=1
)
)
IF %found%==1 (
ECHO.
ECHO Deleted all .%ext% files.
ECHO.
) ELSE (
ECHO.
ECHO There were no .%ext% files.
ECHO Nothing has been deleted.
ECHO.
)
PAUSE
EXIT
Hope this comes in useful to anyone who wants it :)
I don't have enough reputation to add comment, so I posted this as an answer.
But for original issue with this command:
#echo off
FOR %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
The first For is lacking recursive syntax, it should be:
#echo off
FOR /R %%p IN (C:\Users\vexe\Pictures\sample) DO FOR %%t IN (*.jpg) DO del /s %%p\%%t
You can just do:
FOR %%p IN (C:\Users\0300092544\Downloads\Ces_Sce_600) DO #ECHO %%p
to show the actual output.
this is it:
#echo off
:: del_ext
call :del_ext "*.txt"
call :del_ext "*.png"
call :del_ext "*.jpg"
:: funcion del_ext
#echo off
pause
goto:eof
:del_ext
set del_ext=%1
del /f /q "folder_path\%del_ext%"
goto:eof
pd: replace folder_path with your folder
Step 1:
Navigate to the folder in question using the cd command
For example:
cd C:\Users\tremanleo\Desktop\HoldLEOCMS
Step 2
Delete the the file type.
For Example:
DEL *.bak
If you are trying to delete certain .extensions in the C: drive use this cmd:
del /s c:\*.blaawbg
I had a customer that got a encryption virus and i needed to find all junk files and delete them.
I want to check all the files in one folder to see if they exist in another folder. When a file is found in the first folder that doesn't exist in the second folder I want that file to be deleted.
Is this possible?
Edit path info as appropriate (or incorprate batch arguments %1 %2), and remove ECHO that precedes DEL once you confirm you are getting the correct results.
#echo off
setlocal
set "dir1=."
set "dir2=d1"
set tempFile="%temp%\exclude%random%.txt"
dir /b "%dir2%" >%tempFile%
for /f "eol=: delims=" %%F in ('dir /b /a-d "%dir1%" ^| findstr /vixg:%tempFile%') do echo del "%dir1%\%%F"
del %tempFile%
Note - This solution is simply comparing names. Two completely different files will be considered the same if they have the same name.