Ping output to csv via batch - csv

I got most of the following batch file from this site and have modified it for my needs:
#echo off
set/p host=host Address:
set logfile=Log_%host%.log
set csfile=pings_%host%.csv
echo Target Host = %host% >%logfile%
netsh interface show interface >>%logfile%
nslookup myip.opendns.com resolver1.opendns.com >>%logfile%
for /f "tokens=*" %%A in ('ping %host% -n 1 ') do (echo %%A>>%logfile% && GOTO Ping)
:Ping
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A>>%logfile%
echo %date%,%time:~0,2%:%time:~3,2%:%time:~6,2%,%%A>>%csfile%
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping)
The output to the .log file is exactly how I want it. The csv file output looks like this:
02/16/2016 Tue, 8:02:03,Reply from 173.194.115.33: bytes=32 time=16ms TTL=56
I would like to see something like this at the very least:
02/16/2016 Tue, 8:02:03,Reply from 173.194.115.33:, bytes=32, time=16ms, TTL=56
Optimally, I'd like the csv to look like this:
Date,Time,IP,Bytes,Time,TTL
02/16/2016 Tue,8:02:03,173.194.115.33,32,16ms,56
02/16/2016 Tue,8:02:04,173.194.115.33,32,17ms,56
...
I'd like to stay away from PowerShell if possible.
Thanks,
Joe

#ECHO OFF
SETLOCAL
SET "destdir=U:\destdir"
set "host=google.com"
set "csfile=%destdir%\pings_%host%.csv"
set "flagfile=%destdir%\flagfile.flg"
:: Ensure flagfile exists
ECHO.>"%flagfile%"
ECHO(Date,Time,IP,Bytes,Time,TTL>"%csfile%"
:PING
IF NOT EXIST "%flagfile%" GOTO :EOF
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1 ') do (
FOR /f "tokens=1,4,6,8,10delims=:= " %%P IN ("%time:~0,2% %%A") DO (
echo %date%,%%P:%time:~3,2%:%time:~6,2%,%%Q,%%R,%%S,%%T>>%csfile%
)
echo %date% %time:~0,2%:%time:~3,2%:%time:~6,2% %%A
timeout 1 >NUL
GOTO Ping)
GOTO :EOF
Since you have the logfile processing already sorted, this would generate the csvfile. I've set up filenames suit my system and pinged Google for convenience.
This processes the line
" 8 Reply from 173.194.115.33:, bytes=32, time=16ms, TTL=56"
Assuming "reply from" (no doubt that could be gated if required) usng separators of :=and Space It's then simply a matter of outputting the date data followed by the first token, the minutes and seconds with punctuation and a comma-separated list of the remaining required tokens.
I've added a flagfile which gets created on the first entry. If you delete the flagfile, the batch stops, so you don't have to CtrlC to kill the process.

You can use a FOR loop to parse out only the values you care about.
FOR /F "usebackq tokens=1-10 delims==:< " %%A IN (`ping google.com -n 1`) DO (
REM Only process the response line.
IF "%%A"=="Reply" (
ECHO %%C,%%E,%%G,%%I
)
)
This snippet puts the the following data into CSV format:
IP, bytes, time, TTL
Sample output:
74.125.138.138,32,11ms,43
You should be able to adapt this to your script fairly easily.

Related

Use data from a .csv file as input to choice command in batch file

I have a simple batch to write to C:\Program Files (x86)\Data\ori.csv file the folowing information: division, originator name
#echo off
CHOICE /C NS /M "Please Choose Division:"
echo.
if errorlevel 1 set division=A8-NN
if errorlevel 2 set division=A8-NS
:PROMPT
set /P ori= "Add %division% Originator? [(Y)=yes / (N)=No] "
IF /I "%ori%" NEQ "N" goto add (
) else (
goto exit
)
:add
set /p oriname= "Please Enter %division% Originator Name "
echo Division %division% Originator %oriname% has been Sucessfully added
echo %division%,%oriname% >>C:\%programfiles(x86)%\data\Ori.csv
echo.
goto prompt
:exit
pause
the output of csv to be e.g.
A8-NN,Chris
A8-NN,Alfredo
A8-NS,Joe
A8-NN,Patrick
A8-NS,Ann
etc
the data of this .csv is gonna change every 2 months for the divisions (new people assigned in each division)
My problem is that i want in a seperate batch file from ori.csv file to read the data and for a specific division use the choice command to choose one originator
As far i have done this:
CHOICE /C NS /M "Please Choose Division:"
echo.
if errorlevel 1 set division=A8-NN
if errorlevel 2 set division=A8-NS
count=
for /f "tokens=1-20* delims=," %%a in ('type "C:\%programfiles(x86)%\data\Ori.csv"') do (
if %%a== ("%division%)
set b = %%b
set "count=!count!+1"
echo %count% %%b
)
)
What i tried to do is to the %count% variable store a number identifier and to the %b variable store the originator name. How can I use those two variables as input to a choice command?
You'll need a slightly different approach. After some clarification in the comments.. I would recommended that you move towards set /p here, simply because we never know how many options there will be, you say max 20, but tomorrow suddenly there are 27, then what? So I will rather be safe than sorry :)
#echo off & set cnt=0
setlocal enabledelayedexpansion
for /f "usebackq tokens=1* delims=," %%a in ("%programfiles(x86)%\data\Ori.csv") do (
if not defined %%a (
set /a cnt+=1
set "%%a=%%a"
set "!cnt!=%%a"
echo !cnt!. %%a
)
)
set /p "oper=Please choose Division (1 to !cnt!): "
if not %oper% gtr !cnt! (
set cnt=0
for /f "usebackq tokens=1-20* delims=," %%a in ("%programfiles(x86)%\data\Ori.csv") do if "%%a" == "!%oper%!" set /a cnt+=1 & echo !cnt! %%b
)

Windows Batch - Extract values for ffmpeg processing

I have dozens of json files, and I am trying to find two values in each of them and assign the results to two separate variables for ffmpeg processing.
An example json file looks like this:
{
"year": "2018",
"track": "12",
... other data omitted
}
I wish to extract 2018 and 12 so that I can use them in the following ffmpeg command:
ffmpeg -i "same_file_name_as_json.m4a" -metadata:s:a:0 year=2018 --metadata:s:a:0 track=12 -acodec libmp3lame "same_file_name_as_json.mp3"
Is it possible to write a single batch file to achieve the desired result? Any help would be greatly appreciated as I am a complete novice at findstr and setting variables. Thank you.
EDITED:
set "year=" & set "track="
for %%i in (*.json) do (
for /f "usebackq tokens=1,2 delims={:}, " %%a in ("%%i") do (
set "%%~a=%%~b"
if defined year if defined track goto :CONT
)
:CONT
C:\ffmpeg -i "%%~ni.m4a" -metadata:s:a:0 year=%year% -metadata:s:a:0 track=%track% -acodec libmp3lame "%%~ni.mp3"
)
pause
Windows batch scripting does not understand the JSON file format, so it is better to use a language natively supports it. It is not the best idea to treat JSON as "normal" text, because only a slight change (for instance, added, deleted, or moved line-breaks) that do not violate the JSON format can still make big troubles then.
That said, given that the JSON file exactly appears as you have shown it and it features Unix- or DOS/Windows-style line-breaks (that is, a carriage-return character followed by a line-feed character), this code could work for you:
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do set "%%~M=%%~N"
echo year = %year%
echo track = %track%
If you have got a huge JSON file you do not want to unnecessarily fully process, you could use this code instead:
set "year=" & set "track="
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do (
set "%%~M=%%~N"
if defined year if defined track goto :CONT
)
:CONT
echo year = %year%
echo track = %track%
If the (non-array) values you want to extract may also contain one of the defined delimiters ({, :, }, ,, SPACE), you could extend the code to this, given that the values do not contain the characters *, ?, <, >:
set "year=" & set "track="
for /F "usebackq tokens=1,* delims={:}, " %%M in ("file.json") do (
for %%K in (%%N) do set "%%~M=%%~K"
if defined year if defined track goto :CONT
)
:CONT
echo year = %year%
echo track = %track%
To prevent the script from assigning unwanted superfluous variables, you may try this:
for /F "usebackq tokens=1,2 delims={:}, " %%M in ("file.json") do (
if "%%~M"=="year" (set "%%~M=%%~N") else if "%%~M"=="track" set "%%~M=%%~N"
)
echo year = %year%
echo track = %track%
Or this, which prepreocesses the data by the findstr command and filters out the desired lines:
for /F "tokens=1,2 delims={:}, " %%M in ('
findstr /R /C:"^ *\"year\" *:" /C:"^ *\"track\" *:" "file.json"
') do set "%%~M=%%~N"
echo year = %year%
echo track = %track%
Based on your edit, let me suggest to use the last of the above methods, because there is no goto :CONT, which cannot be used within loops as it breaks the block context, and it does not assign additional unwanted variables. Since variables are written and read within the loop body, you have to enable and apply delayed variable expansion. I would do all that the following way:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem /* Iterate over the `*.json` files in the current working directory (`%CD%`);
rem to use the parent directory of this script, use `%~dp0*.json` instead: */
for %%I in ("*.json") do (
rem // Store name of current JSON file in variable:
set "name=%%~nI"
rem // Clear variables for later check for availability:
set "year=" & set "track="
rem // Process the current JSON file:
for /F "tokens=1,2 delims={:}, " %%M in ('
findstr /R /C:"^ *\"year\" *:" /C:"^ *\"track\" *:" "%%~I"
') do (
rem // Assign year and track variables:
set "%%~M=%%~N"
rem // Check of both year and track are available:
if defined year if defined track (
rem // Toggle delayed expansion to avoid troubles with `!`:
setlocal EnableDelayedExpansion
rem // Eventually execute `ffmpeg` tool using all the derived data:
ffmpeg -i "!name!.m4a" -metadata:s:a:0 year=!year! -metadata:s:a:0 track=!track! -acodec libmp3lame "!name!.mp3"
endlocal
)
)
)
endlocal
exit /B
I have dozens of json files...
Windows' cmd doesn't support JSON, so you'd have to resort to PowerShell, or use an external tool that does. You might find xidel interesting.
To extract the value for "year" and "track":
xidel -s input.json -e "$json/(year,track)"
#or
xidel -s input.json -e "$json/year,$json/track"
2018
12
To export to a variable %year% and %track%:
FOR /F "delims=" %A IN ('xidel -s input.json -e "$json/(year:=year,track:=track)" --output-format^=cmd') DO %A
#or
FOR /F "delims=" %A IN ('xidel -s input.json -e "year:=$json/year,track:=$json/track" --output-format^=cmd') DO %A
You don't however need variables to create the strings (ffmpeg commands) you want. xidel can do that too.
You could use a FOR-loop to iterate over all your JSON-files...
FOR %A IN (*.json) DO #xidel -s %A -e "$json/concat('ffmpeg -i \"%~nA.m4a\" -metadata:s:a:0 year=',year,' --metadata:s:a:0 track=',track,' -acodec libmp3lame \"%~nA.mp3\"')"
ffmpeg -i "name-of-json-file.m4a" -metadata:s:a:0 year=2018 --metadata:s:a:0 track=12 -acodec libmp3lame "name-of-json-file.mp3"
...but to call xidel for each and every JSON-file is very inefficient. xidel can do this much more efficiently.
xidel's equivalent for FOR %A IN (*.json) DO #ECHO %A is xidel -se "file:list(.,false(),'*.json')"
Then you can use the following query to process all your JSON-files at once:
xidel -se "for $x in file:list(.,false(),'*.json') return json-doc($x)/concat('ffmpeg -i \"',replace($x,'json','m4a'),'\" -metadata:s:a:0 year=',year,' --metadata:s:a:0 track=',track,' -acodec libmp3lame \"',replace($x,'json','mp3'),'\"')"
Prettified command/query:
xidel -se ^"^
for $x in file:list(.,false(),'*.json') return^
json-doc($x)/concat(^
'ffmpeg -i \^"',^
replace($x,'json','m4a'),^
'\^" -metadata:s:a:0 year=',^
year,^
' --metadata:s:a:0 track=',^
track,^
' -acodec libmp3lame \^"',^
replace($x,'json','mp3'),^
'\^"'^
)^
"

parse csv to make more readable

I am using PsInfo from sysinternals to return drive information in the form of a csv.
When I run this command.
psinfo -c -d volume
I get the following output.
PCName,,C:,Fixed,NTFS,,930.97 GB,705.81 GB,75.8%,D:,CD-ROM,,,,,0.0%,G:,Fixed,NTFS,My Book,1862.98 GB,889.71 GB,47.8%
My goal here is to parse that output to achieve this format:
PCNAME,,
,,C:,Fixed,NTFS,,930.97 GB,705.81 GB,75.8%
,,G:,Fixed,NTFS,My Book,1862.98 GB,889.71 GB,47.8%
So when the output is read by a spreadsheet viewer it appears readable and organized.
I've tried using regex to match the drive letter bit I don't know how to correctly capture the output.
Edit: (forgot to actually post my code...)
This is what I have so far.
ECHO OFF
For /f "tokens=1*" %%x in ('net view ^| find "\\"') do (psinfo %%x filter -d -c >> out.csv & echo. >> out.csv)
::used to remove blanmk lines from output
#jrepl "^[ \t]*(.*?)[ \t]*$" "$1?$1:false" /jmatch /f out.csv /o -
pause
#ECHO OFF
SETLOCAL enabledelayedexpansion
FOR /f "delims=" %%a IN ('psinfo -c -d volume') DO SET "var=%%a"
SET "var2=!var:%%=\!"
SET "retained="
:again
REM FOR /f "tokens=delims==:" %%a IN () DO
FOR /f "tokens=1*delims=:" %%a IN ("%var2%") DO (
CALL :FORMAT "%%a"
SET "var2=%%b"
IF NOT "%%b"=="" GOTO again
)
GOTO :EOF
:FORMAT
SET "data=%~1"
IF "%data:~-1%"=="\" (SET "data=%retained%:%data%") ELSE (SET "data=%retained%:%data:~,-2%")
IF DEFINED retained (
ECHO ,,!data:\=%%!
) ELSE (
ECHO %data:~1%,
)
SET "retained=%~1"
SET retained=%retained:~-1%
GOTO :eof
substitutes \ for % for ease of processing.
This worked for me. Given this example you should be able to play around with the format of the output you desire.
#echo off
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
SET "pcname=%%G"
SET "driveinfo=%%H"
)
set driveinfo="%driveinfo:,=","%"
:LOOP
FOR /F "tokens=1-7* delims=," %%G IN ("%driveinfo%") DO (
echo "%pcname%",%%G,%%H,%%I,%%J,%%K,%%L,%%M
SET "driveinfo=%%N"
)
IF DEFINED driveinfo GOTO LOOP
Output
"COCO","C:","Fixed","NTFS","","930.97 GB","705.81 GB","75.8%"
"COCO","G:","Fixed","NTFS","My Book","1862.98 GB","889.71 GB","47.8%"
And here is the code changed to replicate your exact needed Output.
#echo off
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
SET "pcname=%%G"
SET "driveinfo=%%H"
)
set driveinfo="%driveinfo:,=","%"
echo %pcname%,,
:LOOP
FOR /F "tokens=1-7* delims=," %%G IN ("%driveinfo%") DO (
echo ,,%%~G,%%~H,%%~I,%%~J,%%~K,%%~L,%%~M
SET "driveinfo=%%N"
)
IF DEFINED driveinfo GOTO LOOP
And here is doing it in a couple of less lines.
#echo off
setlocal enabledelayedexpansion
FOR /F "tokens=1* delims=," %%G IN ('psinfo -c -d volume') DO (
echo %%G,,
SET "driveinfo=%%H"
)
set "drive2=echo ,,!driveinfo:%%=%%&echo,,!"
%drive2:~0,-1%
You can get the same information with Powershell, eg with the Get-Volume command. This returns, everything except the ration eg:
DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining Size
----------- --------------- ---------- --------- ------------ ----------------- ------------- ----
D CD-ROM Healthy Unknown 0 B 0 B
C Win7 NTFS Fixed Healthy OK 43.59 GB 238.03 GB
H Projects NTFS Fixed Healthy OK 96.73 GB 375.24 GB
Get-Volume returns volume information objects. The shell just displays the most common ones.
You can select only the properties you want, and add more columns with the select command:
Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
#{Name="Ratio";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
|Format-Table
DriveLetter FileSystemLabel FileSystem SizeRemaining Size Ratio
----------- --------------- ---------- ------------- ---- -----
C Win7 NTFS 46793633792 255584587776 18,31%
H Projects NTFS 103853056000 402911129600 25,78%
I'm cheating a bit here, by filtering out the CD ROM with where
If the results are too long to fit in the window, Powershell displays them as a list of name/values. The final Format-Table is used to force Powershell to format the results as a table
I can store the table to a file with a normal redirection :
Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
#{Name="Rate";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
|Format-Table > somefile.txt
or I can export the results to a real CSV with Export-CSV
Get-Volume | where {$_.DriveType -ne 'CD-ROM'}
| select DriveLetter, FileSystemLabel,FileSystem,SizeRemaining, Size,
#{Name="Rate";Expression={"{0:P}" -f ($_.SizeRemaining/$_.Size)}}
| Export-CSV somefile.csv
The following code snippet reformats the output of psinfo -c -d volume as you wish. Since it seems that you want it filtered by drive type (Fixed) according to your sample data, I implemented such a feature; to not filter anything, simply change line set "FILTER=Fixed" to set "FILTER=":
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "FILTER=Fixed"
(set LF=^
%= empty line =%
)
for /F "tokens=1,* delims=, eol=," %%K in ('psinfo -c -d volume') do (
echo(%%K,,
set "LINE=%%L"
setlocal EnableDelayedExpansion
set ^"LINE=!LINE:%%,=%%^%LF%%LF%!^"
for /F "delims= eol=," %%E in ("!LINE!") do (
endlocal
if defined FILTER (
for /F "tokens=2 delims=, eol=," %%F in ("%%E") do (
if "%%F"=="%FILTER%" (
echo(,,%%E
)
)
) else (
echo(,,%%E
)
setlocal EnableDelayedExpansion
)
endlocal
)
endlocal
exit /B

How to copy files using Set_Monitor function in BATCH file CMD

I need some help with cmd .bat code. I have a machine which makes different tests. Tests results goes to pc in specific folder. Each test is named automatically as stated in a program. Tests are written in .csv format. Problem is, that each time i make a new test, the older test file is owerwritten. At least i have made bat file, which sets monitor at that folder specific file and copy, rename and write it in specific place. I need that with all my test files, now i have just with one..
This is what i have:
#Echo Off
if not DEFINED IS_MINIMIZED set IS_MINIMIZED=1 && start "" /min "%~dpnx0" %* && exit
Set _Delay=3
Set _Monitor="c:\Users\Topas\Desktop\Export\Tacktestorezultatai.csv"
Set _Base=%temp%\BaselineState.dir
Set _Chck=%temp%\ChkState.dir
Set _OS=6
Ver|Findstr /I /c:\Users\Topas\Desktop\Export\Tacktestorezultatai.csv>Nul
If %Errorlevel%==0 Set _OS=5 & Set /A _Delay=_Delay*1000
:_StartMon
Call :_SetBaseline "%_Base%" "%_Monitor%"
:_MonLoop
If %_OS%==5 (Ping 1.0.0.0 -n 1 -w %_Delay%>Nul) Else Timeout %_Delay%>Nul
Call :_SetBaseline "%_Chck%" "%_Monitor%"
FC /A /L "%_Base%" "%_Chck%">Nul
If %ErrorLevel%==0 Goto _MonLoop
::
echo %DATE%
echo %TIME%
set datetimef=%date:~-4%_%time:~0,2%_%time:~3,2%_%time:~6,2%
echo %datetimef%
xcopy /s/z "c:\Users\Topas\Desktop\Export\" "c:\Users\Topas\Desktop\galas"
setlocal EnableDelayedExpansion
for %%f in (Tacktestorezultatai.csv) do (
set i=0
for /F "delims=" %%l in (%%f) do (
set /A i+=1
set line!i!=%%l
)
echo %%f, >> bendras.csv
echo !line2!>> bendras.csv
echo !line3!>> bendras.csv
)
ren "c:\Users\Topas\Desktop\galas\" "Tacktestorezultatai_%datetimef%.csv"
::
Echo.Change Detected
Goto :_StartMon
:::::::::::::::::::::::::::::::::::::::::::::::::::
:: Subroutine
:::::::::::::::::::::::::::::::::::::::::::::::::::
:_SetBaseline
If Exist "%temp%\tempfmstate.dir" Del "%temp%\tempfmstate.dir"
For /F "Tokens=* Delims=" %%I In ('Dir /S "%~2"') Do (
Set _Last=%%I
>>"%temp%\tempfmstate.dir" Echo.%%I
)
>"%~1" Findstr /V /C:"%_Last%" "%temp%\tempfmstate.dir"
Goto :EOF
exit
Tacktestorezultatai.csv is one of the files, which are exported after making a test. There are more files, for example 90laipsniurezultatai.csv ; Trintiestestorezultatai.csv . I want to make the same to these files.

Merge CSV Files (Without Duplicates) in Batch

I want to merge two similar CSV files using batch. I have found a file that was working perfectly and now is not. This may have been due to renaming the CSV files that were used.
I want what is demonstrated below:
File 1:
name1,group1,data1
name2,group2,data2
name3,group3,data3
File 2:
name1,group1,data1,time1
name2,group2,data2,time2
Merged file:
name1,group1,data1,time1
name2,group2,data2,time2
name3,group3,data3
(Note that the fourth column was not filled in by name3 and was subsequently not on file 2.)
The following code was modified from: http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_27694997.html.
#echo off
set "tmpfile=%temp%\importlist.tmp"
set "csvfile=importlist.csv"
copy nul "%tmpfile%" >nul
echo.
echo Processing all CSV files...
set "header="
for %%a in (%1) do (
if not "%%a"=="%csvfile%" (
set /p =Processing %%a...<nul
for /f "tokens=1* usebackq delims=," %%b in ("%%a") do (
if /i "%%b"=="Keyword" (
if not defined header (
set /p =Found header...<nul
set "header=%%b,%%c"
)
) else (
title [%%a] - %%b,%%c
findstr /b /c:"%%b" /i "%tmpfile%">nul || echo %%b,%%c>>"%tmpfile%"
)
)
echo OK
)
)
echo Finished processing all CSV files
echo.
echo Creating %csvfile%
echo %header%>"%csvfile%"
set /p =Sorting data...<nul
sort "%tmpfile%">>"%csvfile%"
echo OK
del "%tmpfile%"
echo Finished!
title Command Prompt
exit /b
The problem is that when executed it just creates a sorted CSV with all the data from the first file and not the second.
I have attempted to get it working by putting quotation marks around the parameter (%1 - "directory*.csv") to no avail.
you might try this
#echo off &setlocal disabledelayedexpansion
for /f "delims=" %%a in (file1.csv) do set "#%%~a=7"
for /f "tokens=1-4delims=," %%a in (file2.csv) do (
set "lx1=#%%~a,%%~b,%%~c"
setlocal enabledelayedexpansion
if defined !lx1! (
endlocal
set "#%%~a,%%~b,%%~c="
) else (
endlocal
)
set "#%%~a,%%~b,%%~c,%%~d=4"
)
(for /f "delims==#" %%a in ('set #') do echo %%~a)>merge.csv
type merge.csv
It doesn't work, if you have = or # in your data.