Get image file dimensions in .bat file - batch-file

I have a bat file that lists the paths of all images in a folder the code is
#echo off
break > infofile.txt
for /f "delims=" %%F in ('dir /b /s *.bmp') do (
echo %%F 1 1 1 100 100 >>infofile.txt
)
The text file looks like this
C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(1).bmp 1 1 1 100 100
C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(348).bmp 1 1 1 100 100
C:\Users\Charles\Dropbox\trainer\temp\positive\rawdata\diags(353).bmp 1 1 1 100 100
What I want to do is replace the 100 100 at the end by the dimentions of each image width and height.. Thanks in advance.

you can use MediaInfo:
#ECHO OFF &SETLOCAL
(for /r %%a in (*.jpg *.bmp *.png) do (
set "width="
set "height="
for /f "tokens=1*delims=:" %%b in ('"MEDIAINFO --INFORM=Image;%%Width%%:%%Height%% "%%~a""') do (
echo(%%~a 1 1 1 %%~b %%~c
)
))>infofile.txt
type infofile.txt
output example:
C:\Users\Private\Pictures\snap001.png 1 1 1 528 384
C:\Users\Private\Pictures\snap002.png 1 1 1 1920 1080
C:\Users\Private\Pictures\snap003.png 1 1 1 617 316
C:\Users\Private\Pictures\snap004.png 1 1 1 1920 1080
C:\Users\Private\Pictures\snap005.png 1 1 1 514 346
C:\Users\Private\Pictures\snap006.png 1 1 1 1920 1080
C:\Users\Private\Pictures\snap007.png 1 1 1 395 429
C:\Users\Private\Pictures\snap008.png 1 1 1 768 566
C:\Users\Private\Pictures\snap009.png 1 1 1 1536 1080
C:\Users\Private\Pictures\snap010.png 1 1 1 1600 480

Here's a tooltipInfo.bat (jscript\bat hybrid that can be used as a .bat) that takes the tooptip information for a file and does not require any external software:
#if (#X)==(#Y) #end /* JScript comment
#echo off
rem :: the first argument is the script name as it will be used for proper help message
cscript //E:JScript //nologo "%~f0" %*
exit /b %errorlevel%
#if (#X)==(#Y) #end JScript comment */
//////
FSOObj = new ActiveXObject("Scripting.FileSystemObject");
var ARGS = WScript.Arguments;
if (ARGS.Length < 1 ) {
WScript.Echo("No file passed");
WScript.Quit(1);
}
var filename=ARGS.Item(0);
var objShell=new ActiveXObject("Shell.Application");
/////
//fso
ExistsItem = function (path) {
return FSOObj.FolderExists(path)||FSOObj.FileExists(path);
}
getFullPath = function (path) {
return FSOObj.GetAbsolutePathName(path);
}
//
//paths
getParent = function(path){
var splitted=path.split("\\");
var result="";
for (var s=0;s<splitted.length-1;s++){
if (s==0) {
result=splitted[s];
} else {
result=result+"\\"+splitted[s];
}
}
return result;
}
getName = function(path){
var splitted=path.split("\\");
return splitted[splitted.length-1];
}
//
function main(){
if (!ExistsItem(filename)) {
WScript.Echo(filename + " does not exist");
WScript.Quit(2);
}
var fullFilename=getFullPath(filename);
var namespace=getParent(fullFilename);
var name=getName(fullFilename);
var objFolder=objShell.NameSpace(namespace);
var objItem=objFolder.ParseName(name);
//https://msdn.microsoft.com/en-us/library/windows/desktop/bb787870(v=vs.85).aspx
WScript.Echo(fullFilename + " : ");
WScript.Echo(objFolder.GetDetailsOf(objItem,-1));
}
main();
Output if used against a picture:
C:\TEST.PNG :
Item type: PNG image
Dimensions: ?871 x 836?
Size: 63.8 KB
so you can:
for /f "delims=? tokens=2" %%a in ('toolTipInfo.bat C:\TEST.PNG ^|find "Dimensions:"') do echo %%a
EDIT:
Another way with WIA.ImageFile object - imgInfo.bat

I'm not sure whether you will be able to get at file properties like that in a batch script. I would recommend using something like Python. Here is a link to another thread which suggests using the PIL.imaging library for this.
If you are interested in perusing this route but do not know any Python let me know and I can put a quick script together for this.
Instructions to install Python
As discussed you will need to install Python for this to run. I have also found out that PIL is a third party library, so you will also need to download and install this (make sure you pick the same version as your python installation e.g. if you have installed Python 2.7 on 64 bit, you would need "Pillow-2.1.0.win-amd64-py2.7.exe" from here).
Once you have done the install you can check that this is working by opening the command prompt (cmd) and entering c:\python27\python.exe (if you add c:\python27 top your PATH environment variable you will just need to type "Python"). This will open the python command prompt. Type print "test" and you should see thr output printed then exit().
Once Python is installed you can create a script. Here is some code that will do what you have requested (list all files of a given extension that are found from a base path with 1 1 1 width height to a file).
Open a text editor e.g. notepad paste in the code below and save as "image_attr.py" or whatever name you decide to use:
from PIL import Image
import os, sys
def main():
# if a cmd line arg has been passed in use as base path...
if len(sys.argv) > 1:
base_path = sys.argv[1]
# else use current working dir...
else:
base_path = os.getcwd()
# image file extensions to be included, add or remove as required...
ext_list = ['.bmp', '.jpg']
# open output file...
outfile = os.path.join(base_path,'infofile.txt')
file_obj = open(outfile, 'wb')
# walk directory structure...
for root, dirs, files in os.walk(base_path):
for f in files:
# check of file extension is in list specified above...
if os.path.splitext(f)[1].lower() in ext_list:
f_path = os.path.join(root, f)
width, height = Image.open(f_path).size
output = f_path + ' 1 1 1 ' + str(width) + ' ' + str(height) +'\r\n'
file_obj.write(output)
file_obj.close()
if __name__ == '__main__':
main()
Save this and remember the path to the file, I will use c:\python27\image_attr.py for this example. You can then call this from cmd or from a batch script passing in an arguement for the base path e.g.:
python c:\python27\image_attr.py E:\Users\Prosserc\Pictures
Please note that any arguements with spaces in them should be enclosed with double quotes.
Please let me know if you have any questions.
EDIT
For Python 3 the amendments should be minimal in theory. In this case I am writing the output the the screen rather than a file, but redirecting to a file from cmd:
from PIL import Image
import os, sys
def main():
# if a cmd line arg has been passed in use as base path...
if len(sys.argv) > 1:
base_path = sys.argv[1]
# else use current working dir...
else:
base_path = os.getcwd()
# image file extensions to be included, add or remove as required...
ext_list = ['.bmp', '.jpg']
# walk directory structure
for root, dirs, files in os.walk(base_path):
for f in files:
# check of file extension is in list specified above...
if os.path.splitext(f)[1].lower() in ext_list:
f_path = os.path.join(root, f)
width, height = Image.open(f_path).size
output = f_path + ' 1 1 1 ' + str(width) + ' ' + str(height) +'\r\n'
print(output)
if __name__ == '__main__':
main()
Call with:
python c:\python27\image_attr.py E:\Users\Prosserc\Pictures > infofile.txt

The code below is based on tooltipInfo.bat by npocmaka except that I used ExtendedProperty() instead of GetDetailsOf().
#if (#X==#Y) #then
:: Batch
#echo off & setLocal enableExtensions disableDelayedExpansion
(call;) %= sets errorLevel to 0 =%
(
for /f "tokens=1,2 delims=x " %%X in ('
cscript //E:JScript //nologo "%~dpf0" "%~dpf1" %2
') do (set "width=%%X" & set "height=%%Y") %= for /f =%
) || goto end %= cond exec =%
echo("%~nx1": width=%width% height=%height%
:end - exit program with appropriate errorLevel
endLocal & goto :EOF
#end // JScript
// objects
var FSOObj = WScript.CreateObject("Scripting.FileSystemObject"),
objShell = WScript.CreateObject("Shell.Application");
var ARGS = WScript.Arguments;
if (ARGS.length != 1) {
WScript.StdErr.WriteLine("too many arguments");
WScript.Quit(1);
} else if (ARGS.Item(0) == "") {
WScript.StdErr.WriteLine("filename expected");
WScript.Quit(1);
} // if
ExistsItem = function (path) {
return FSOObj.FolderExists(path) || FSOObj.FileExists(path);
} // ExistsItem
getFullPath = function (path) {
return FSOObj.GetAbsolutePathName(path);
} // getFullPath
getParent = function(path) {
var splitted = path.split("\\"), result = "";
for (var s=0; s<splitted.length-1; s++) {
if (s == 0) {
result = splitted[s];
} else {
result = result + "\\" + splitted[s];
} // if
} // for
return result;
} // getParent
getName = function(path) {
var splitted = path.split("\\");
return splitted[splitted.length-1];
} // getName
var filename = ARGS.Item(0),
shortFilename = filename.replace(/^.+\\/, '');
if (!ExistsItem(filename)) {
WScript.StdErr.WriteLine('"' + shortFilename + '" does not exist');
WScript.Quit(1);
} // if
var fullFilename=getFullPath(filename), namespace=getParent(fullFilename),
name=getName(fullFilename), objFolder=objShell.NameSpace(namespace),
objItem;
if (objFolder != null) {
objItem=objFolder.ParseName(name);
if (objItem.ExtendedProperty("Dimensions") != null) {
WScript.Echo(objItem.ExtendedProperty("Dimensions").slice(1, -1));
} else {
WScript.StdErr.WriteLine('"' + shortFilename +
'" is not an image file');
WScript.Quit(1);
} // if 2
} // if 1
WScript.Quit(0);

This can be done with PowerShell with the Wia.ImageFile
break>infofile.txt
$image = New-Object -ComObject Wia.ImageFile
dir . -recurse -include *.jpg, *.gif, *.png, *.bmp | foreach{
$fname =$_.FullName
$image.LoadFile($fname)
echo ($fname -replace "\\","/" 1 1 1 $image.Width $image.Height)>>infofile.txt
}
The benifit is most windows computers have powershell instaled
It seams slower than CMD/batch scripts
That said CMD/batch scripts can't do this as far as I know.

Install imagemagick, then use the following inside a batch file:
FOR /F "tokens=* USEBACKQ" %%F IN (`magick identify -format "%%wx%%h" %1`) DO (SET dimensions=%%F)
#ECHO result: %dimensions%

Related

Getting a Windows command prompt contents to a text file

I want to write a batch utility to copy the output of a command prompt window to a file. I run my command prompt windows with the maximum depth of 9999 lines, and occasionally I want to grab the output of a command whose output is off-screen. I can do this manually with the keys Ctrl-A, Ctrl-Cand then pasting the result into Notepad - I just want to automate it in a batch file with a call to:
SaveScreen <text file name>
I know I can do it with redirection, but that would involve knowing that I will need to save the output of a batch command sequence beforehand.
So if I had a batch script:
call BuildPhase1.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
call BuildPhase2.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
call BuildPhase3.bat
if "%ErrorLevel% gtr 0 goto :ErrorExit
I could write:
cls
call BuildPhase1.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase1.err & goto :ErrorExit
call BuildPhase2.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase2.err & goto :ErrorExit
call BuildPhase3.bat
if "%ErrorLevel% gtr 0 call SaveScreen.bat BuildPhase3.err & goto :ErrorExit
or I could just type SaveScreen batch.log when I see that a run has failed.
My experiments have got me this far:
<!-- : Begin batch script
#cscript //nologo "%~f0?.wsf" //job:JS
#exit /b
----- Begin wsf script --->
<package>
<job id="JS">
<script language="JScript">
var oShell = WScript.CreateObject("WScript.Shell");
oShell.SendKeys ("hi folks{Enter}") ;
oShell.SendKeys ("^A") ; // Ctrl-A (select all)
oShell.SendKeys ("^C") ; // Ctrl-C (copy)
oShell.SendKeys ("% ES") ; // Alt-space, E, S (select all via menu)
oShell.SendKeys ("% EY") ; // Alt-space, E, Y (copy via menu)
// ... invoke a notepad session, paste the clipboard into it, save to a file
WScript.Quit () ;
</script>
</job>
</package>
My keystrokes are making it to the command prompt so presumably I have the correct window focused - it just seems to be ignoring the Ctrl and Alt modifiers. It also recognises Ctrl-C but not Ctrl-A. Because it has ignored the Ctrl-A to select all the text, the Ctrl-C causes the batch file to think it has seen a break command.
I've seen the other answers like this one but they all deal with methods using redirection, rather than a way of doing it after the fact "on demand".
* UPDATE *
On the basis of #dxiv's pointer, here is a batch wrapper for the routine:
Get-ConsoleAsText.bat
:: save the contents of the screen console buffer to a disk file.
#set "_Filename=%~1"
#if "%_Filename%" equ "" #set "_Filename=Console.txt"
#powershell Get-ConsoleAsText.ps1 >"%_Filename%"
#exit /b 0
The Powershell routine is pretty much as was presented in the link, except that:
I had to sanitise it to remove some of the more interesting character substitutions the select/copy/paste operation introduced.
The original saved the trailing spaces as well. Those are now trimmed.
Get-ConsoleAsText.ps1
# Get-ConsoleAsText.ps1 (based on: https://devblogs.microsoft.com/powershell/capture-console-screen/)
#
# The script captures console screen buffer up to the current cursor position and returns it in plain text format.
#
# Returns: ASCII-encoded string.
#
# Example:
#
# $textFileName = "$env:temp\ConsoleBuffer.txt"
# .\Get-ConsoleAsText | out-file $textFileName -encoding ascii
# $null = [System.Diagnostics.Process]::Start("$textFileName")
#
if ($host.Name -ne 'ConsoleHost') # Check the host name and exit if the host is not the Windows PowerShell console host.
{
write-host -ForegroundColor Red "This script runs only in the console host. You cannot run this script in $($host.Name)."
exit -1
}
$textBuilder = new-object system.text.stringbuilder # Initialize string builder.
$bufferWidth = $host.ui.rawui.BufferSize.Width # Grab the console screen buffer contents using the Host console API.
$bufferHeight = $host.ui.rawui.CursorPosition.Y
$rec = new-object System.Management.Automation.Host.Rectangle 0,0,($bufferWidth - 1),$bufferHeight
$buffer = $host.ui.rawui.GetBufferContents($rec)
for($i = 0; $i -lt $bufferHeight; $i++) # Iterate through the lines in the console buffer.
{
$Line = ""
for($j = 0; $j -lt $bufferWidth; $j++)
{
$cell = $buffer[$i,$j]
$line = $line + $cell.Character
}
$line = $line.trimend(" ") # remove trailing spaces.
$null = $textBuilder.Append($line)
$null = $textBuilder.Append("`r`n")
}
return $textBuilder.ToString()
The contents of the console buffer can be retrieved with the PS script from PowerShell's team blog Capture console screen mentioned in a comment, now edited into OP's question.
The last line could also be changed to copy the contents to the clipboard instead of returning it.
Set-Clipboard -Value $textBuilder.ToString()
As a side note, the reasons for using a StringBuilder rather than direct concatenation are discussed in How does StringBuilder work internally in C# and How the StringBuilder class is implemented.

Looking for MORE/MOVE solutions that can handle files with more than 65534 rows

I have numerous uniquely named .CSV files that I need to remove the first 17 lines from. Some of these files exceed 65534 rows so my MORE/MOVE Batch script is not working. Looking for alternative solutions.
#echo off
for %%a in (*.csv) do (
more +17 "%%a" >"%%a.new"
move /y "%%a.new" "%%a" >nul
)
Regardless of number of rows input I am looking to have the 17 header rows removed and new file with all remaining rows built.
Here's a powershell option; this one uses a stream to cater for your large files:
$csvs = Get-ChildItem -Path "P:\ath to\your csvs" -Filter *.csv
foreach ( $csv in $csvs ) {
$fin = New-Object System.IO.StreamReader( $csv.FullName )
$fout = New-Object System.IO.StreamWriter( $csv.FullName+".new" )
try {
for( $s = 1; $s -le 17 -and !$fin.EndOfStream; $s++ ) {
$fin.ReadLine()
}
while( !$fin.EndOfStream ) {
$fout.WriteLine( $fin.ReadLine() )
}
}
finally {
$fout.Close()
$fin.Close()
}
}
Just change the path to your .csvs on the first line, before testing it.
I have purposely left out the deletion of the original files, simply appending .new to the new filenames to allow you time to check the results, test the speed etc. I will leave it to you to include a Rename/Delete or Move should you feel the need to extend the functionality.
Here's a one-line solution
for %%a in (*.txt) do powershell -Com "sc -Path '%%a' -Value (gc '%%a' | select -Skip 17)"
where gc and sc are default aliases for Get-Content and Set-Content respectively. See also
Powershell select-object skip multiple lines?
Powershell skip first 2 lines of txt file when importing it
If your files are huge then it'll be better to read in lines or blocks which can also be implemented easily using file functions, [IO.File]::OpenText or the -ReadCount option of Get-Content in PowerShell
Reading large text files with Powershell
Reading very BIG text files using PowerShell
How to process a file in PowerShell line-by-line as a stream
How can I make this PowerShell script parse large files faster?
As Squashman mentioned, for /f also has an option to skip lines at the beginning of the file
for %%a in (*.csv) do (
for /f "usebackq skip=17 delims=" %%l in ("%%f") do #echo(%%l>>"%%a.new"
move /y "%%a.new" "%%a" >nul
)
But that won't work if your file contains lines with special characters like & or |. For more information about it run for /?
Make your own cut command. This is VBScript ported to VB.NET.
Cut
cut {t|b} {i|x} NumOfLines
Cuts the number of lines from the top or bottom of file.
t - top of the file
b - bottom of the file
i - include n lines
x - exclude n lines
Example
cut t i 5 < "%systemroot%\win.ini"
Cut.bat
REM Cut.bat
REM This file compiles Cut.vb to Cut.exe
REM Cut.exe Removes specified from top or bottom of lines from StdIn and writes to StdOut
REM To use
REM cut {t|b} {i|x} NumOfLines
Rem Cuts the number of lines from the top or bottom of file.
Rem t - top of the file
Rem b - bottom of the file
Rem i - include n lines
Rem x - exclude n lines
Rem
Rem Example - Includes first 5 lines Win.ini
Rem
Rem cut t i 5 < "%systemroot%\win.ini"
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\Cut.exe" "%~dp0\Cut.vb" /verbose
pause
Cut.vb
'DeDup.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Public Module DeDup
Sub Main
Dim Arg() As Object
Dim RS as Object
Dim LineCount as Object
Dim Line as Object
Arg = Split(Command(), " ")
rs = CreateObject("ADODB.Recordset")
With rs
.Fields.Append("LineNumber", 4)
.Fields.Append("Txt", 201, 5000)
.Open
LineCount = 0
Line=Console.readline
Do Until Line = Nothing
LineCount = LineCount + 1
.AddNew
.Fields("LineNumber").value = LineCount
.Fields("Txt").value = Console.readline
.UpDate
Line = Console.ReadLine
Loop
.Sort = "LineNumber ASC"
If LCase(Arg(0)) = "t" then
If LCase(Arg(1)) = "i" then
.filter = "LineNumber < " & LCase(Arg(2)) + 1
ElseIf LCase(Arg(1)) = "x" then
.filter = "LineNumber > " & LCase(Arg(2))
End If
ElseIf LCase(Arg(0)) = "b" then
If LCase(Arg(1)) = "i" then
.filter = "LineNumber > " & LineCount - LCase(Arg(2))
ElseIf LCase(Arg(1)) = "x" then
.filter = "LineNumber < " & LineCount - LCase(Arg(2)) + 1
End If
End If
Do While not .EOF
Console.writeline(.Fields("Txt").Value)
.MoveNext
Loop
End With
End Sub
End Module

SOAPUI execute groovy script from command line

I wrote batch script which executes soapui from command line. It does sth like Launch LoadTestRunner and generates reports in indicated folder.
In my Test Steps I need to change dates in specific nodes on tomorrows date. I wrote groovy script which works fine in soapui but I can't load it from batch script.
My question is: is there a possibility to run tests from the console (batch script) but first run groovy script which will change dates in the same batch script?
My groovy script:
import com.eviware.soapui.impl.wsdl.teststeps.*
def ui = com.eviware.soapui.support.UISupport;
def project = context.testCase.testSuite.project
def testSuite = project.getTestSuiteAt(0)
def testCase = testSuite.getTestCaseAt(0)
//set date
def today = new Date()
def dd = today.getDate() + 1
def mm = today.getMonth() + 1 //January is 0!
def yyyy = 1900 + today.getYear()
if (dd < 10) dd = '0' + dd
if (mm < 10) mm = '0' + mm
tomorrowsDateAndTime = yyyy + '-' + mm + '-' + dd + "+01:00"
//xml paths to change date
def somePathToChangeDate = "//somePathToChangeDate"
def somePathToChangeDate2 = "//somePathToChangeDate2"
def stepList = testCase.getTestStepList() //list of all tests in package
for (singleTest in stepList) {
log.info(" " + singleTest.getName())
def testName = singleTest.getName()
testSuite.setPropertyValue("testName", testName)
def testStep = testCase.getTestStepByName(testName)
def testStepContext = new com.eviware.soapui.impl.wsdl.testcase.WsdlTestRunContext(testStep);
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(testStepContext)
def inputHolder = testName + "#Request"
def holderRawReq = groovyUtils.getXmlHolder(inputHolder)
holderRawReq[somePathToChangeDate] = tomorrowsDateAndTime
holderRawReq[somePathToChangeDate2] = tomorrowsDateAndTime
holderRawReq.updateProperty()
}
log.info " Dates are updated"
My batch script:
#echo off
cd %~dp0\SoapUI-5.2\bin
cmd.exe /C loadtestrunner.bat -s"TestSuite 1" -cmyTests -l"LoadTest 1" -uusername -ppassword -r -f%~dp0\reports %~dp0\my-soapui-project.xml
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" %~dp0\Reports\LoadTest_1-log.txt| find /C ":""
::this code checks if there are any error reports
for /f %%a in ('!cmd!') do set iterator=%%a
echo %iterator%
cls
if [%iterator%] equ [3] (echo "Tests was completed successfully") else (
echo "Tests failed"
)
pause

Batch - Countup Timer

I'm trying to make a countup timer in batch and so far, I have this
:timer
set time=0
:time
set /a time=%time%+1
ping localhost -n 2 >nul
goto time
The problem is, I want this to run at the same time another event is happening in the same batch file. How can I do this?
Are you wanting to time how long it takes to execute your batch script? If you have PowerShell installed you can use the Measure-Command cmdlet.
Save this as timeit.bat:
#echo off
>~timeit-%~n1.bat echo(%*
powershell "Measure-Command {Start-Process ~timeit-%~n1.bat -Wait}"
del ~timeit-%~n1.bat
Then when you want to time the execution of something, say, systeminfo, just timeit systeminfo.
If you prefer a non-powershell solution (as powershell takes several annoying seconds to prime itself in its first run per Windows session), here's a faster batch / JScript hybrid solution (more info). It also executes the command in the same window, whereas the powershell solution spawns a new window for your command. Save this as timer.bat:
#if (#a==#b) #end /* begin multiline JScript comment
:: timer.bat command args
:: measures execution time of a command
#echo off & setlocal
if "%~1"=="" (
echo Usage: %~nx0 command args
goto :EOF
)
cscript /nologo /e:JScript "%~f0" %*
goto :EOF
:: end batch portion / begin JScript */
var osh = new ActiveXObject('wscript.shell'), cmd = [], b4 = new Date();
for (var i=0; i<WSH.Arguments.length; i++) {
var arg = WSH.Arguments(i);
cmd.push(/\s/.test(arg) ? '"' + arg + '"' : arg);
}
var exe = osh.Exec('cmd /c ' + cmd.join(' '));
while(!exe.StdOut.AtEndOfStream) WSH.Echo(exe.StdOut.ReadLine())
var x = (new Date() - b4) / 1000,
d = Math.floor(x / 86400),
d = d ? d + ' days ' : '',
h = ('0' + (Math.floor(x / 3600) % 24)).slice(-2),
m = ('0' + (Math.floor(x / 60) % 60)).slice(-2),
s = Math.floor(x % 60) + x % 1,
s = (s < 10) ? '0'+s : s;
WSH.Echo('\r\nExecution completed in ' + d + h + ':' + m + ':' + s);

Batch rename sequential files by padding with zeroes

I have a bunch of files named like so:
output_1.png
output_2.png
...
output_10.png
...
output_120.png
What is the easiest way of renaming those to match a convention, e.g. with maximum four decimals, so that the files are named:
output_0001.png
output_0002.png
...
output_0010.png
output_0120.png
This should be easy in Unix/Linux/BSD, although I also have access to Windows. Any language is fine, but I'm interested in some really neat one-liners (if there are any?).
Python
import os
path = '/path/to/files/'
for filename in os.listdir(path):
prefix, num = filename[:-4].split('_')
num = num.zfill(4)
new_filename = prefix + "_" + num + ".png"
os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
you could compile a list of valid filenames assuming that all files that start with "output_" and end with ".png" are valid files:
l = [(x, "output" + x[7:-4].zfill(4) + ".png") for x in os.listdir(path) if x.startswith("output_") and x.endswith(".png")]
for oldname, newname in l:
os.rename(os.path.join(path,oldname), os.path.join(path,newname))
Bash
(from: http://www.walkingrandomly.com/?p=2850)
In other words I replace file1.png with file001.png and file20.png with file020.png and so on. Here’s how to do that in bash
#!/bin/bash
num=`expr match "$1" '[^0-9]*\([0-9]\+\).*'`
paddednum=`printf "%03d" $num`
echo ${1/$num/$paddednum}
Save the above to a file called zeropad.sh and then do the following command to make it executable
chmod +x ./zeropad.sh
You can then use the zeropad.sh script as follows
./zeropad.sh frame1.png
which will return the result
frame001.png
All that remains is to use this script to rename all of the .png files in the current directory such that they are zeropadded.
for i in *.png;do mv $i `./zeropad.sh $i`; done
Perl
(from: Zero pad rename e.g. Image (2).jpg -> Image (002).jpg)
use strict;
use warnings;
use File::Find;
sub pad_left {
my $num = shift;
if ($num < 10) {
$num = "00$num";
}
elsif ($num < 100) {
$num = "0$num";
}
return $num;
}
sub new_name {
if (/\.jpg$/) {
my $name = $File::Find::name;
my $new_name;
($new_name = $name) =~ s/^(.+\/[\w ]+\()(\d+)\)/$1 . &pad_left($2) .')'/e;
rename($name, $new_name);
print "$name --> $new_name\n";
}
}
chomp(my $localdir = `pwd`);# invoke the script in the parent-directory of the
# image-containing sub-directories
find(\&new_name, $localdir);
Rename
Also from above answer:
rename 's/\d+/sprintf("%04d",$&)/e' *.png
Fairly easy, although it combines a few features not immediately obvious:
#echo off
setlocal enableextensions enabledelayedexpansion
rem iterate over all PNG files:
for %%f in (*.png) do (
rem store file name without extension
set FileName=%%~nf
rem strip the "output_"
set FileName=!FileName:output_=!
rem Add leading zeroes:
set FileName=000!FileName!
rem Trim to only four digits, from the end
set FileName=!FileName:~-4!
rem Add "output_" and extension again
set FileName=output_!FileName!%%~xf
rem Rename the file
rename "%%f" "!FileName!"
)
Edit: Misread that you're not after a batch file but any solution in any language. Sorry for that. To make up for it, a PowerShell one-liner:
gci *.png|%{rni $_ ('output_{0:0000}.png' -f +($_.basename-split'_')[1])}
Stick a ?{$_.basename-match'_\d+'} in there if you have other files that do not follow that pattern.
I actually just needed to do this on OSX. Here's the scripts I created for it - single line!
> for i in output_*.png;do mv $i `printf output_%04d.png $(echo $i | sed 's/[^0-9]*//g')`; done
For mass renaming the only safe solution is mmv—it checks for collisions and allows renaming in chains and cycles, something that is beyond most scripts. Unfortunately, zero padding it ain't too hot at. A flavour:
c:> mmv output_[0-9].png output_000#1.png
Here's one workaround:
c:> type file
mmv
[^0-9][0-9] #1\00#2
[^0-9][0-9][^0-9] #1\00#2#3
[^0-9][0-9][0-9] #1\0#2#3
[^0-9][0-9][0-9][^0-9] #1\0#2#3
c:> mmv <file
Here is a Python script I wrote that pads zeroes depending on the largest number present and ignores non-numbered files in the given directory. Usage:
python ensure_zero_padding_in_numbering_of_files.py /path/to/directory
Body of script:
import argparse
import os
import re
import sys
def main(cmdline):
parser = argparse.ArgumentParser(
description='Ensure zero padding in numbering of files.')
parser.add_argument('path', type=str,
help='path to the directory containing the files')
args = parser.parse_args()
path = args.path
numbered = re.compile(r'(.*?)(\d+)\.(.*)')
numbered_fnames = [fname for fname in os.listdir(path)
if numbered.search(fname)]
max_digits = max(len(numbered.search(fname).group(2))
for fname in numbered_fnames)
for fname in numbered_fnames:
_, prefix, num, ext, _ = numbered.split(fname, maxsplit=1)
num = num.zfill(max_digits)
new_fname = "{}{}.{}".format(prefix, num, ext)
if fname != new_fname:
os.rename(os.path.join(path, fname), os.path.join(path, new_fname))
print "Renamed {} to {}".format(fname, new_fname)
else:
print "{} seems fine".format(fname)
if __name__ == "__main__":
sys.exit(main(sys.argv[1:]))
$rename output_ output_0 output_? # adding 1 zero to names ended in 1 digit
$rename output_ output_0 output_?? # adding 1 zero to names ended in 2 digits
$rename output_ output_0 output_??? # adding 1 zero to names ended in 3 digits
That's it!
with bash split,
linux
for f in *.png;do n=${f#*_};n=${n%.*};mv $f $(printf output_"%04d".png $n);done
windows(bash)
for f in *.png;do n=${f#*_};mv $f $(printf output_"%08s" $n);done
I'm following on from Adam's solution for OSX.
Some gotchyas I encountered in my scenario were:
I had a set of .mp3 files, so the sed was catching the '3' in the '.mp3' suffix. (I used basename instead of echo to rectify this)
My .mp3's had spaces within their names, E.g., "audio track 1.mp3", this was causing basename+sed to screw up a little bit, so I had to quote the "$i" parameter.
In the end, my conversion line looked like this:
for i in *.mp3 ; do mv "$i" `printf "track_%02d.mp3\n" $(basename "$i" .mp3 | sed 's/[^0-9]*//g')` ; done
Using ls + awk + sh:
ls -1 | awk -F_ '{printf "%s%04d.png\n", "mv "$0" "$1"_", $2}' | sh
If you want to test the command before runing it just remove the | sh
I just want to make time lapse movie using
ffmpeg -pattern_type glob -i "*.jpg" -s:v 1920x1080 -c:v libx264 output.mp4
and got a similar problem.
[image2 # 000000000039c300] Pattern type 'glob' was selected but globbing is not supported by this libavformat build
glob not support on Windows 7 .
Also if file list like below, and uses %2d.jpg or %02d.jpg
1.jpg
2.jpg
...
10.jpg
11.jpg
...
[image2 # 00000000005ea9c0] Could find no file with path '%2d.jpg' and index in the range 0-4
%2d.jpg: No such file or directory
[image2 # 00000000005aa980] Could find no file with path '%02d.jpg' and index in the range 0-4
%02d.jpg: No such file or directory
here is my batch script to rename flies
#echo off
setlocal enabledelayedexpansion
set i=1000000
set X=1
for %%a in (*.jpg) do (
set /a i+=1
set "filename=!i:~%X%!"
echo ren "%%a" "!filename!%%~xa"
ren "%%a" "!filename!%%~xa"
)
after rename 143,323 jpg files,
ffmpeg -i %6d.jpg -s:v 1920x1080 -c:v libx264 output.mp4

Resources