Reputation: 23
I am looking for some help creating a BAT file to help delete videos and images over a certain filename length.
A bit of background:
We have a custom made application and we do not have the source code. it updates its videos from a feed. recently we have been getting "buffer overrun detected" errors. we believe that the file names longer than 90 characters are becoming an issue.
I would like to remove these WMV and JPG files before they become a problem. We usually use bat files to perform these small tasks.
Upvotes: 1
Views: 213
Reputation: 67216
I think this is the simplest way to do this:
@echo off
setlocal EnableDelayedExpansion
for %%a in (*.WMV *.JPG) do (
set "filename=%%a"
if "!filename:~90!" neq "" (
ECHO del "%%a"
)
)
Upvotes: 1
Reputation: 57252
without external tools (not tested):
@echo off
rem:::: set the correct directory
set "rootdir=c:\somewhere"
pushd "%rootdir%"
setlocal enableDelayedExpansion
for %%# in (*.WMV *.JPG) do (
call ::strlen0 "%%~nx#" len
if !len! GEQ 90 (
rem:::: remove the echo if the correct files are listed
echo del /q /f "%%~nx#"
)
)
endlocal
exit /b 0
:strlen0 StrVar [RtnVar]
setlocal EnableDelayedExpansion
set "s=#!%~1!"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "len+=%%N"
set "s=!s:~%%N!"
)
)
endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b
Pay attention to the comments - these are the places you need to alter your script.
Upvotes: 0
Reputation: 63912
Don't stick to something OS -specific, use scripting language that would run on any platform, e.g. Node.js
var fs = require("fs");
var folderName = __dirname; // for current folder
var maxlength = 90;
function getFiles(dir, files_) {
files_ = files_ || [];
var files = fs.readdirSync(dir);
for ( var i in files) {
var name = dir + '/' + files[i];
if (fs.statSync(name).isDirectory()) {
getFiles(name, files_);
} else {
files_.push(name);
}
}
return files_;
}
var files = getFiles(folderName);
console.log(files);
for (var i = 0; i < files.length; i++) {
filename = files[i];
if (filename.length > maxlength) {
console.log("Deleting " + filename);
fs.unlinkSync(filename);
}
}
Upvotes: 0