CMD DIR filename and path - windows

I perform a search in the CMD shell.
The goal is to find all files with an x from DRIVE C. I only need to see the path and the filename.
I have tried this so far. If I insert the following, I only get a path.
DIR "C:\*x*.*" /s /b
How can I also return the filename?

You could use the Where command too:
Where/R C:\ *x*
You can add an /F too which places all output files into double quotes.
Where/F /R C:\ *x*
Drives require terminating with a back slash as above directories do not.
Where/F /R C:\Users\belsober *x*
Note: I've maintained the search mask you used, just remember that this will also match every single file extension too, e.g. .xml, .xls, .txt, .ocx, .exe and .docx

Related

Batch file to copy files

Need to have a batch file that will actually check if the file name has "abc" in its name and if its there , then it will initiate a copy.
So suppose folder /test has 5 files
apt.text
mar.text
may.text
Jan.text
abc_xyx1234.text
So since "abc" appears in string only in the last file , it should pick only last one.
I tried
#echo off
for /f %%a in (' dir /b /a-d ^| find /v /i "abc" ') copy "%%a"
pause
but it didn't work out.
You have no destination for your copy.
The find /v /i will find those filesnames that do not (/v) match abc
It's probably easier to use
copy *abc* destination
I think part of your problem is the following:
Even though forward slashes work sometimes, Windows' canonical path separator is the backslash \. Using forward slashes as path separators confuses programs which use the forward slash as parameter prefix, and most if not all of the Microsoft Windows tools do so, including FIND and FINDSTR.
Also, if you're talking about /folder - which should be \folder as explained above - you're talking about a directory in the root of the current drive. I'm not sure you really mean that.
Apart from that, Magoo's suggestion is still kind of valid, but it omits the path "problem".
So let's use absolute directories to clarify:
I'll assume your source directory is c:\work\source and contains the file set you've stated in your question. I'll assume your target directory is c:\stackoverflow\target. So you can do the following from anywhere on your system:
copy c:\work\source\*abc* c:\stackoverflow\target
If you're doing this from an arbitrary folder on drive c, you could omit the drives:
copy \work\source\*abc* \stackoverflow\target
And, if you're inside the source folder, you could use Magoo's answer:
copy *abc* \stackoverflow\target

How to do a simple file search in cmd

I want to quickly search for a file given its name or part of its name, from the windows command line (not power shell). This is similar to opening explorer and using the search box at the top.
Note: dir can search based on a string template but it will not search in the subdirectories.
Note2: findstr can be used to search for a token inside files and has a recursivity flag; it's funny that a more complex find can be easily discovered ...
dir /s *foo* searches in current folder and sub folders.
It finds directories as well as files.
where /s means(documentation):
/s Lists every occurrence of the specified file name within the
specified directory and all subdirectories.
dir /b/s *.txt
searches for all txt file in the directory tree. Before using it just change the directory to root using
cd/
you can also export the list to a text file using
dir /b/s *.exe >> filelist.txt
and search within using
type filelist.txt | find /n "filename"
EDIT 1:
Although this dir command works since the old dos days but Win7 added something new called Where
where /r c:\Windows *.exe *.dll
will search for exe & dll in the drive c:\Windows as suggested by #SPottuit you can also copy the output to the clipboard with
where /r c:\Windows *.exe |clip
just wait for the prompt to return and don't copy anything until then.
EDIT 2:
If you are searching recursively and the output is big you can always use more to enable paging, it will show -- More -- at the bottom and will scroll to the next page once you press SPACE or moves line by line on pressing ENTER
where /r c:\Windows *.exe |more
For more help try
where/?
dir *.txt /s /p
will give more detailed information.
Problem with DIR is that it will return wrong answers.
If you are looking for DOC in a folder by using DIR *.DOC it will also give you the DOCX. Searching for *.HTM will also give the HTML and so on...
You can search in windows by DOS and explorer GUI.
DOS:
1) DIR
2) ICACLS (searches for files and folders to set ACL on them)
3) cacls ..................................................
2) example
icacls c:*ntoskrnl*.* /grant system:(f) /c /t ,then use PMON from sysinternals
to monitor what folders are denied accesss.
The result contains
access path contains your drive
process name is explorer.exe
those were filters youu must apply

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

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.

How to search a hard drive for a directory name using DOS

Is there a way to find a file on the C: of a computer , using a DOS command, without having to CD to the root of the C: drive? I would like to do it using the DIR and the FINDSTR command only.
Ultimately, my goal is to search for a file and then store the parent directory name in a ENV variable without changing directories and hopefully without creating a temp file.
Have a look at How to find a file in MS-DOS.
dir *bob*.* /s
See also List of DOS commands
This gets the complete file name with directory in a useable form.
dir C:\FILENAME.EXT /s /b
c:> findstr /s /i *.ext
REM finds in drive c: every file called *.ext containing (case insensitive)

Copy a directory tree to a single directory at a command line

Anyone know of a command line utility (or one that can run as a command line) that will collect all the .jpg files in a directory tree to a single folder, only copying files that change?
I started with Renamer, which is great for renaming files in their current directories, but fell short when I tried to mangle the path. This is probably because I don't know Renamer that well. I ended up creating a text file directory dump, then using a REGEX find / replace to create a batch file, but this is hardly efficient nor automated.
The REGEX:
(G:\DIR\DIR\)([0-9]+\)([0-9]+\)([0-9]+\)([0-9]+\)(p[0-9]+.jpg)
changed this
G:\DIR\DIR\00\00\00\00\p0000000000.jpg
to this
G:\DIR\DIR\p0000000000.jpg
(copy \1\2\3\4\5\6 \1\6) in the batch file.
I need to run the whole thing as a scheduled task without a real person logging in. Not really looking for a Zip file because I don't want to disturb the system processor, plus most of the files will not change from day to day. This is more of a file sync.
In a Windows command line you can do this:
for /R A %i IN (*.jpg) DO xcopy %i B /M /Y
Where A is the source directory and B is the destination directory. You need to have command extensions enabled, which I believe is the default.
A couple of notes from the comments:
If any of your paths could have spaces in you will need to add quotes around the second %i. This prevents the string being interpreted by the xcopy command as two separate parameters. You may need to do the same around A and B paths. Like this:
for /R "A" %%i IN (*.jpg) DO xcopy "%%i" "B" /M /Y
If you are putting this inside a .bat or .cmd file you will need to double the percentage like this.
for /R A %%i IN (*.jpg) DO xcopy %%i B /M /Y
The /M option on xcopy will only copy files with the Archive bit set and then unset this bit. This prevents the files being copied twice. If you have other processes that also alter this bit it may cause issues. It is also possible to use the /D option which compares the file's last modified time with that in the destination and only copies newer files.
I'm guessing you're on Windows from the path format.
I've not read the whole thing, but http://www.infionline.net/~wtnewton/batch/batguide.html#6a might help you.
The same page has dizzy.bat, (http://www.infionline.net/~wtnewton/batch/dizzy.bat) which should be trivial to edit to do what you want.
In a Unix environment I would use find or rsync (and maybe some features of the shell). Cygwin and MinGW come with find, maybe with rsync. You can also probably get a standalone port of find for Windows somewhere.
If the SOURCE shell variable is the directory containing subdirectories with files to copy, and the DEST shell variable is the directory to copy them to:
find $SOURCE -name \*.jpg -exec cp --update \{\} $DEST/ \;
find is by nature recursive. "-name \*.jpg" selects files that match that pattern. You can add additional conditions with -and. The --update option to the cp command (or -u) only bothers copying the file if changed or not yet copied. There are other options to cp that might be useful too.
If $SOURCE is the same as $DEST as in your DIR/DIR/ example, then find will also find the destination files (already copied), though this will be ok, cp will recognize that you are trying to copy the same file to itself and skip it, but if you want to avoid that wasted work you can use 'for' and 'if' (or something) to only run find on the subdirectories of DIR/DIR/.
You can also use rsync, which has options that can delete files from the destination directory if they have also been deleted from the source directory, and many other such variations.

Resources