I have a folder with multiple photos in it with two different resolutions for each photo. I want to delete the photos with lower resolution. For example the folder contains the following files WP_20140917_19_15_04_Pro.jpg and WP_20140917_19_15_04_Pro__highres.jpg. I want to keep all the photos with the highres in the filename and delete the other one with use of CMD.
One important note is that it should only delete the photo if a highres version of the photo exists, because the folder does not always contains highres versions of the photos.
I know how to delete files when a certain word is repeatedly returned (such as highres), but I can't do the opposite, that is why I don't know how to start. All help is appreciated.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
set /a count=0
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*__highres.*" '
) DO (
SET "name=%%a"
SET "name=!name:__highres=!"
IF EXIST "%sourcedir%\!name!" (
ECHO(DEL "%sourcedir%\!name!"
set /a count+=1
)
)
echo %count% files deleted.
GOTO :EOF
You would need to change the setting of sourcedir to suit your circumstances.
The required DEL commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(DEL to DEL to actually delete the files.
Uber-simple. Read the directory, targeting all files matching the pattern *__highres.*. Remove the __highres from the name and if a file exists with the modified name, delete it.
---edit : added-in count of deleted files.
Related
There are two folders holding equal amounts of files between them.
I'd like to apply the names from one set ; to the other set of files in no particular order. Inherit the name, but retain the extension .
Input files are .bmp Output files are .ini ( has a gear symbol ).
Example :
folder 1- Flowers.bmp ; folder 2- blank.ini
. To this:
folder 1- Flowers.bmp ; folder 2- Flowers.ini
There would be more files , but equal .
The .ini files are all copies . So they may have a generic name and numbered if that matters to know . Those would all receive one name each from the other .bmp files in the other folder.
Normally I have both folders situated on the Desktop .
I make sure both folders have equal number of files between them . That would be a constant .
I'm trying to stream line some menial repetitive daily tasks .
I did search and what I have found does not really help.
#ECHO OFF
SET "vers=%~1"
IF "%vers%" == "" SET /P "vers=Enter Vers: "
FOR %%F IN (file_XX_*.*) DO CALL :process "%%F"
GOTO :EOF
:process
SET "name=%~nx1"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "name=!name:_XX_=_%vers%_!"
RENAME %1 "%name%"
ENDLOCAL
Hoping to find a solution to this finally .
Ok, here is a slightly long version, but it makes sure it gets the content of each folder.
Note You must ensure that the path to both folders are correctly specified in the third and fourth line:
#echo off
setlocal enabledelayedexpansion
set "source=%userprofile%\Desktop\folder 1"
set "destination=%userprofile%\Desktop\folder 2"
set /a num=0
for %%a in ("%source%\*") do (
set /a num+=1
set "fr!num!m=%%~na"
)
set /a oldn=!num!
set /a num=0
for %%b in ("%destination%\*") do (
set /a num+=1
set "to!num!r!=%%~nxb"
set "ext=%%~xb"
)
pushd "%destination%"
for /l %%i in (1,1,!oldn!) do ren "!to%%ir!" "!fr%%im!%ext%"
popd
pause
You can remove pause at the bottom of the script, once you are happy with the output.
This is not the most effective way, but considering your limited batch experience, I guess an understandable approach is better than an optimized one (using array-like variables).
#echo off
setlocal enabledelayedexpansion
(for %%A in ("folder 1\*.bmp") do echo %%~nA)> "%temp%\names.txt"
<"%temp%\names.txt" (
for %%A in ("folder 2\*.ini") do (
set /p "name="
ECHO ren "%%A" "!name!.ini"
))
The first for loop creates a list of the desired names (from Folder 1). The modifier %%~nA returns the name only.
The second for processes each file in Folder 2 and takes a name from the previously generated file for each file. This depends on the fact that the number of files in both folders are the same, else you may get undesired results.
I disabled the actual ren command by just echoing it. Check the output and only when you are satisfied, remove the ECHO.
I have multiple text files in a folder that I am searching the content for and renaming the found files but having a problem. When I run the batch it renames all the found files to the same filename. So if it finds 3 files with the string in them it renames all of them to the same file. Still kinda new to this stuff. I am trying to find the string "test1" inside all the text files and then rename the files found to test1.txt, test1 (2).txt, etc.
#ECHO OFF
#SETLOCAL enableextensions disabledelayedexpansion
SET "sourcedir=C:\test"
SET "searchfor=test1"
FOR /f "delims=" %%a IN ('findstr /m /L /c:"%searchfor%" "%sourcedir%\*.txt"') DO (
REN "%%a" "%searchfor%.txt"
)
change
REN "%%a" "%searchfor%.txt"
to
if exist "%searchfor%.txt" (
set "renamed="
for /L %%r in (1,1,100) do if not defined renamed if not exist "%searchfor% (%%r).txt" REN "%%a" "%searchfor% (%%r).txt"&set "renamed=%%r"
) else (
REN "%%a" "%searchfor%.txt"
)
which will for each filename-to-be-changed first detect whether %searchfor%.txt exists, and rename the target file if not. If %searchfor%.txt exists, then set the renamed flag to nothing and loop for %%r = 1 to 100 by steps of 1. If the filename %searchfor% (%%r).txt does not exist, then do the rename and set renamed to (well - any value other than nothing will do - %%r is convenient). Since renamed is now defined, if not defined will be false, so no further rename attempts will be made for that target filename.
(untested)
Note - the syntax is important. Don't try to break the statements over many lines or alter the positions of the parentheses. Just cut-and-paste.
Tip:
Instead of ren use echo ren which will simply REPORT the proposed rename. Obviously, since the file won't actually be renamed, the procedure will report the same new name. Then manually create %searchfor%.txt and try again. Then manually create %searchfor% (1).txt and try yet again. You'll see the proposed new name change. After testing in this manner, change each echo ren to ren and all should proceed smoothly.
(perform tests on a copy with a small number of "hits" to avoid confusion)
Found a pyhton solution here, but I need a batch file-based solution.
Have many files:
SSP4325_blah-blah-blah.xml
JKP7645_blah.xml
YTG6457-blah-blah.xml
And folder names that contain a piece of the file name:
RefID - SSP4325, JKP7645, GHT1278, YRR0023
RefID - YTG6457
I'm looking for a batch solution which would read a portion of the file name at the front (before either the first dash or underscore) and then move that file into the folder where the front of the filename exists as part of the folder name.
So in the above examples, the first two files (SSP4325 and JKP7645) were moved into the first folder because it contained it contained that text as part of the folder name.
The third file would be moved into the second folder.
I have hundreds of files and 63 folders. So I'm hoping to be able to automate.
Can't use Powershell or Python due to limitations of the environment. So hoping for a batch file approach.
Thanks. Sean.
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\*.xml" '
) DO (
FOR /f "tokens=1delims=_-" %%b IN ("%%a") DO (
FOR /f "delims=" %%d IN (
'dir /b /ad "%destdir%\*%%b*" '
) DO (
ECHO(MOVE "%%a" "%destdir%\%%d\"
)
)
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
After establishing the directories, the outer loop puts the filename in %%a, the next loop gets the first part of that name, up to but not including the first - or _ (the delims specified) into %%b.
The inner loop finds the target directory containng %%b in the destination directory and constructs an appropriate move line.
This solution review the folders just one time and store they in an array, so this method should run faster.
#echo off
setlocal EnableDelayedExpansion
rem Process the folders
set i=0
for /D %%a in (*) do (
rem Store this folder in the next array element
set /A i+=1
set "folder[!i!]=%%a"
rem Separate folder in parts and store the number of the array element in each one
for %%b in (%%a) do set "part[%%b]=!i!"
)
rem Process the files
for %%a in (*.xml) do (
rem Get the first part of name
for /F "delims=-_" %%b in ("%%a") do (
rem If such a folder exists...
if defined part[%%b] (
rem Get the number of the corresponding array element and move the file
for %%n in (!part[%%b]!) do ECHO move "%%a" "!folder[%%n]!"
) else (
echo No folder exists for this file: "%%a"
)
)
)
This method have also several advantages: you may check if a certain folder does not exists, or get the number of files moved to each folder, etc. If you are not interested in these points, just remove the if command and make the code simpler...
An explanation of array management in Batch files is given at this answer.
I have a folder that gets a new file added everyday to the folder with the same file name but incremental extension such as .001, .002, .003, etc. However, if there's no file within the folder it starts at .001 again.
The problem is they are all named the same and if I move them to another folder to archive them it would just overwrite the same file over and over again. I could create a folder each day with the date with only one file in it, but that seems a bit redundant.
Is there a way to look at the create date of each file and rename it to the create date?
I've gotten this far, but it looks like for this situation I have to use a static file name, how to loop through the entire directory?
SET filename = C:\test.001
FOR %%f IN (%filename%) DO SET filedatetime=%%~tf
rename c:\test.001 C:\test_%filedatetime%.txt
move C:\*.txt C:\archive\
this provides the correct sort order:
#echo off &setlocal disableDelayedExpansion
set "startfolder=%userprofile%\test"
cd /d "%startfolder%"
for %%a in (*) do (
for /f "delims=." %%b in ('wmic datafile where "name='%startfolder:\=\\%\\%%~a'" get lastmodified^|find "."') do (
echo(ren "%startfolder%\%%~a" "%%~b.txt"
)
)
Remove echo to get it working.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "targetdir=c:\sourcedir"
SET "destdir=c:\destdir"
PUSHD "%targetdir%"
FOR %%a IN (*.*) DO (
SET "timestamp=%%~ta"
SET "timestamp=!timestamp:/=_!
SET "timestamp=!timestamp::=_!
SET "timestamp=!timestamp:.=_!
SET "timestamp=!timestamp:,=_!
SET "timestamp=!timestamp: =_!
ECHO MOVE "%%a" "%destdir%\%%~na.!timestamp!"
)
GOTO :EOF
This should work with any file in the nominated target directory where the name does not include ! or ^.
The required MOVE commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files. Append >nul to suppress report messages (eg. 1 file moved)
The gymnastics around timestamp are intende to replace /: with _ since these are illegal filename characters. Space., are similarly replaced - they're legal but often painful.
If you want the destination filename to be name.003.timestamp, remove the ~na from the destination name.
Try like this :
SET $path=The_path_who_contain_the_FILES
FOR /F "DELIMS=" %%f IN ('dir "%$path%" /a-d/b') DO (
SET filedatetime=%%~tf
move "%%~dpnxf" "C:\archive\test_%filedatetime%.txt")
In most modern systems, the system will automatically create extra files that are unnecessary. How can I create a .bat file that makes use of a checklist to delete any file that isn't on it?
This would be useful for removing automatically generated backup files, plot logs, error logs, etc. in the customization files I have created.
Try this, make sure to set SEARCHDIR to the real dir, and populate keep.txt with the files you want to keep, 1 file per line, use the full path including the dir. If you don't want to use the dir, adjust the DIR /S /B command so it outputs the same format as what is in your keep.txt
#echo off
setlocal enabledelayedexpansion
set KEEPFILES=keep.txt
set SEARCHDIR=.\test_folder
SET /a KEEPTHISFILE=2
FOR /f "tokens=*" %%A IN ( 'DIR /S /B %SEARCHDIR%\*' ) DO (
SET /a KEEPTHISFILE=0
for /f "tokens=1,* delims=ΒΆ" %%B in ( '"type %KEEPFILES%"') do (
if "%%A"=="%%B" SET /a KEEPTHISFILE=1
)
if "!KEEPTHISFILE!"=="0" (
echo "deleting %%A"
del /f %%A
)
)
Make a file called 'deletestuff.bat' (or whatever you want it to be called, as long as it is a .bat). Next, fill it with:
rm *.tmp
rm *.blah
Where .tmp, and .blah are the extensions of the files that you would like to delete (they might be .log, for instance). Test it first by copying a sample of files into a new folder and running the new .bat there, just to make sure it doesn't delete important things.