Delete semi duplicate files - file

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).

Related

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!

shift file names with a batch file

Here is the file name format. The leading number is the layer and the second number is the material (3D printer).
01118_7.tif,
01118_6.tif,
01118_5.tif,
01118_4.tif,
01118_3.tif,
01118_2.tif,
01118_1.tif,
01118_0.tif
What I need to do is shift the files ending in _1, _4, _6 six places higher. So, 01124_1, 01124_4, 01124_6 while the rest of the files stay the same. I need to do it all the way down to layer 00112_*.
I'd like to do this via a batch file if I can. Was trying to follow a guide but the name format is tripping me up.
Basic excel format
I can't tell if you need to modify file names that appear within a text file, or if you need to rename files. Either way, I have a simple solution using one of two hybrid JScript/batch regex utilities:
Modify filenames within a text file using JREPL.BAT:
jrepl "^\d{5}(?=_[146]\.tif)" "lpad(Number($0)+6,'00000')" /i /j /f test.txt /O -
Rename files within the current directory using JREN.BAT:
jren "^\d{5}(?=_[146]\.tif$)" "lpad(Number($0)+6,'00000')" /i /j
Use call jrepl or call jren if you put the command within a batch script.
It took me awhile to understand that "shift file names six places higher" really means "add six to file name".
#echo off
setlocal EnableDelayedExpansion
set "numbers=/1/4/6/"
for /F "tokens=1,2 delims=_." %%a in ('dir /B /A-D *.tif') do (
if "!numbers:/%%b/=!" neq "%numbers%" (
set "newNum=1%%a+6"
ECHO ren "%%a_%%b.tif" "!newNum:~1!_%%b.tif"
)
)
If the names are not in files, but are lines of text placed in a text file, change the 'dir /B /A-D *.tif' command by the name of the text file.

How to call a batch file from batch file

I need the syntax to call a batch file from the first batch file. The second batch files name changes with the revision. so i have only half of my second batch file name.
How do i search in a particular folder and call the second batch file..`?
Don't know the revision prefix or suffix, but you could try something like this:
for /f "tokens=1" %%n in ('dir /on /l /b /a-d "bat_file_*.bat"') do set latest_bat_file=%%n
It's relying on dir /on to sort by name, so it puts the last entry alphabetically in the variable %latest_bat_file%. You can then call it with:
call "%latest_bat_file%"
This assumes there are no spaces in your bat file names and that the revision is a numeric or alphabetical suffix. If you're using numbers, to avoid sorting problems, prefix your revision names with zero's (e.g. bat_file_001, bat_file_002).
FOR /R <path> will walk the directory tree for you.
FOR /R "%DIR_TO_SEARCH%" %%b IN (matching_*.bat) DO cmd /c "%%~b"
cmd /c will create a new shell instance, which means that if the invoked .BAT file sets environment variables, they won't be changed in calling script. This is usually what people want. If you actually wanted those side effects preserved, you could use call "%%~b" instead.

Batch file for opening most recent Malwarebytes' Anti-Malware Log

In Vista, I want to run a batch script to open the most recent file, based either on last modified date, or the date in the filename. It's for Malwarebytes' Anti-Malware logs in the %username&/appdata/roaming/Malwarebytes/Malwarebytes' Anti-Malware/Logs folder.
Log files are in this format here
mbam-log-2009-03-21 (00-20-21).txt
mbam-log-2009-03-21 (09-42-40).txt
mbam-log-2009-03-21 (11-02-43).txt
mbam-log-2009-03-21 (11-12-01).txt
mbam-log-2009-03-21 (12-01-42).txt
mbam-log-2009-03-21 (12-04-49).txt
mbam-log-2009-03-21 (14-01-41).txt
So its 24-hr format. I read on another page on here, and got this script here..
#echo off
dir *.txt /b /on > systext.bak
FOR /F %%i in (systext.bak) do set sysRunCommand=%%i
call %sysRunCommand%
del systext.bak /y
but it doesn't like the space in the filename.. always get an error.
Anyone have any ideas?
You actually have three problems in the script. The first is that %%i is likely to be set to the words with a filename- this can be fixed by using "delims=" in the for statement.
The second is that you need to quote spacey filenames in you call statement.
The third is that I'm not aware of a /y option for del, perhaps you mean del /f.
Amyway, give this one a shot:
#echo off
dir *.txt /b /on > systext.bak
FOR /F "delims=" %%i in (systext.bak) do set sysRunCommand=%%i
call "%sysRunCommand%"
del /f systext.bak
The following one-line batch file will open the most recently modified file:
for /f "usebackq delims=" %%i in (`dir /b /o-d`) do #start "%%i"&goto :eof
Using dir strikes me as much simpler than trying to dissect the date in the filename. You can also order by filename (as the date format is somewhat ISO-8601-ish is sorts well).
The goto :eof is just there to make sure only the most recent file will be opened and not all in order of date/time.
As for your space problem, surrounding the file name with quotes usually should fix that, but sometimes it's a little difficult to know where they have to be. Also, for by default tokenizes its input at spaces, that's why I included delims= in there which essentially says »Put everything into the variable and don't do any tokenizing«.
I know this is an old post, and I don't mean to revive it - I have an alternative approach to the question.
Malwarebytes' Anti-Malware includes the ability to reroute the log file to an alternate location (folder or specified file).
You can run "%programfiles%\malwarebytes' anti-malware\mbam.exe" /logtofile c:\logs\bob-mbam-log.txt. /logtofolder also works. (the above command assumes you're using a 32 bit system.)
When specifying a file, mbam appends instead of overwriting. So, that may make the log a little more difficult to parse.

Resources