Using For /R to delete files from subdirectories - batch-file

This is a really beginners question. I have never work with scripts before and today I decided to start learning because of some unknown reason upgrading to Windows 10 duplicated many of my files. There is a pattern for music files, duplicates end in -1,-2,-3... and so on. I created a script with the following and it did work to delete duplicated files.
del E:\folder_name\*-1.mp3
del E:\folder_name\*-2.mp3
del E:\folder_name\*-3.mp3
However, there are too many folders and specifying one by one will take me forever. I found this script to recursively loop through subdirectories.
For /R E:\music_sample\ %%G IN (*-1.mp3) do Echo del "%%G"
Which produces the following output
but it does not actually deletes the files. Can you help me understand what I am doing wrong? Thanks!

Remove the term "Echo" from your script:
For /R E:\music_sample\ %%G IN (*-1.mp3) do del "%%G"

Taking a moment to learn something new is always great!
Sometimes there is already a tool for the job.
Agent Ransack allows you to search by patterns/regular expressions for files. This or something similar might save you some time in the future.

del /s "%userprofile%\music\*-1.mp3" "%userprofile%\music\*-2.mp3 "%userprofile%\music\*-3.mp3"
type
set u
and
del /?
for help.
Command prompt has it's own regular expressions. It's different to dos even if the tokens are the same. EG in dos * is only valid at the end of a filename or extension but in command prompt it is valid anywhere.

Related

Can someone explain this batch script?

Recently I've been trying to create my own batch commands to make everyday things a lot easier for me. I wanted to create a script that deletes everything within my Temp folder since it is always getting flooded with unnecessary files. I found this code online and it works, but I'm confused on what it's actually meant to do. I understand certain parts of it, but the majority has me confused. I understand it is using a forloop but that doesn't really tell me much. I know logically it makes sense, but I feel like the documentation isn't clear for what the entire script is doing. Anyway, here's the script:
set folder="C:\Users\%USERNAME%\AppData\Local\Temp"
cd /d %folder%
for /F "delims=" %%i in ('dir /b') do (rmdir "%%i" /s/q || del "%%i" /s/q)
Any help would be much appreciated, thanks!
Okay, so the first two lines are very simple. It just sets a variable to a folder path, then CDs into the folder. The /d flag is for changing the drive (C:) if it's currently set to another one.
Now, the last one is a for cycle. What it does is, basically: for each file in dir /b, which shows all files in current directory in bare format (only names), it either executes rmdir, which deletes a directory, or, if rmdir failed because the argument is not a directory, it just dels it. The /s/q stuff in both cases means: /s - delete from all subfolders, /q - do not prompt a Y/N.
Hope I helped!
The first line sets the value of the variable folder.
The second changes the drive and directory to the value assigned to folder.
The third first performs a directory list in basic form (names only) of the current directory (as has just been changed) and assigns each line of the resultant list, in its entirety, to %%i in turn.
The name %%i is then removed using a rmdir or rd command in /q quiet mode and /s including any subdirectories. This works on directories, but not files. The || invokes a del command, again with /s /qwith the same meanings and del deletes files.
hence, any name that is a directory is deleted, and if it's a file, then the rmdir fails and so the del deletes it.
The quotes are to ensure that any name encountered that contains a separator like space or comma is interpreted correctly.

win cmd: Deleting files that have no sidecars

Having troubles with cmd syntax, trying to delete files with specific extension in a specific folder that don't have sidecars. eg.: if folder contains:
1.A, 1.B, 2.A, 3.A, 4.A, 4.B
bat should only delete
2.A, 3.A
..hope that makes sense.
The code I've got so far must be real close, unfortunatelly not working
#echo off
FOR %%x IN (%1\*.A) DO
(
IF not exist "%1\%x.B" del "%1\%x.A"
)
Any help mostly appreciated.
Comments to question are correct
FOR variable percents must be consistently doubled when used in batch script
Open paren must be on same line as DO. But there isn't any need for parens with such a simple script.
Also, you want only the base name of the FOR variable, so you need the ~n modifier.
I made the code a bit more robust by using PUSHD at beginning.
#echo off
pushd %1
for %%F in (*.A) do if not exist "%%~nF.B" del "%%F"
popd

How do I stop my DEL command from deleting files with the same names in a subdirectory?

So my del command deletes two files instead of one only.
My filesystem with the file I wish to delete looks like this (an example):
/executable.exe
/folder/executable.exe
When I run del /s /q "executable.exe", both executables will be deleted. I'm running Windows 10, any way to stop this happening? I can absolutely not change the second executable's name.
The comments to your question are all quite apropos. The solution to your problem is simply to omit the /S switch, as this causes the DEL command to delete from the current directory, plus all of the subdirectories, all the way down to the bottom of the filesystem.
CMD.EXE offers help for most commands; type the command name followed by /? to get the command help (e.g., DEL /?). You can also get the same information by typing HELP followed by the command of interest, e.g., HELP DEL.
You should not use a destructive command (like DEL) without first understanding what the command will do. Read the help; that's why it was provided.

Locate and Delete Folder(s) with Command Prompt

At work we handle people every day who wants help regarding signing on to their Online Bank from home. Some times we need the user to delete files and guiding them to do this, can be tiresome and bothersome a lot of the time, wasting upwards to 20 minutes because the user is inexperienced in computers.
We have been talking about a solution (if possible) where we let the user download a batch file that we tell them to run, which can then delete the required files for us. I ask this question here, because none of us here at work, have experience with batch files.
Personally I've played around with it from time to time but I can't really come up with a solution that fits our needs and when I search around on the net, I can't find a solution that fits either. The script would have to locate the folder automatically (if possible).
Is this possible?
Would you be able to help me accomplish this?
Thanks in advance!
Maybe this could help you, I commented it so you understand what it is doing ;)
:: Program to remove Folders which fullfill the criterias
:: You won't see the commands in the console
#echo off
:: Go to drive C (i think you'll need application data or similar and this is
:: on c, but if the bat is started in an other partition, it will search the
:: one he starts in
C:
:: Go to the path in which you'll search, if you want C:\ , remove this
CD "C:\yoursearchstartpath"
:: Okay, this is the loop, it gets all folders ( /A -D is for folders)
:: which have delMe in their name and then asks if you want to delete it
:: showing the path to make sure you don't delete sth accidentially
for /f "delims=" %%a IN ('dir /S /B /A -D *delMe*') do RD /S "%%a"
:: That the batch file is not closed automatically but shows that it's finished
set /p id="Finished, press Space to quit " %=%
A simple one:
rd /s "%USERPROFILE%\.oces2"
If they follow a pattern (e.g. start with .oces):
#echo off
cd /d "%USERPROFILE%"
for /d %%i in (.oces*) do rd /s "%%i"
pause
Check out this link
http://support.microsoft.com/kb/65994
I created a sample batch file A.BAT. It checks if the C:\Folder\F1 exists, and removed the folder F1 if available
IF EXIST D:\FOLDER\F1 GOTO A
EXIT
:A
D:
DEL D:\FOLDER\F1\*.*
RMDIR F1
Hope it helps

Concatenating files in multiple directories from batch file

I'm trying to concatenate files from multiple directories. From a single directory, I know that you can execute
copy /B *.blah all.blah
to concatenate the files with extension .blah to a single file named all.blah. What my structure looks like is:
level 1/
level 2_1/
file_1.blah
file_2.blah
level 2_2/
...
level 2_3/
...
do_not_include_this_directory/
...
What I'm looking to do is create a single all.blah file in the top level directory which is a concatenation of all the .blah files in the level* sub-directories, without including any files from the do_not_include_this_directory directory.
My aim is to do this in a batch file (there will be other file concatenation logic for different directories included in this batch file), but I've spent the hour past playing around with cmd for logic to no avail (some of my directories have spaces in the names). Maybe this is something that I should just do using a python script? I'm thinking that this can be done relatively easily though using some for loops with copy, but my skills with these things are lacking to say the least (just came across cmd for about 2 hours ago).
Does anyone know how to do this, or would you just recommend that I buck up and write something using Python? Any help or suggestions would be appreciated.
It can be done easily from the command line without a batch script :)
copy nul all.blah >nul&for /d %F in (level*) do #copy /b all.blah + "%F\*.blah" >nul
As a batch script
#echo off
copy nul all.blah >nul
for /d %F in (level*) do copy /b all.blah + "%F\*.blah" >nul
I'm not sure if the /B switch is exactly correct. It has different meaning depending on where it appears: before any file, after a source, or after the destination.
This seems suitably horrific for a Windows batch file. Tested under Windows 7; YMMV, etc.
#rem get all pathnames, even in excluded directories
#rem EDIT THIS COMMAND to change wildcard to match
dir /b/s *.c >files.tmp
#rem get rid of things with prefix we want to exclude
#rem EDIT THIS COMMAND to change prefix
findstr /V "C:\temp\fee" files.tmp >files2.tmp
del copy.tmp
#rem append things one at a time the hard way!
For /f tokens^=*^ delims^=^ eol^= %%a in (files2.tmp) do (
copy "%%a" + copy.tmp copy2.tmp
del copy.tmp
rename copy2.tmp copy.tmp
echo.%%a)
#rem clean up
del copy2.tmp
del files.tmp
del files2.tmp

Resources