Exclude Windows folder in CMD Search - windows

I am fairly new to Batch files but this is my Batch File for displaying path for Jpegs, Mp3, Mp4 etc.
#echo off
setlocal
cd /d C:\
Echo
echo Files Paths :
dir /b *.mp3 /s
dir /b *.mp4 /s
dir /b *.jpg /s
endlocal
pause
1.) Is there anyway that I can exclude Microsoft and Windows (wallpapers, icons, sounds, etc) folder from my search?
2.) How do I save the results in this output file (which is already created) C:\output.txt
Thanks!

This is quite an easy task for the findstr command:
dir /S /B /A:-D *.mp3 *.mp4 *.jpg | findstr /V /I /C:"\\Microsoft\\" /C:"\\Windows\\" > "C:\output.txt"
The \\ represent one literal \ in order to ensure that only directories become excluded whose full names match the predefined names. Since findstr uses the \ as an escape character \\ is necessary.
As you can see it is not necessary to use multiple dir commands. The filter option /A:-D excludes any directories to be returned even if they match one of the given patterns.
The returned data is written to a file using redirection. To append to the file rather than overwriting it replace > by >>.

Related

How to distinguish/target extension-less files? (Command Batch, Windows 10)

I'm working with files with various extensions, such as .bmp, .txt, etc, and I'm in the process of sorting them around through a batch file. However there are some files without any extension that I would like to be able to specifically manipulate.
How can I target extension-less files alone? Such as moving/deleting them exclusively. Or moving files with extensions while ignoring extension-less files?
If you only want to view or process files with an extension, use this syntax:
dir /b /a-d [path] | findstr /l "."
Only files containing a dot will be displayed.
You can add this to the for statement to get the output to a processable variable.
Of course, you must add an escape character before the pipe character in the for sentence.
However, to perform the operation in the subdirectories as well, it is best to use the for sentence, as a dot may appear in the folder name. like this:
for /f "tokens=*" %d in ('dir /s /b /ad') do #dir /b /a-d "%d" | findstr /l "."
To do the opposite (showing only files without an extension) simply add the findstr /v parameter.

How to copy all files found with script to another folder using Windows Shell

I want to copy the files that are found from a search into a folder.
The script
where /r C:\ *.jpg *.jpeg *.png *.gif >> C:\output.txt
gives a text document with the location of the files I want. How can I create a copy of the files found from the search into some output folder X:\output\
I want to get copies of all images in a folder including all subfolders into an output folder of all the images.
You could get the output of a command with a for /f loop:
for /f "delims=" %%A in ('where /r C:\ *.jpg *.jpeg') do echo working on %%A
or process a file with:
for /f "delims=" %%A in (C:\output.txt) do echo working on %%A
But for is capable to do it itself:
for /r "C:\" %%A in (*.jpg *.jpeg *.png *.gif) do ECHO copy "%%~fA" "X:\output\%%~nxA"
(remove the ECHO after troubleshooting to actually enable the copy command)
Note:
- this doesn't take care of possible duplicate names.
- this is batch file syntax. For use on command line, replace every %% with a single %

copy multiple file selected by criteria in windows cmd

I am trying to copy from one directory to another, all files that have 2 digits format (ex. 12.txt, 15.pdf, 25.doc... etc), this is in Windows.
In Linux this works:
cp -t target_directory {10..99}.*
In Windows what will be the solution?
This is ugly but should work
for /L %i in (10,1,99) do #copy %i.* dest_folder_here >nul 2>&1
It tries every file in the range suppressing conditions. Adjust copy to overwrite if needed.
I would filter for the correct files by findstr, like this:
for /F "delims= eol=|" %%I in ('dir /B /A:-D "??.*" ^| findstr /X "[0123456789][0123456789]\.[^.]*"') do copy "%%~I" "\path\to\destination"
I avoided to use range expressions [0-9] because they might also match ², ³.
If you also want to match files with no file name extension, change the search string to [0123456789][0123456789] [0123456789][0123456789]\.[^.]*.

deleting folders recursively using windows batch script

I have a folder with multiple subfolders under it. I would like to delete all test folders under the subfolders/example. How can I do this using windows batch script? Note that there is no test folder in some.
I know how to delete mainfolder/subfolder1/example/test. But am stuck with recursively deleting under each subfolder i.e delete mainfolder/*/example/test.
TIA
E.g:
mainfolder
subfolder1
source
example
test
subfolder2
source
example
test
subfolderX
source
example
for /r "c:\sourcedir" /d %a in (*) do if /i "%~nxa"=="test" echo rd /s /q "%a"
direct from the prompt - double each % to use as a batch line.
replace c:\sourcedir as appropriate
required rd is merely echoed to show what he script intends to do. Remove the echo keyword after testing to actually perform the deletion.
I recommend looking at ss64.com's CMD.EXE reference, specifically the DIR, FINDSTR, and FOR commands.
for /f %i in ('dir /s /a:d /b ^| findstr /i /e "example\test"') do rmdir /s %i
appears to be the appropriate command for what you have requested.
Notes on DIR: /S - Subdirectories, /A:D - directories only ("Attribute:Directory"), /B - "bare", no headers or footers, just the full pathname.
Notes on FINDSTR: /I - Case-insensitive, /E - Match at end-of-string
Verbose descriptive summary of command: Create a list of directories, including all subdirectories, and select only those that end in "example\test", then remove each of them, including all files and subdirectories in them.
for /R "mainfolder" /D %%a in (example\te?t) do rd /S "%a"
The only inconvenient of this method is that the name of the target folder must be given as a wild-card, so you must give a name that does not include any other undesired folder. If the name is given with no wild-card, the for command may include other folders.
You just need to iterate through the main directories, and check for the existence of the directory that you want to delete:
for /D %%d in (mainfolder\*) do if exist "%%d\example\test" rd /s /q "%%d\example\test"

I need a batch file to generate a list with *only* file names

I need it to work on Win10 and Win7 machines. If I can get this to work I'll make a batch file.
Winkey, "cmd"
cd "e:\media\trainingvids"
dir *.* /s /b /a -d > c:\temp\dork.txt
So, to state the obvious but make sure I'm getting it, I'm opening a command prompt, changing to the correct directory, doing a directory listing of all files (including sub-directories (/s), no headers or footers so 'bare' format (/b), and trying to NOT display the directory (/a -d) – and then sending/piping that (>) to a file I've designated to be named and created (dork.txt) in a temporary directory (\temp) that already exists on my c:.
The problem is that it doesn't work. I'm not able to find a way to NOT include the full path along with the file names. I need a nudge with the syntax. Or maybe I've got it all wrong and it can't be done in this way.
What does my Basic batch file look like that can do this?
You will need the for /F command to accomplish this:
> "D:\temp\dork.txt" (
for /F "eol=| delims=" %%F in ('
dir /B /S /A:-D "E:\media\trainingvids\*.*"
') do #(
echo(%%~nxF
)
)
You placed a SPACE between /A and -D in your dir command line, which must be removed.
Since I stated the full path to the target directory in the dir command and also to the output file, you can save this script at any location and run it from anywhere.
for /f "delims=" %%a in ('dir /s/b/a-d') do echo %%~nxa
should accomplish that task.
If you can tolerate double quoted names this batch file works well.
set myPath=c:\temp
set myMask=*.pdf
set myLog=c:\temp\myLogFile.log
FORFILES /P %myPath% /S /M %myMask% /C "CMD /C ECHO #file >> %myLog%"
Alter the values to meet your needs.
You're almost there with:
dir /s /w /a-d | find "."
The only drawback is that you get the file names in columns, and possibly a Volume line which you can remove with another find filter.

Resources