Search and then delete depending on whether files contain a string - file

I'd like to search through multiple text files in a single directory for a string ('monkey'), if the string exists, then either, depending on what's easiest:
rename the matching string - e.g. change monkey monkey1 and save then file and carry on searching/processing
or
Delete any file that has the matching string.
Have searched but can't seem to find anything straightforward.

Modifying the contents of a text file is fairly complex using native Windows batch commands, so option 1) is not easy. Though it is easy if you download a 3rd party tool like gnu sed for Windows.
Option 2) is very easy. You can do it on the command line without a batch file. Assuming your current directory is where you want to look for the files:
for /f "eol=: delims=" %F in ('findstr /m monkey *.txt') do del "%F"
If executed from within a batch file then you need to double up the percents - use %%F instead of %F.
There are many options to the FINDSTR command, such as /I for case insensitive search, /S to search subdirectories, and /R for primitive regex searches.

Related

A batch file to delete a file if (a part) of its filename is not found in a text file?

I have studied this: Is it possible for a batch file to delete a file if not found in list from text file?
In my case there is a list of words (one in each line) in a text file e.g:
ext
abcd
in which there are extensions of the files (like ext above) or fragments of filenames' bases (like abcd above).
So, I don't want files like *abcd* or *ext* to be deleted (where * is for any number of any allowed characters) - and I do want to delete any other files from the directory.
I took this code:
for /f "eol=: delims=" %%F in ('dir /b /a-d "SomeFolder" ^| findstr /vibg:"ExcludeFile"') do del "SomeFolder\%%F"
and what I did to it - I deleted /b option of findstr (so now it is
...findstr /vig... - so as I think findstr shoud try to match strings from the list (.txt file) to any part of the 8.3 filenames in directory. But it does not. It does not exclude files witch matching extensions from deleting, (it deletes for instance antything.ext despite the string ext is in the list). Can you help please?
PS Thanks for trailing spaces, it helped - is it possible to use an implication of two conditions e.g a file would be deleted if:
a part of filename is not on exclude list
and a file has a specific extension (let's say .bmp).
In above example the file 123.xyz would be deleted because of the first condition (not a part of it is abcd nor ext), but would not be actually because of the second condition (it's extension is not .bmp)?
Use the following in your Exclude File.
.*abcd.*
.*\.ext$
Remove the /B option from the FINDSTR command.
for /f "eol=: delims=" %%F in ('dir /b /a-d "SomeFolder" ^| findstr /vig:"ExcludeFile"') do del "SomeFolder\%%F"
I would read the help for the FINDSTR command so that you understand what the regular expressions are doing in your exclude list. Here is brief list of the options I used.
. Wildcard: any character
* Repeat: zero or more occurrences of previous character or class
$ Line position: end of line
\x Escape: literal use of metacharacter x

Windows command line renaming files in subfolders?

I didn't find so far a similar question, so I try to explain the problem:
I have large number of files that are in subfolders inside "C:\images"
I have a list of names in two columns, so that 1st column is old filename and 2nd is a new filename. I want to change that list into a batch file.
Names are pretty unique so I want to make batch file - so that will be one command for every file to be renamed.
RENAME "C:\images\768e11ab.jpg" "4ca5d042.jpg"
RENAME "C:\images\5402c708.jpg" "b802820b.jpg"
RENAME "C:\images\1c039e0e.jpg" "80ce9797.jpg"
etc...
It is rather simple, only, files are scattered across subfolders. Is there any way to make a command so it will look for that specific file in all subfolders in "C:\images" and rename it.
Following some similar questions tried this, with no result:
for /r "C:\images\" "%%~G" (768e11ab.jpg) do "4ca5d042.jpg"
Also, tried to use some renaming application for this but they freeze when I try to rename big list of files, so I would avoid them and use batch file. Also, I would like to use this way where there is one line in batch file for every file because it is simpler for me to use it (and change it later). I appreciate your help.
Approach the problem from the other side. Instead of looping over the image files, loop over the text file.
Assuming your textfile to be something like
"768e11ab.jpg" "4ca5d042.jpg"
"5402c708.jpg" "b802820b.jpg"
"1c039e0e.jpg" "80ce9797.jpg"
Then the code could look like:
#echo off
REM read each line; %%A is the first column, %%B is the second one
for /f "tokens=1,2" %%A in (list.txt) do (
REM search the image file %%A
for /R "C:\Images\" %%C in ("%%~A") do (
REM %%C now holds the full path of %%A
ECHO ren "%%~C" "%%B~%%~xC"
)
)
If your list looks different, the tokens and perhaps the delims for the for /f loop have to be adapted.
NOTE: the ren command is just echoed for security reasons. Once you verified it does exactly what you want, remove the ECHO to enable the * ren` command.

Copy files matching the names in a list of paths and paste them in one new folder?

I'm working in a windows 7 machine and I'm trying to take all of the files matching the names in a list of file paths (I have the list saved as a csv, rda, and can make a txt file if needed). Ie: the list looks like:
Y:/iglgrelkgjkrle/originals/jsfhdjk.xls
Y:/iglgrelkgjkrddsle/ffhej/originals/jsfhdjk.xlsx
Y:/kssrldsse/ffhej/originals/jsfhdjk.xlt
Y:/blahblah/blah/blahhh/blahhhhhh/originals/blahahaha.pdf
...
...
And basically I want all of these files in this list copied to a new folder in a different location. Thanks!
Almost any problem can be solved with a FOR statement in windows command processor. Using for /f we can search a list in a text document and for each item (This case; location) specified, can run a command to copy it to a new location.
For copying the file, xcopy will be very handy as it has many copy option switches we can use such as /i /z /y.
/I - If in doubt always assume the destination is a folder
/Z - Copy files in restartable mode. If the copy is interrupted part way through,
it will restart if possible.
/Y - Suppress prompt to confirm overwriting a file. (Use /-Y for reverse)
In the following commands bellow, C:\list.txt is used as an example. This is where you specify the location of your list file. This can support a wide range of file formats including html. It does not hurt to try your extensions.
For the place to output the copied files - C:\CopyFolder is an example of the location of the folder you wish to send them too. You can also send them to a local server via \\server\folder\.
From command Line:
for /f "delims=" %i in (C:\list.txt) do (xcopy "%i" "C:\CopyFolder" /i /z /y)
From batch file:
for /f "delims=" %%i in (C:\list.txt) do (xcopy "%%i" "C:\CopyFolder" /i /z /y)
If this has solved your issue, please don't forget to mark this response as solved. I will be happy to further explain any questions!

Delete semi duplicate files

I'm wondering if there is a way to remove semi-duplicate files (name based) using a batch file or any other means (freeware utility) in Windows?
To give an example I have following files in a directory:
fileNameXXX(aaa).ext
fileNameXXX(bbb).ext
In this case, I only want to keep the fileNameXXX(bbb).ext
it's a single line in batch:
for /f "delims=" %%f in ('dir /b "*(*).ext" ^| find /v "(ddd)"') do ECHO del "%%f"
For every file matching the filemask excluding files with (ddd) do: delete it.
Remove the ECHO if the output fits your needs.
Note: if you want to use it directly on command line (instead in a batch file), replace every %%f with %f.
Tip: think about using some more code to check, if there is a Dutch version, and if not, keep the English one (or whatever you prefer).

Batch command to ignore specific files after dir command?

How do I set a batch script to ignore certain files? Like, not filter, but exact files.
Like
dir /b *.zip
Results in the file list
file1.zip
file2.zip
file3.zip
file4.zip
file5.zip
And I want to ignore the specific files
file3.zip
file4.zip
And end up with the file list result
file1.zip
file2.zip
file5.zip
How do I do this?
dir /b *.zip|findstr /v /i /g:"filecontainingexcludednamesonetoaline.txt"
should handle this task quite adequately.
On more information provided (best to provide relevant information at the start to prevent an endless revision cycle)
...
dir /b *.zip|findstr /x /v /i /g:"~f0"
....
goto :eof
---- this info at end-of-file. just plain text
---- files-to-exclude-from-listing
---- these last 3 lines are just comments playing no part in the script
exclude me.zip
and_me.zip
dont_show me.zip
Nearly the same as Magoo's answer, but without the extra file, and also with the addition of the /L option to force a literal match:
dir /b *.zip|findstr /vilx /c:"file1.zip" /c:"file2.zip"
This is a bit overkill for this problem, but you could also use my JREN.BAT utility. It is primarily intended for renaming files, but it does have an option to list files instead. The advantage of this utility is it allows you to specify very precise regular expression terms to match, as well as to specify files to exclude.
In this case, I use the /FM and /FX options to specify masks with normal Windows wildcard rules instead of using regular expressions.
jrepl "^" "" /list /fm *.zip /fx "file1.zip|file2.zip"
If you have complicated matching rules, then /RFM and /RFX could be used to specify regular expression masks instead.

Resources