Windows command line - Remove files with specific extension, but not ones where extension contains substring (doc not docx) - windows

I'm using wordconv.exe to convert a bunch of a old doc files into docx. Afterwards I'd like to delete the original doc files.
When I run "del /S *.doc" on the command line it deletes both the doc and docx files. Anyway to get it to delete just .doc files?
Thanks!

Let's try to use something like this:
forfiles /s /m *.doc /c "cmd /c del #file"

The problem is the short 8.3 names of the files, because for internal cmd commands and also many external ones, wild-cards (*, ?) also match against them. For example, a file like important-document.docx has got a short name like import~1.doc, which is matched by *.doc.
There are some commands that treat wild-cards differently:
forfiles (as already demonstrated in the answer by Michał Mielczarek):
forfiles /S /M "*.doc" /C "cmd /C if #isdir==FALSE del #relpath"
The #isdir query checks whether the current item is a file and lets skip folders.
where:
for /F "delims=" %%F in ('set "PATHEXT=" ^& where /R "." "*.doc"') do #del "%%~F"
The surrounding for /F loop captures the result of where. The command set "PATHEXT=" deletes the PATHEXT variable in the cmd instance in which where is executed, because this also regards that variable, so a file like important-document.doc.com could also be matched unintentionally with a default value of PATHEXT of .COM;.EXE;.BAT;.CMD, for instance.
Although the where approach appears a little bit more complicated, you might prefer it, because it is faster than forfiles, particularly when having a huge directory tree.
N. B.:
forfiles /M "*.*" does not match files (or folders) with no extension (so use /M "*" to match something like testfile), but where "*.*" does, so this indicates that these two commands have their own individual wild-card handling/substitution routines.

for /f "tokens=*" %%? in ('dir /b /a-d /s *.doc') do if /i "%%~x?"==".doc" del "%%~f?"
You can add if exist "%%~dpn?.docx" before del, if .doc and .docx are in same folder.

Related

When I put a batch program in the task scheduler, it didn't finish the whole task, so why did this happen?

This problem is from the scheduler task, batch program, or the files in a folder that I want to delete it using the batch program?
and this is the batch program:
forfiles /p "D:\nameOfFolder" /s /m *.* /d -7 /c "cmd /c del #path"
Replace /M *.* by /M * to even include files without extension, because forfiles treats wildcards differently than most other commands. /M * may even be omitted since this is the default setting anyway.
Regard that forfiles iterates both files and directories, so you need to exclude the latter, because del may try to delete its contents then. Therefore, implement a condition based on the forfiles-specific variable #isdir.
forfiles /S /P "D:\nameOfFolder" /M * /D -7 /C "cmd /D /C if #isdir==FALSE del #path"
As a side note, never append a trailing backslash to a (quoted) path, because something like /P "D:\" would let the command fail since \" constitutes an escaped quotation mark for forfiles, ruining the command line. You may however specify /P "D:\.".
Are you sure you even have such files?
I tried the following (very similar) command:
forfiles /p "C:\Temp_Folder" /s /m . /d -7 /c "cmd /c echo #path"
ERROR: No files found with the specified search criteria.
One thing I noticed, is that the name of the directory should not end with a backslash:
forfiles /p "C:\Temp_Folder" is working fine.
forfiles /p "C:\Temp_Folder\" is not working.
. on its own is not a valid searchmask to supply to /m.
You ned to use *.* (all) *.ext (the supplied extension) *string*.* (files with names containing the string).
? is also supported as a searchmask to match any character. example: *.?a* would match any file with an extension type with a as the second character

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.

Command line to list files with timestamps, sizes, and paths

I have the following command line that extracts the timestamp, size, and file name of specific files, however I need the entire path listed. What should I use for this task?
DIR "C:\Users\Heather\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\" "*Process*" /S /A:-D /TW /OSD
Assuming you are looking for files matching the pattern *Process* within the directory C:\Users\Heather\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\, you could use the following code:
forfiles /S /P "C:\Users\Heather\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5" /M "*Process*" /C "cmd /C if #isdir==FALSE echo #fdate #ftime #fsize #path"
The great advantage of the forfiles command is that the time stamp is returned with a resolution of 1 second (rather than 1 minute as with dir or for).
However, the sort order you used in your dir command line (/O:SD) is not maintained.
for /f "delims=" %a in ('DIR "C:\Users\heather\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\" "*Process*" /S/b /A:-D') do #echo %~za %~da %~ta %~fa
should provide that data.
Note that you appear to be looking for all files in "C:\Users\heather\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\" and also for "Process"

How do I write a batch script to search for a specific file based on folder name?

I need to write a batch script that searches for occurrences of a file named SQLite.Interop.dll in a certain directory. There will actually be many occurrences of this file nested under different subdirectories, and specifically I'd like it to find the one where the folder name is net45.
I started to try and write a batch script myself by piecing together different StackOverflow answers, but ultimately didn't get very far. Here's my initial, feeble attempt:
#setlocal enableextensions enabledelayedexpansion
#echo off
for /r C:\Specified\Directory %%i in (SQLite.Interop.dll) do (set str = %%i & if x%str:net45=%==x%filestr% (copy %%i ./SQLite.Interop.dll & goto BreakStmt)
:BreakStmt
endlocal
Things not yet working:
The Specified Directory path on the for /r statement
Substring searching for net45 in the file path
Not sure if & is the proper way to chain commands?
General syntax... I'm a bit like a fish out of water with this batch stuff...
Use recursive dir /s and filter the output by the directory name surrounded by \, parse the result with for /f:
for /f "delims=" %%a in ('
dir /s /b "C:\Specified\Directory\SQLite.Interop.dll" ^| find /i "\net45\"
') do copy /y "%%a" .
This method doesn't require delayed variable expansion.

Duplicate error when renaming files using FORFILES in BATCH with a search mask

I'm running a batch script using FORFILES with the search mask /M *.log.* on files such as these:
a.log.1
a.log.2
a.log.3
I want to rename them by appending the current date and move them to the destination folder. But I get an error stating:
Duplicate file names
Only the file a.log.1 is moved to its destination and renamed.
This is my code:
for /f "delims=" %%G in ( 'forfiles /s /c "cmd /c echo #path" /d -7 / m *.log.*' ) do ren "%%~G" "%%~nG-%Ret%"
The only thing that differentiates the three files is the extension (.1, .2, or .3). But your target name uses the base name only, without the extension, so of course you get duplicate names. You could change your target name to include the extension using "%%~nxG-%Ret%". (I assume you have defined Ret elsewhere.)
But I don't see any need for your FOR loop - the FORFILES can do the rename directly:
forfiles /s /d -7 /m *.log.* /c "cmd /c ren #path #file0x22-%Ret%0x22"
I don't know what your date format in %Ret% looks like, so I included quotes (0x22) around it just to be safe. It looks weird, but a name like "name.ext""date" works just fine - the quotes are stripped out, but they protect against spaces and special characters.

Resources