Goal is to delete all jpg and png files in a folder.
del "ToDelete/*/*.jpg"
del "ToDelete/*/*.png"
pause
This code doesn't delete any of files. Are wildcards used correctly?
Wildcards can only be used in the last element of a path. You can use a for /D loop to loop through the parent directories of the files to delete:
for /D %%D in ("ToDelete\*") do (
del "%%~D\*.jpg" "%%~D\*.png"
)
Add the /Q switch to delete without prompting.
Note: In Windows, use \ as path separators as / might cause trouble.
In case you want to delete files in any directory depth, use switch /S of del:
del /S "ToDelete\*.jpg" "ToDelete\*.png"
Add the /Q switch to delete without prompting.
Related
I'm trying to find and remove ._ files using a batch file. The files were copied from a Mac onto a PC.
ECHO Again!
DIR /B /A-D /ON /S ._* 2>nul
DEL /S ._*
It will list them but won't delete them. What's up with that?
D:\projects\._my_colours.txt
D:\projects\._png_vs_jpeg.jsx
D:\projects\._rainbow_screen_sizes.jsx
D:\projects\._save_it_mac.jsx
D:\projects\._some_text.txt
Could Not Find D:\projects\*._
I can delete them by hand, however, I just want to know where I'm going wrong.
The files carried over from MAC should have the archive and hidden properties only, so to ensure that you only pick those up, I'd suggest that you only select those for deletion. The DEL command has an /A option which selects the files to delete based upon their attributes. As soon as you use the /A option it picks up all attributes as if you'd selected them. If you do not exclude any attributes using the - prefix it will delete them all, (except for Read-only, unless you've included the /F option too). For example DEL /F /A ._* will delete those with Read-only, System, Hidden, Archive, unIndexed and reparse points (L). In this case your wanting those with just H and A, so to exclude all others you should use their exclusion prefix. Additionally as ._* is a global wildcard, you'll probably want to use the /Q option to prevent a prompted request for confirmation.
Del /S /Q /A:HA-R-S-I-L ._*
As a note-worthy point, if you have MAC directories transferred over too, those may have file partners. For example the ready for archiving hidden directory .Trashes will be partnered with a ready for archiving hidden file named ._.Trashes along side it. These would be deleted using the wildcard above, so if you have those type of directories, and you're not removing those too, you may wish to use a different method in order to preserve their partner files.
You could do that from the command-line via a for-loop. (From a batch-file you need to escape the % characters with another, %%):
For /F "Delims=" %A In ('Dir /B /S /A:HA-D-R-S-I-L ._* 2^>NUL') Do #If Not Exist "%~dpxA\" Del /A "%A"
In this case, the DIR command selects all of the files using the same methodology as the DEL command used, and passes those files as metavariables to the Do portion. We then delete each passed file, as long as it isn't a partner to a directory. We can do that using the /A option alone, because DIR has preselected only those we want. To perform the check for a partner, we just use a simple IF NOT EXIST statement, remembering that when checking for the existence of a directory, we use a trailing backslash. The check is performed by expanding each metavariable, %A to its drive:, \path\ and .extension using %~dpxA. The expansion of D:\MyDirectory\SubDirectory\._.Trashes would return D:\MyDirectory\SubDirectory\.Trashes so the check performed will effectively be If Not Exist "D:\MyDirectory\SubDirectory\.Trashes\". If that directory does not exist then it is deleted using Del /A "D:\MyDirectory\SubDirectory\._.Trashes".
System files! D'oh!
DEL /AH /S ._*
I have a folder Target contains multiple folders,
each folder contains one file with the same name of the folder
I want to move the files in folders Part1,Part2,Part3,Part4 and Part5
to parent folder ("Target" in this case) using cmd then delete the folders.
The result should be like that :
In Linux i could've used mv Part*\*.* ..
I've tried copy "Part*\*" "" command,
but it doesn't work.
Use a For loop. The key to getting directory names in this code is "dir /a:d" which only lists directories. I put that into the %%a variable. Use %~dp0 to refer to the directory the batch file is in. If your bat is somewhere else, do a find and replace all for that to the directory path you need. Lastly use RMDIR to remove each folder with /q /s to make it silent and remove all files within the directory (part1 part2 etc...) and the directories themselves.
#echo off
for /f "tokens=* delims=" %%a in ('dir /a:d /b "%~dp0"') do (
copy "%~dp0%%a\*.*" "%~dp0"
RMDIR /q /s "%~dp0%%a"
)
In the Windows Command Prompt, copy "Part*\*" "" cannot work, because wildcards are only permitted in the last element of a path but not somewhere in the middle.
To work around this, use a for /D loop to resolve wildcards in Part*, then let copy deal with the remaining ones:
for /D %I in ("Part*") do #copy "%~I\*" ".."
To move the files rather than to copy, simply replace copy by move. If you want to remove empty sub-directories then, append rd using &&:
for /D %I in ("Part*") do #move "%~I\*" ".." && #rd "%~I"
To use the above code fragments in a batch-file, ensure to double all the %-signs.
I have a "release" folder inside c:\data.
I would like to delete the entire "release" folder, its sub directories and all files inside them using windows commands. After deleting it, I need to recreate the "release" folder and add another folder "app" inside "release" folder using commands.
I tried using rd /s and few other commands but it complains about non empty directory and workarounds to re-try rd /s command. I thought it should be fairly easy to do. Does anyone have a script / commands for them?
You can of course remove (rd) and recreate (md) a directory. However, you will lose its attributes then (like Hidden or Owner).
To maintain attributes, remove the contents of the directory but not the directory itself, either doing something like this:
rem // First remove all sub-directories with all their contents:
for /D %%I in ("C:\Data\release\*") do rd /S /Q "%%~I"
rem // Then delete any files located in the target directory:
del /Q /A "*.*"
Or this:
rem // Change into the target directory:
cd /D "C:\Data\release"
rem /* Try to remove the target directory and all its contents; since you are
rem in the target directory, it cannot be removed, although its contents can;
rem the `2> nul` portion suppresses the error message about access problems: */
rd /S /Q "." 2> nul
RM is not a windows cmd command. This should work:
IF EXIST "c:\data\" (
RD c:\data\release /S /Q
MD c:\data\release
MD c:\data\release\app
)
I need to delete 4 files, and 1 folder in a specific location and in unknown number of subfolders(different names). I would like to keep this as simple as possible. I managed to make this almost work, without any loops, but it doesn't work with folder removal in subfolders. All 4 files are deleted in main folder and all subfolders, but folder "scss" with all his content is still present in subfolders.
How to force this script to additionally delete folder in subfolders too ??
START /c cmd %this_dir%
DEL gulp_start.bat, .brackets.json, package.json, gulpfile.js /s /q scss
rmdir scss
You will have to use a For Loop.
Example
Add following to your batch.
FOR /d /r . %%d IN (scss) DO #IF EXIST "%%d" rd /s /q "%%d"
I am currently using this statement:
for /d %%X in (C:\Users\*) do (del %%X\Desktop\deleteme.txt )
Although I would like to use the "%%X" in to parts of this statement
e.g.
del %%X\%%X\deleteme.txt
How can this be done?
Any help would be greatly appreciated
It seems like you want to recursively delete files. This could be done by using the del command directly. By typing:
del deleteme.txt /s
deleteme.txt will be deleted from all subfolders. If that is not what you want you also could have your for loop call another batch file to perform the deletion from sub folders.
%%X will not work, as it gets fully expanded first.