forfiles modified files excluding newly created - batch-file

I am trying to write code that will allow me to identify files in a folder that have been modified since the beginning of the month, but that are not newly created (signifying a revision change). Outputting the number of changed files is more important than the name of the modified files, but it can be either.
I have tried to use forfiles to achieve this, but am unable to figure out how to do this without it also listing newly created files.
I have thought that maybe searching for files created before the beginning of the month yet modified within the last month might be the answer, but am unsure how to do this. My code so far still shows newly created files.
#echo off
REM set revisions folder path
set revisions="C:\Users\nlehman\Desktop\New folder (3)\\"
forfiles /P %revisions% /S /D +08/01/2019 > "Revisions last 30days.txt"

I will make a SWAG and think that you want to identify files written in the current month, but were created earlier than the last time they were written. Is that close?
Place all four (4) files in the same directory. This assumes that if the difference between the creation time and last write time is less than one (1) second, that there has not been a revision.
=== revs.bat
#powershell -NoLogo -NoProfile -File "%~dp0revs.ps1"
=== revs.ps1
$RevDir = 'C:\Users\nlehman\Desktop\New folder (3)'
$CloseEnough = New-Timespan -Seconds 1
Get-ChildItem -File -Recurse -Path $RevDir |
Where-Object {
($_.LastWriteTime -ge (Get-Date (Get-Date) -day 1 -hour 0 -minute 0 -second 0)) -and
(($_.LastWriteTime - $_.CreationTime) -gt $CloseEnough)
}
=== revsnot.bat
#powershell -NoLogo -NoProfile -File "%~dp0revsnot.ps1"
=== revsnot.ps1
$RevDir = 'C:\Users\nlehman\Desktop\New folder (3)'
$CloseEnough = New-Timespan -Seconds 1
Get-ChildItem -File -Recurse -Path $RevDir |
Where-object {
($_.LastWriteTime -ge (Get-Date (Get-Date) -day 1 -hour 0 -minute 0 -second 0)) -and
(($_.LastWriteTime - $_.CreationTime) -lt $CloseEnough)
}

This is going to hurt the eyes of everyone who reads it as it is quite possibly the absolute worst coding on the history of this site, but it works for its purpose, and is the best I can currently do. I am still learning this stuff, and although this does work, I really would have liked to know how to put my results into variables so I did not have to do it the way I did with .txt files...
#echo off
setlocal enabledelayedexpansion
set revisions="C:\Users\nlehman\Desktop\New folder (3)\\"
dir %revisions% /T:C /A-D /O:-D > %revisions%"creation date.txt"
dir %revisions% /T:W /A:-D /O:-D > %revisions%"modified date.txt"
findstr /vig:"C:\Users\nlehman\Desktop\New folder (3)\creation date.txt" "C:\Users\nlehman\Desktop\New folder (3)\modified date.txt" > "C:\Users\nlehman\Desktop\New folder (3)\difference.txt"
findstr /b "08/" difference.txt > "rev changes.txt"
find /v /c "" "rev changes.txt" > revs.txt
del %revisions%"creation date.txt"
del %revisions%"modified date.txt"
del %revisions%"difference.txt"
type "rev changes.txt" "revs.txt" > "revisions.txt"
del %revisions%"rev changes.txt"
del %revisions%"revs.txt"

Related

Batch file that will delete files that are older than 6 months, but will exclude files listed in a separate text file

Just wanted to ask for help in batch file. I need to have a text file where the file names of excluded files would be stored in the deletion of files with 6 months above date.
Inside list.txt are trial.csv, trial2.csv which are older than 6 months but should be excluded in the deletion since it is in my list.
I have this code but can't seem to make it work. Please help
#echo off
set exclude_file=C:\Users\Desktop\Test\list.txt
set directory=C:\Users\Desktop\Test
forfiles /p %directory% /s /m *.* /c "cmd /c if #fdate < %date% -180 and not #isdir == true (find /i ""#fname"" < %exclude_file% >nul || del /q #path)"
pause
If you are okay with using Powershell, try this. Would recommend using a -whatif on the remove-item line for testing. Use at your own risk...
$protectedFiles = Get-Content -Path "C:\Users\Desktop\Test\list.txt" | Where {$_}
$oldFiles = Get-ChildItem -Path "C:\Users\Desktop\Test" -File -Recurse -Force | Where-Object { !$_.PSIsContainer -and $_.LastWriteTime -lt (Get-Date.AddMonths(-6)}
Foreach($oldFile in $oldFiles){
If ($protectedFiles -NotContains $($_.Name)){Remove-Item $_ -Force}
}

batch script to rename the most recent file in all subfolders?

#echo off
for /f %%i in ('dir /b /a-d /od /t:c "*.pdf"') do (
set LastFileCreated=%%i
)
echo The Last File Created is %LastFileCreated%
pause
:: ren %LastFileCreated% "decs365.pdf"
pause
all I have is this script, but unfortunately it needs to be copied in each and every sub folder, thus defeating the purpose, and it wont even rename the files. Is there any way to add a specified path "D:\Monthly Report" and have it rename the most recently created file in all of the sub folders to "decs365"
I'd suggest you leverage powershell.exe, from your batch-file for this task:
#%__AppDir__%WindowsPowerShell\v1.0\powershell.exe -NoProfile "Get-ChildItem -Filter '*.pdf' -File -Recurse | Sort-Object CreationTime -Descending | Select-Object -First 1 | Rename-Item -NewName 'Decs365.pdf'"
Obviously, if you're not running the command in the same working directory, as you are in your question, you'd need to add that directroy to the command above, e.g. Get-ChildItem -Path 'C:\MyDirectoryPath' -Filter …

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%!"
)

cmd how to delete JPG file and keep only RAW

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

How to find files NOT containing a defined text and output their names into a file?

I am having an application folder with sub-folders and thousands of files in it. I want to write a batch script which lists all the files which DO NOT contain particular text, say SAMPLE_TEXT and redirect output to a file. Please help with the script.
Inspired by http://tobint.com/blog/powershell-selecting-files-that-dont-contain-specified-content/ this powershell worked well for me;
Get-ChildItem -include *.sql -recurse | ForEach-Object { if( !( select-string -pattern "USE " -path $_.FullName) ) { $_.FullName}} > FilesMissingUse.txt
In my case I was searching for database scripts (.sql files) which were missing "USE " string.
This may help you - launch it in the top level folder.
#echo off
(for /r %%a in (*) do find "SAMPLE_TEXT" "%%a" >nul || echo %%a)>file.log
#echo off
findstr /S /M /V "SAMPLE_TEXT" *.* > output.txt
With grep you can use
grep -L Font *.pdf > list_of_files.txt
The -L switch returns only files that do not contain the string "Font."

Resources