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
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
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%
Hi I would like to know as to how I can pass an array to vbscript file. I have 5text files containing integers. I read them in a batch program and have to pass those 5 integers to the vbscript file and detect them in vbscript
I don't know if this is what you want:
How to use:
Integers.vbs "number1" "number2" "number3" "number4" "number5"
Integers.vbs
' set the vars
Integer1 = Wscript.Arguments.Item(0)
Integer2 = Wscript.Arguments.Item(1)
Integer3 = Wscript.Arguments.Item(2)
Integer4 = Wscript.Arguments.Item(3)
Integer5 = Wscript.Arguments.Item(4)
' Print all the args
Set objArgs = WScript.Arguments
For Each strArg in objArgs
WScript.Echo strArg
Next
I am trying to pass parameters from a .Net console app to a batch file. The parameters are not coming into the batch file.
How can I properly set up passing the parameters into the bat file?
Here is the method in the console app that I'm executing.
private static int ProcessBatFile(string ifldr, string ofldr, string iext, string oext, Int16 filewidth, Int16 fileheight, Int16 ctr)
{
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = ConfigurationSettings.AppSettings.Get("BatProcessDir") + "imagemagick.bat";
psi.Arguments = "-ifldr=" + ifldr + " -ofldr=" + ofldr + " -iext=" + iext + " -oext=" + oext + " -iwid=" + filewidth + " -ihgt=" + fileheight;
psi.UseShellExecute = false;
Process process = new Process();
process.StartInfo = psi;
process.Start();
return ctr;
}
Below, is the code in the bat file I'm trying to execute:
#echo on
echo %ofldr%
echo %ifldr%
echo %iwid%
echo %ihgt%
echo %oext%
echo %iext%
If you pass them as paramters, you can do this in the c# code:
psi.Arguments = ifldr + " " + ofldr + " " + iext + " " + oext + " " + filewidth + " " + fileheight;
and do this in the batch file:
#echo on
set ifldr=%1
set ofldr=%2
set iext=%3
set oext=%4
set iwid=%5
set ihgt=%6
echo %ofldr%
echo %ifldr%
echo %iwid%
echo %ihgt%
echo %oext%
echo %iext%
As an alternative solution, you can also directly modify the environment before executing the batch file using System.Environment.SetEnvironmentVariable:
System.Environment.SetEnvironmentVariable ("ifldr", ifldr);
....
This causes less problems if the parameters may contain spaces.