Windows CMD: List files in dir & subdir WITHOUT given extensions - windows

I'd like to recursively search a directory and find files, which have NOT a certain extension, or precisely, which have NOT a certain set of extensions.
Sketch: find in "dir" all files without "ext1", "ext2", "ext3" and print results to .txt
I tried around several hours with DIR and ATTRIB, but unfortunately without bigger success.
Your consideration is highly regarded! Thanks.

Try this:
dir /b /s /a-d | findstr /vi ".ext1$ .ext2$ .ext3$"
The /a-d switch excludes directories, giving you only files. The findstr parameter lets you search the files for strings, and the /vi switch indicates to exclude files containing the next parameter, the search being case insensitive.
As Joey pointed out, the $ is necessary to indicate end of the line.

Related

How to get only the number of all files on a drive?

I want to get only the number of files on my hard disk drive.
I tried:
DIR /S C:\
But it lists nearly forever all the files recursively and at the end I get what I want: the number of files.
How do I get only the number of files like 300 000?
List files in bare format and pipe to find, which simply counts all lines:
dir /s /b /A-D-L c:\* | find /c /v ""
lists all files (including hidden and system files) without folders. You can adapt which files to include/exclude. See dir /? for details

replace with files from multiple directories

I have a large amount of JPEG-files that have been sorted and placed in several different directories. Unfortunately these directories were lost - although I was able to restore them from an older version. However all the files are corrupted.
Luckily I still have the JPEG-files that aren't sorted and I want to replace the corrupted files with these healthy files by filename. So basically go through the unsorted files and match them with the sorted files.
I found out that by using the command replace I'm able to do this and it works perfectly with one exception: I can only go through 1 source directory at a time. This is a problem because the unsorted JPEG-files are located in ~2000 different directories.
What I'm currently doing is:
replace "folder1\folder2\folder3\folder4\*.jpg" "sorted_images_dir" /s
What I'd like to do is:
replace "folder1\*EVERY SUBDIRECTORY OF folder1*\*.jpg" "sorted_images_dir" /s
Is this possible on the commandline or would I have to create a batch file? If so, any advice for this? It seems like it would be a rather simple task.
read HELP FOR and try FOR /D %a in (*) do #echo %a this way you might iterate over all the folders in the current directory.
You might then try FOR /D /R %a in (*) do #echo %a to iterate recursively on all the folders and subfolders.
So, finally you can try
FOR /D /R %a in (*) do #replace "%a\*.jpg" "sorted_images_dir" /s

want to copy names of files to text in a directory based on their file extension

I want to copy names of files to text in a directory based on their file extension.
As of now I am using dir /b >i67.txt which works fine for me but its not resolving problem of specific file extensions.
Can someone help me in getting a batch script for the same.
You are looking for the following command, run it in the context of the directory which contains your files:
dir /b /s /-p *.txt /o:n | findstr /E .txt > i67.txt
Using the above code example, you will be able to find all *.txt files in the directory and output the results into the i67.txt file (will be outputted to the same directory).
You can specify multiple file masks within one DIR /B command. Based on your comment to Yair Nevet's answer, it seems you want the following extensions: .ovr, .inc, and .dat. That can be done simply using:
dir /b /s *.ovr *.inc *.dat >i67.txt
If the files are on an NTFS volume that has short 8.3 names enabled, then you might get additional undesired file extensions if you have any file extensions longer than 4 characters that begin with your wanted extension. For example someName.data would show up in your output because it most likely would have a short name of SOMENA~1.DAT that matches your file mask.
You can prevent short name inclusion by piping the output to FINDSTR. The /L option forces a literal search as opposed to regular expressions, the /I option ignores case, and the /E option matches only the end of each line. Multiple search terms are delimited by spaces.
dir /b /s *.ovr *.inc *.dat | findstr /lie ".ovr .inc .dat"
Regarding your following comment:
Here is what I am using now: dir /b | findstr [a-z].*ovr>i67.txt &&
dir /b | findstr [a-z].*inc>>i67.txt && dir /b | findstr
[a-z].*dat>>i67.txt What it does?? --- It copies all
names(remember,only name except files itself which are ending with
extension .ovr .dat and .cpi ) present in a directory and copy it to a
text file(here name is i67.txt)
That will not actually do what you want for several reasons.
Windows file names are not case sensitive. Windows would treat NAME.OVR and name.ovr the same, so you should as well. That requires the /I option.
There is nothing in your search to anchor ovr to the extension. It will look for your pattern anywhere within the file name. And the dot is a meta character that represents any character - not a literal dot. The asterisk allows the dot to match any number of characters.
I can't be sure, but it looks like perhaps you only want to match files that begin with a letter. The following modification to my answer should do the trick:
dir /b /s *.ovr *.inc *.dat | findstr /ri "^[a-z].*\.ovr$ ^[a-z].*\.inc$ ^[a-z].*\.dat$"
The \R option forces a regular expression match instead of a literal. It is the default behavior for the given search, but it is a good idea to be explicit with regard to regex vs literal search.
^ anchors the search to the beginning of the name
[a-z] matches any letter (sort of). Remember it is not case sensitive because of the /I option. Without the /I option, it would not match upper case Z. See Why does findstr not handle case properly (in some circumstances)? for an explanation.
.* matches any number of characters, without restriction
\. matches a dot literal, marking the beginning of your extension
Then comes your extension
$ anchors the match to the end of the name

Command to list all files in a folder as well as sub-folders in windows

I tried searching for a command that could list all the file in a directory as well as subfolders using a command prompt command.
I have read the help for "dir" command but coudn't find what I was looking for.
Please help me what command could get this.
The below post gives the solution for your scenario.
dir /s /b /o:gn
/S Displays files in specified directory and all subdirectories.
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
Then in :gn, g sorts by folders and then files, and n puts those files in alphabetical order.
If you want to list folders and files like graphical directory tree, you should use tree command.
tree /f
There are various options for display format or ordering.
Check example output.
Answering late. Hope it help someone.
An addition to the answer: when you do not want to list the folders, only the files in the subfolders, use /A-D switch like this:
dir ..\myfolder /b /s /A-D /o:gn>list.txt
If you simply need to get the basic snapshot of the files + folders. Follow these baby steps:
Press Windows + R
Press Enter
Type cmd
Press Enter
Type dir -s
Press Enter
An alternative to the above commands that is a little more bulletproof.
It can list all files irrespective of permissions or path length.
robocopy "C:\YourFolderPath" "C:\NULL" /E /L /NJH /NJS /FP /NS /NC /B /XJ
I have a slight issue with the use of C:\NULL which I have written about in my blog
https://theitronin.com/bulletproofdirectorylisting/
But nevertheless it's the most robust command I know.
The below post gives the solution for your scenario.
**dir /s /b /o:gn**
/S Displays files in specified directories and all subdirectories.
/B Uses bare format (no heading information or summary).
/O List by files in sorted order.
:gn, g sorts by folders and then files, and n puts those files in alphabetical order.
Just for all files except long path, write the following command:
**dir /b /o:gn**
For Tree:
write in your cmd
tree /f

Batch File to Delete Files in Folders

I have many folders in a directory that contain various files. Each filename begins with XXX_ where XXX could be the name of the folder the file is in. What I am needing to do is to go through all those folders and delete any file where XXX is the name of the folder that file is in.
Please have an eye out this question: Iterating through folders and files in batch file?.
I think this should help you.
Please let me know if you need further assistance.
EDIT #1
The joker character in DOS command line is *. Then, while searching a directory for certain files, you may consider your regular expression, that is, your XXX_, and complete it with *, this shall return only the files for which you're looking for.
This means that instead of *.zip pattern in one of the FOR loops given the linked question, your first FOR loop should contain your directory name, then take this variable concatenated with the * character to obtain only the files you're looking for.
For example, consider trying the following:
dir /s XXX_*.*
This should return only the files you're interested in, given the right folder name.
EDIT #2
Thanks for having precised your concern.
Here is a code sample that, I do hope so, should help. Now I know you say you have the looping correct, so that perhaps only piece of this code might be needed.
#echo off
setlocal enableextensions enabledelayedexpansion
for /F "delims==" %%d in ('dir /ogne /ad /b /s .') do (
for /F "delims==" %%f in ('dir /b "%%d\%%~nd_*.*"') do (
echo %%d\%%f
)
)
endlocal
This works and lists the files contained in subfolders from the current (.) folder.
I have tested it from the following folder:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1
Where a 'XXX' folder is contained. This 'XXX' folder contains the following files:
Copy of XXX_blah.bmp;
XXX_blah.bmp;
XXX_1234.ppt;
XXX_textfile.txt.
From this structure, the output is:
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_blah.bmp
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_1234.ppt
C:\Docume~1\marw1\MyDocu~1\MyMusi~1\XXX\XXX_textfile.txt
I then suspect that putting a del instruction instead of an echo command shall do the trick. This means that to isolate the foldername itself from its path, you need to use the ~n instruction with your folder variable name like %%~nd, where your iterating folder variable name is %%d.
Furthermore, you could even use a parameterized batch file in the process, instead of hardcoding it, that is, if your 'set YourFolder =...' is part of your production code. This could look like:
#echo off
setlocal...
set root = %1
set root = %root:~1%
set root = %root:~0,-1%
...
endlocal
Instead of having '.' as pictured in my first FOR loop, your would replace it with "%root%" in order to consider your command line parameter instead of a hardcoded filepath.
I do help this helps, sincerely!
As Ron says, since the question is tagged "windows".
EDIT:
Ron's answer, which seems to have disappeared!, was to use del /s
EDIT2:
OK, it's valid only for file names, not for directories. For the directories you'd have to use something like sketched below.
Additional info: when you want to do the same thing recursively to files in a directory tree, and (unlike del) there's no command that already does the traversing for you, you can use the /R option of the for command.
To see the for command's docs, do e.g. start "for-help" cmd /k for /?.
Cheers & hth.,
– Alf
cd C:\"foldername"
del /s XXX_"*"
cls
exit

Resources