cmd how to delete JPG file and keep only RAW - batch-file

I´m taking pictures with my mobile phone sometimes in JPG only, but sometimes in RAW. When shooting RAW, mobile phone actually stores two files (filename.jpg and filename.dng).
I would like to write a script which would search defined folder and delete all JPGs which have same filename like the DNGs (RAW).
Example - folder has following files:
IMG_20170625_105228.dng
IMG_20170625_105228.jpg
IMG_20170625_105326.jpg
IMG_20170625_105337.jpg
IMG_20170625_105350.dng
IMG_20170625_105350.jpg
Script should delete:
IMG_20170625_105228.jpg
IMG_20170625_105350.jpg

Iterate over the .dng files and if a like-named .jpg file exists, delete it. When you are satisfied that the correct files would be deleted, remove the ECHO from the DEL command.
PUSHD "C:\the\dir\containing\pics"
FOR /F "usebackq tokens=*" %%f IN (`DIR /B "*.dng"`) DO (
IF EXIST "%%~nf.jpg" (ECHO DEL "%%~nf.jpg")
)
POPD
If, for whatever reason, you wanted to do this in PowerShell, you could do something like this. When the correct files are being removed, remove the -WhatIf from the Remove-Item command. I would be interested to hear from anyone about a better way to do this in PowerShell.
$picdir = 'C:\dir\path\to\pics'
Get-ChildItem -Path $picdir -File -Filter '*.dng' |
Where-Object { Test-Path -Path "$($_.DirectoryName)/$($_.BaseName).jpg" -PathType Leaf } |
Select-Object #{Name="Path";Expression={"$($_.DirectoryName)\$($_.BaseName).jpg"}} |
Remove-Item -WhatIf

Related

batch-script to replace dots in foldernames with spaces

a user of mine is having problems with syncing cloud to local workstation. he is having lots of folders in a deep (10+) folder structure pattern and every folder is containing at least three dots ("xy.asdf.qwer $generic descriptor").
so now there is big foobar. online everything works fine, new files can be created and edited as expected. but the synced files keeps crashing on windows 10 enterprise and explorer warns with "bad foldername" and stuff. so i allowed long folder names (260 chars max.... seriously?) via GPO for user and i thought "well done. problem solved" ... but no... synced local files are still having trouble... so i allowed same option via regedit. same same. explorer keeps complaining.
so i decided to change folder names and remove the dots and replace them with spaces. so folder "xy.asdf.qwer $generic descriptor" will be "xy asdf qwer $generic descriptor". user says it's fine so i wrote a batch to rename folders. sry, my powershell is really bad. but if u know a solution in powershell it is appreciated and fine as well.
#echo off
setlocal enabledelayedexpansion
set "leer= "
set "punkt=."
call :rekursion
goto :eof
:rekursion
for /D %%d in (*) do (
set name=%%~nxd
ren "%%d" "!name:%punkt%=%leer%!"
cd %%d
call :rekursion
cd ..
)
exit /b
batch is working fine, but keeps throwing errors and telling me "not allowed" ... what is wrong with it? why is it working but complaining? what am i missing? all rights are set proper (full access...)
[sry for bad English...]
This PowerShell script will do what you want. If you are on a supported Windows platform, PowerShell will be available.
I would not want SPACE characters in directory or file names, but that is your choice. When you are satisfied that the names would be changed correctly, remove the -WhatIf from the Rename-Item command.
#Requires -Version 3
$BaseDir = 'C:\src\t'
Get-ChildItem -Recurse -Directory -Path $BaseDir |
Sort-Object -Descending -Property FullName |
Where-Object { $_.Name -match '\.' } |
ForEach-Object {
$NewName = $_.Name -replace '\.',' '
Rename-Item -Path $_.FullName -NewName $NewName -WhatIf
}
The order is the issue. You are renaming the folder BEFORE you try to change directories. It works because after you rename the folder, the loop will pick up the new folder name, but is much less efficient. I was seeing errors on the 'cd' line. This works for me and is a bit simpler:
#echo off
setlocal enabledelayedexpansion
set "leer= "
set "punkt=."
:rekursion
for /D %%d in (*) do (
pushd %%d
call :rekursion
popd
set name=%%~nxd
ren "%%d" "!name:%punkt%=%leer%!"
)

Remove nef files that haven't got a corresponding jpg file in a folder

I would like to write a script that searches through a folder and deletes any raw files (.nef) that don't have a matching jpg. For example,
Folder structure:
DSC_0001.nef
DSC_0001.jpg
DSC_0002.nef
DSC_0003.nef
The program would delete DSC_0002.nef and DSC_0003.nef.
How can I do this?
FOR /R %%F IN (*.NEF) DO #IF NOT EXIST "%%~dpnF.jpg" DEL "%%F"
Note: the /R switch means recursive (i.e. it will continue to run this command throughout the entire directory tree from the directory where it is run from). Omit that switch if you don't want to process subdirectories as well.
A possible PowerShell solution:
Get-ChildItem -Path "C:\temp\nef" -Filter "*.nef" | foreach {
if( -not (Get-Item $_.FullName.Replace('.nef','.jpg') -ErrorAction SilentlyContinue)){
$_ | Remove-Item -WhatIf
}
}
Remove the -WhatIf to actually make it delete the files.
You will need to use (for a single folder):
#echo off
cd to\folder
for %%A IN (*.nef) do (
if not exist %%~nA.jpg (del %%~fA)
)
And for multiple folders, use:
#echo off
for /R "full_path_to_parent_folder" %%A IN (*.nef) do (
if not exist %%~dpnA.jpg (del %%~fA)
)

batch to delete subfolders of all subfolders

So i'm currently trying to delete a bunch of subfolders that were created by a program, on a recurring basis because it will recreate the subfolders. I know how to schedule the batch file, right now my issue is how to efficiently create the batch file to begin with. To clarify, say i have a directory named D:\IGSG. There are 250 folders within D:\IGSG, and within each one of those is a folder named D:\IGSG\F252341\arg. I want to delete all of the \arg folders, from all ~250 F folders I have, without deleting the other files inside the F folders. Right now the only way to do it that I know of would be to have a batch that goes
cd D:\IGSG\F252341
del /Q arg
and to repeat those lines for every subfolder, but typing out those folder names for every folder in there would be tedious, especially considering new ones are created from time to time. So i'm looking for a way with a batch to delete the subfolder of a subfolder, without deleting other files, if that makes sense.
On the cmd line for /d /r X:\path in combination with a wildcard will enumerate all folders names arg the appended .? meets this requirement:
for /d /r D:\IGSG %A in (arg.?) do #echo rd /s /q "%A"
if the output looks right remove the echo.
In a batch file double the %-signs -> %%A
#Echo off
for /d /r D:\IGSG %%A in (arg.?) do echo rd /s /q "%%A"
One of the rare cases where batch/cmd line can compete with PowerShell.
This PowerShell script will recurse through the subdirectory structure and delete all files and subdirectories inside arg directories. Be sure to change the location of the IGSG directory to yours. When you are satisfied that the correct files will be deleted, remove the -WhatIf from the Remove-Item cmdlet.
Get-ChildItem -Directory -Recurse -Path 'C:\src\t\delsubs\IGSG\F*' -Filter 'arg' |
Remove-Item -Path {(Join-Path -Path $_.FullName -ChildPath '*')} -Recurse -Force -WhatIf
If you need to run it from a cmd.exe shell, put the code above into a file named 'dodel.ps1' and run using:
powershell -NoProfile -File .\dodel.ps1

Copying Multiple files with specific extension

So I have this little problem...
(Windows 10 Education N x64)
I am a GoldSrc engine developer and i am trying to achieve the
following thing with Batch script or PowerShell, to do that automated
task by itself instead of me manually picking every damn folder,
eventually to spare time. :)
I am having directory structure as following:
Main(root) folder names "GoldSrc" is located at "Desktop"
So it goes like this:
Absolute Path:
Image from TREE VIEW:
[Main folder]
C:\Users\Andrej Poženel\Desktop\GoldSrc
[Source directory to copy from with recursive subdirectories]
C:\Users\Andrej Poženel\Desktop\GoldSrc\prefabs
[Directory to copy to]
C:\Users\Andrej Poženel\Desktop\GoldSrc\Maps
I want to lookup into subfolder named "prefabs" and search all
subfolders in that directory for files that have file extension .map
AND .rmf, so both filters together and copy them from its source
location [../GoldSrc/prefabs/like_100_folders_here] to "GoldSrc"
subdirectory named "Maps", everything shown on picture)
So i want the things look like this after this process:
C:\Users\myusername\Desktop\GoldSrc\Maps: blabla.map bleble.rmf
bleble.rmf cacac.rmf adasdad.map ...
and not each file in its own directory like it is in source dir...
Thanks in advance
pushd "C:\Users\myusername\Desktop\GoldSrc"
for /f "delims=" %%a in ('dir /s /b /a-d .\prefabs\*.map .\prefabs\*.rmf') do ECHO copy "%%a" ".\maps\"
popd
Should execute this.
First move to the required subdirectory, then perform a directory scan including subdirectories in basic form and excluding directorynames. For each returned line matching either of the filespecs, assign the name found to %%a, then copy that file to the required subdirectory.
then return to the original directory.
The commands generated will simply be echoed to the console. To actually execute the command (after checking), remove the ECHO keyword.
Note that this is a batch file, and not intended to be executed directly from the prompt.
If you want to do it the Powershell way, you can use the below code
gci "C:\Users\Andrej Poženel\Desktop\GoldSrc\prefabs" -filter *.map | %{
copy-item -Path $_.FullName -Destination "C:\Users\Andrej Poženel\Desktop\GoldSrc\Maps"}
gci "C:\Users\Andrej Poženel\Desktop\GoldSrc\prefabs" -filter *.rmf | %{
copy-item -Path $_.FullName -Destination "C:\Users\Andrej Poženel\Desktop\GoldSrc\Maps"}
or
gci "C:\Users\Andrej Poženel\Desktop\GoldSrc\prefabs" -recurse | ?{
!$_.PsIsContainer -and $_.Extension -match "map|rmf" } | %{
copy-item -Path $_.FullName -Destination "C:\Users\Andrej Poženel\Desktop\GoldSrc\Maps"}

Move .txt files in different folders to a single folder using Batch file

I'm trying to move all .txt files in different folders to a single folder using a batch file, I'm new to batch coding so I'm having some difficulties.
My code is as follows:
FOR /D /r %%G IN ("C:\Users\Rodrigo\Desktop\PR\2016\08.2016\") DO MOVE G\*.txt C:\Users\Rodrigo\Desktop\PR\2016\
See the correct syntax of For /r or in an open cmd window type help for
#Echo off
For /r "C:\Users\Rodrigo\Desktop\PR\2016\08.2016\" %%G IN (*.txt
) Do echo Move "%%G" "C:\Users\Rodrigo\Desktop\PR\2016\"
Pause
If the output to screen looks OK remove the echo in front of the move command.
You could do this all in PowerShell. I am not sure it will work if you do not pass the $_.FullName which contains the path to the file.
Get-ChildItem -Path "C:\Users\Rodrigo\Desktop\PR\2016\08.2016\" -Filter *.txt | `
ForEach-Object { $_.FullName } | `
Move-Item -Destination "C:\Users\Rodrigo\Desktop\PR\2016\"

Resources