Use Batch File Processing To Remove Last Character - batch-file

I have a process where an incoming file is csv and has trailing commas after the last entry.
I need to process this and send it out sans that final comma as it causes a verification error with the phantom "empty column"
Currently I've got this piece of code to write each line into a new file:
for /f "tokens=* delims=" %%x in (file.csv) do echo %%i >> test.txt
And I've been trying to use something like echo %string:~0,-1% to remove the trailing comma in conjunction but I'm not having much luck. I don't think %%i can be used the same as a string would be referenced with the above. I've tried writing %%i into a string but seems I've got that syntax wrong too.
[edit]
I do run the file through a vbs script to replace the commas with pipes (, = |) so if there's an easier way to do it as part of that process, in my searching of stackoverflow to try and resolve this prior to asking I found this line which I thought may help:
strNewText = strNewText.Substring(0,strNewText.Length-1)
strNewText being the variable holding the updated data, doesn't work though, now the find and replace text section doesn't actually run when I add that in:
rem CREATE Find And Replace Text VBS SCRIPT
echo Const ForReading = 1 > "%tmp%\fart.vbs"
echo Const ForWriting = 2 >> "%tmp%\fart.vbs"
echo strFileName = Wscript.Arguments(0) >> "%tmp%\fart.vbs"
echo strOldText = Wscript.Arguments(1) >> "%tmp%\fart.vbs"
echo strNewText = Wscript.Arguments(2) >> "%tmp%\fart.vbs"
echo Set objFSO = CreateObject("Scripting.FileSystemObject") >> "%tmp%\fart.vbs"
echo Set objFile = objFSO.OpenTextFile(strFileName, ForReading) >> "%tmp%\fart.vbs"
echo strText = objFile.ReadAll >> "%tmp%\fart.vbs"
echo objFile.Close >> "%tmp%\fart.vbs"
echo strNewText = Replace(strText, strOldText, strNewText) >> "%tmp%\fart.vbs"
echo strNewText = strNewText.Substring(0,strNewText.Length-1) >> "%tmp%\fart.vbs"
echo Set objFile = objFSO.OpenTextFile(strFileName, ForWriting) >> "%tmp%\fart.vbs"
echo objFile.WriteLine strNewText >> "%tmp%\fart.vbs"
echo objFile.Close >> "%tmp%\fart.vbs"
(all the echo's are because I generate the vbs during the batch runtime, I tend to find things easier when everything's done in the one file, this vbs file is then deleted later in the process).

#if (#This==#IsBatch) #then
#echo off
rem **** batch zone *********************************************************
type inputfile.csv | cscript //nologo //e:javascript "%~f0" > outputfile.csv
exit /b
#end
// **** Javascript zone *****************************************************
while (!WScript.StdIn.AtEndOfStream) WScript.StdOut.WriteLine(WScript.StdIn.ReadLine().replace(/,[\s,]+$/,''));
This is an hybrid batch/javascript file. Save as batch file, execute and the inputfile.csv file will be piped into cscript.exe that will execute the javascript code included in the same batch file, iterating over the piped data and deleting all commas and final spaces from last comma to end of line. Output from pipe is sent to outputfile.csv
EDITED - As stated in comments, maybe i missunderstood the question, and it is not necessary to remove commaS at the end of the lines, but only the final comma. In this case, the replace expression should be
... .replace(/,$/,''));

Probably the simplest solution for your problem would be sed for Windows:
C:\>sed "s/,$//" <in.csv >out.csv
You could also write a simple VBScript that does the same thing:
Set re = New RegExp
re.Pattern = ",$"
Do Until WScript.StdIn.AtEndOfStream
WScript.Echo re.Replace(WScript.StdIn.ReadLine, "")
Loop
Usage:
C:\>cscript //NoLogo script.vbs <in.csv >out.csv
PowerShell would be an even better option:
PS C:\> (Get-Content 'in.csv') -replace ',$' | Out-File 'out.csv'
I would not recommend using batch for this.

I'm not sure this will work, but since you only want the code to apply for the last line, I've tried implementing the code differently from your average for loop.
#echo off
setlocal enabledelayedexpansion
set previous=
set current=
ren file.csv file.tmp
3<file.tmp (
:loop
set previous=!current!
set /p current=<&3
if "!current!" == "!previous!" (
Echo !current:~0,-1! >> file.csv
) Else (
Echo !current! >> file.csv
goto :loop
)
)
type file.csv
Echo. &Echo Only delete if file.csv is not corrupt
del /p file.tmp
Endlocal
And that should about do what you want.
Mona.

Related

How can i run the window of the started program minimized

i want to run a .wav-file. This is with
start "" SoundFile.wav
totally practical. But how can i run the window of the standard-windows-soundplayer minimized? So that nothing of my view changes?
start "" SoundFile.wav /MIN
start "" SoundFile.wav /min
start "" SoundFile.wav -MIN
start "" SoundFile.wav -min
didn't work sadly.
Can you help me?
Greets.
Sorry but i am not familiar with stackoverflow, so i can't make it that understandable, but i hope that this is enough.
In a simple way, you could just use a VBS like this:
s = createobject("WScript.Shell")
s.run "music.mp3", 2
Or create an automatic and CLI program to do this:
runmusic.bat
#echo off
set music=%1
set music=%music:"=%
if ":" neq "%music:~1,1%" set music=%cd%\%music%
if "%music:~-4%" neq ".mp3" set music=%music%.mp3
set katorn=%random%
echo >>"%temp%\%katorn%.vbs" set s = createobject("WScript.Shell")
echo >>"%temp%\%katorn%.vbs" s.run "%music%", 2
cscript "%temp%\%katorn%.vbs"
:: Uncomment the next line to autodelete the vbs file.
:: del /q /f "%temp%\%katorn%.vbs"
Usage:
runmusic (name or full path with quotation marks)
In a "professional way" you could do a shortcut file that starts the file minimized.
It could be useful if you needs the musics to have the "artist logo" or something like that, also if you don't want to use an CMD all the time...
Delete all comments to execute.
Set WshShell = CreateObject ("Wscript.Shell")
strDesktop = "X:\Shortcutlocation\yoo"
Set Shortcut = WshShell.CreateShortcut(strDesktop + "\MusicNameMaybe.lnk")
Shortcut.WindowStyle = "7"
Shortcut.IconLocation = "X:\IconPath\youricon.ico" // You can delete this and the file will be the same ever and forever.
Shortcut.TargetPath = "X:\yourpath\music.mp3"
Shortcut.Save
And you don't have to keep the shortcuts on the desktop if you don't want to, you don't even have to keep them fixed. Example:
runmusic.bat
#echo off
set music=%1
set music=%music:"=%
if ":" neq "%music:~1,1%" set music=%cd%\%music%
set music=%music%.mp3
set katorn=%random%
echo >>"%temp%\%katorn%.vbs" Set WshShell = CreateObject ("Wscript.Shell")
echo >>"%temp%\%katorn%.vbs" strDesktop = "%temp%"
echo >>"%temp%\%katorn%.vbs" Set Shortcut = WshShell.CreateShortcut(strDesktop + "\%katorn%.lnk")
echo >>"%temp%\%katorn%.vbs" Shortcut.WindowStyle = "7"
echo >>"%temp%\%katorn%.vbs" Shortcut.TargetPath = "%music%"
echo >>"%temp%\%katorn%.vbs" Shortcut.Save
cscript "%temp%\%katorn%.vbs"
:: Uncomment the next two lines to delete the temporary files.
:: del /q /f "%temp%\%katorn%.vbs"
:: del /q /f "%temp%\%katorn%.lnk"
echo Enjoy!
Usage:
runmusic (name or full path with quotation marks)
Now that you understand, you can even leave a folder with the songs and the other with just the customized shortcuts, which can come in handy.
Hope this helps,
K.

.vbs(5, 1) Microsoft VBScript runtime error: Subscript out of range Error:800A0009

This is my first question on here, because, although I have searched at least 15 different other posts for the answer to my issue, none have the answer. Please help!
QUESTION: How do I fix Error:800A0009?
DETAILS: I am creating a small program that gathers all local computers and sends them all an audio file to be played. Also, I need to know how to force send, if anyone knows. Lastly, I first run "Get Computers.bat".
My Code:
~~~~~~VBS FILE(Remote Speak.vbs)~~~~~~~~~~~~~~~~~~~
(Obtains variable transferred which contains network name of a computer, and sends it a file to be play using SAPI)
'get ip
Option Explicit
Dim args, strOut
set args = Wscript.arguments
strOut= args(0)
IP = strOut
'get MSG
MSG = InputBox("Type what you want the PC to say:", "Remote Voice Send By X BiLe", "")
If MSG = "" Then WScript.quit: Else
'vbs command to send
A = "on error resume next" & VBCRLF & _
"CreateObject(""SAPI.SpVoice"").speak " & """" & MSG & """" & VBCRLF & _
"CreateObject(""Scripting.FileSystemObject"").DeleteFile (""C:\Voice1.vbs"")"
' Create the vbs on remote C$
CreateObject("Scripting.FileSystemObject").OpenTextFile("\\" & ip & "\C$\Voice1.vbs",2,True).Write A
' Run the VBS through Wscript on remote machine via WMI Object Win32_Process
B = GetObject("winmgmts:\\" & IP & "\root\cimv2:Win32_Process").Create("C:\windows\system32\wscript.exe ""C:\Voice1.vbs""", null, null, intProcessID)
~~~BATCH PRIMARY (Get Computers.bat)~~~~~~~~~~~
(Gathers computer names and assign each one, using net view, filtering the "\" to Computer%num%. Also, :tempcall is just an error handler.)
#echo off
cls
set num=1
echo #echo off > Computers.bat
if "%1"=="loop" (
for /f "delims=\ tokens=*" %%a in ('net view ^| findstr /r "^\\\\"') do (
set comp=%%a
call :number
if exist %%f exit
)
goto :eof
)
cmd /v:on /q /d /c "%0 loop"
:tempcall
call temp.bat
echo.
echo.
echo.
echo You have %num%computers on your network!
pause>nul
del /q temp.bat
start Computers.bat
exit
:number
if %comp% == "" (
goto :tempcall
) else (
echo set Computer%num%=%comp% >> Computers.bat
echo cscript "Remote Speak.vbs" %1 >> Computers.bat
echo call "Remote Speak.vbs" >> Computers.bat
echo set num=%num% > temp.bat
echo Computer%num%: %comp%
set /a num=%num% + 1
)
BATCH SECONDARY (Computers.bat)
(The computers I made up off the top of my head, but they are generally in that format.)
#echo off
set Computer1=040227-CYCVN1
cscript "Remote Speak.vbs" //NoLogo > log.txt
set Computer1=051448-YZVN2
cscript "Remote Speak.vbs" //NoLogo > log.txt
pause>nul
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~END DETAILS~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1.) Temp.bat is literally just temporary, it's deleted, as you can see, almost immediately after it's created, it simply holds the value of %num% after it breaks out of the loop, because it didn't show "You have %num%computers on your network!" correctly.
2.) Don't worry too much about the VBScript file except for the top lines:
Option Explicit
Dim args, strOut
set args = Wscript.arguments
strOut= args(0)
IP = strOut
3.) My main issue is that I am trying to find a safe way to have "Computers.bat" call the "Remote Speak.vbs" file and set it's batch variables to be the exact same names to refer to the individual computers, in VBScript variable format.
The error raises because you are not passing any argument to the vbs file, and it is not passed because when you generate computers.bat you are using %1 (the first argument to the :number subroutine) as a parameter, but in call :number there is not any parameter.
Also, the incrementing computer number is not shown in computers.bat because delayedexpansion is not active. When execution reaches a line or block (the code inside parenthesis), the parser replaces variable reads with the value in the variable and then starts to execute it. As the value of the variable changes inside the block, but there is no variable read, only the value of the variable before starting to execute, changes are not seen. You need to setlocal enabledelayedexpansion to enable it and, where needed, change %var% to !var! to indicate the parser that the variable read needs to be delayed, not replaced at initial parse time.
And anyway, your code does not use it. And what is if exist %%f? And why the loop?
For your third question, the Environment property of the WshShell objects lets you read the required variables
Dim env
Set oEnvironment = WScript.CreateObject("WScript.Shell").Environment("PROCESS")
WScript.Echo oEnvironment("Computer1")
This is a fast cleanup of your code. From your question it seems this is only the starting point. Adapt as needed.
RemoteSpeak.vbs
Option Explicit
If WScript.Arguments.Count < 1 Then
WScript.Quit
End If
'get ip
Dim IP
IP = WScript.Arguments.Item(0)
'get MSG
Dim MSG
MSG = InputBox("Type what you want the PC to say:", "Remote Voice Send By X BiLe", "")
If MSG = "" Then
WScript.Quit
End If
Dim A
A = "on error resume next" & VBCRLF & _
"CreateObject(""SAPI.SpVoice"").speak " & """" & MSG & """" & VBCRLF & _
"CreateObject(""Scripting.FileSystemObject"").DeleteFile(WScript.ScriptFullName)"
' Create the vbs on remote C$
CreateObject("Scripting.FileSystemObject").OpenTextFile("\\" & IP & "\C$\Voice1.vbs",2,True).Write A
' Run the VBS through Wscript on remote machine via WMI Object Win32_Process
Dim B
B=GetObject("winmgmts:\\" & IP & "\root\cimv2:Win32_Process").Create("C:\windows\system32\wscript.exe ""C:\Voice1.vbs""", null, null, intProcessID)
getComputers.bat
#echo off
setlocal enableextensions enabledelayedexpansion
cls
set "num=0"
( echo #echo off
for /f "tokens=* delims=\" %%a in ('net view ^| findstr /r /c:"^\\\\"') do (
set /a "num+=1"
echo set "Computer!num!=%%a"
echo cscript "RemoteSpeak.vbs" %%a
)
) > computers.bat
echo You have %num% computers in your network
pause > nul
start Computers.bat
endlocal
exit /b
Dim env
Set oEnvironment = WScript.CreateObject("WScript.Shell").Environment("PROCESS")
WScript.Echo oEnvironment("Computer1")

Batch trying to log information from a REG_MULTI_SZ value

Have seen - Assigning newline character to a variable in a batch script
I am wanting to take a REG_MULTI_SZ and split to multiple lines..
For example, we have:
if %PROCESSOR_ARCHITECTURE% == AMD64 SET ApacheKey="HKLM\SOFTWARE\Wow6432Node\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java"
if NOT %PROCESSOR_ARCHITECTURE% == AMD64 SET ApacheKey="HKLM\SOFTWARE\Apache Software Foundation\Procrun 2.0\Tomcat6\Parameters\Java"
POWERSHELL "Get-ItemProperty 'HKLM:%ApacheKey%' |select -ExpandProperty Options" >> somelog.txt
Thanks to the response below and the post found at - How to read multi line multi string registry entries in PowerShell?
as this is now working!
Please note: I cannot use vbs (well I could) but would rather not as I have to codesign my scripts.
Can break out the "\0" delimiters. The issue that I have right now is that I type in echo %SETTINGS% from the command-line and I can see the new lines.. Probably going to have to PIPE the original variable to a text file. Read in (type) the file and if the "\0" is found to echo. which should write the file properly.
The original log shows:
-Dcatalina.base=C:\tomcat\0-Dcatalina.home=C:\tomcat\0-Djava.endorsed.dirs=C:\tomcat\endorsed\0-Djava.io.tmpdir=C:\tomcat\temp\0-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager\0-Djava.util.logging.config.file=C:\tomcat\conf\logging.properties\0-Dcom.sun.management.jmxremote\0-Dcom.sun.management.jmxremote.port=1092\0-Dcom.sun.management.jmxremote.ssl=false\0-Dcom.sun.management.jmxremote.authenticate=false\0-XX:MaxPermSize=256m\0-Xmx1024m\0-Xms1024m\0-Xverify:none\0-XX:+UseConcMarkSweepGC\0-XX:+UseParNewGC\0-XX:MinHeapFreeRatio=40\0-XX:MaxHeapFreeRatio=60\0-XX:MaxGCPauseMillis=200
What I want it to show is:
-Dcatalina.base=C:\tomcat
-Dcatalina.home=C:\tomcat
-Djava.endorsed.dirs=C:\tomcat\endorsed
-Djava.io.tmpdir=C:\tomcat\temp
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
-Djava.util.logging.config.file=C:\tomcat\conf\logging.properties
-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=1092
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-XX:MaxPermSize=256m
-Xmx1024m
-Xms1024m
-Xverify:none
-XX:+UseConcMarkSweepGC
-XX:+UseParNewGC
-XX:MinHeapFreeRatio=40
-XX:MaxHeapFreeRatio=60
-XX:MaxGCPauseMillis=200
It can be simple using Vbscript working with your batch script:
:: Create readMulti.vbs
(
echo/Const HKEY_LOCAL_MACHINE = ^&H80000002
echo/strComputer = "."
echo/Set oReg=GetObject^("winmgmts:{impersonationLevel=impersonate}!\\" ^& _
echo/ strComputer ^& "\root\default:StdRegProv"^)
echo/strKeyPath = "SOFTWARE\Apache Software Foundation\Procrun 2.0\tomcat6\Parameters\Java"
echo/strValueName = "Options"
echo/oReg.GetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, _
echo/ strValueName,arrValues
echo/For Each strValue In arrValues
echo/ Wscript.Echo strValue
echo/Next)>readMulti.vbs
for /f "tokens=*" %%a in ('cscript //nologo readMulti.vbs') do (echo/%%a>>log.txt)
del readMulti.vbs

Print Batch results to a text file?

I created a batch file to lookup my external ip.
and it works well .
This is the code.
#echo off
>"%temp%\ip.vbs" echo Set objHTTP = CreateObject("MSXML2.XMLHTTP")
>>"%temp%\ip.vbs" echo Call objHTTP.Open("GET", "http://checkip.dyndns.org", False)
>>"%temp%\ip.vbs" echo objHTTP.Send()
>>"%temp%\ip.vbs" echo strHTML = objHTTP.ResponseText
>>"%temp%\ip.vbs" echo wscript.echo strHTML
for /f "tokens=7 delims=:<" %%a in ('cscript /nologo "%temp%\ip.vbs"') do set ip=%%a
echo %ip:~1%
pause
What i want is to Print the results to a text file named "IPlog.txt"
and every time i run the bat file it has to do the same thing and print the new results to the next line in the text file. So please can anyone help me with this.
... or change your
echo %ip:~1%
to
echo %ip:~1% >>IPlog.txt
to run your batch without the additional " >>IPlog.txt "
Please remove the pause command from your code and run the batch-file like this
mybatch.bat >> IPlog.txt
This will append the resulting ip address on to the log file IPLog.txt every time you run this batch file.

Batch: line & remove

I want a batch file which: removes a certain line [line number by %lnum%] in a txt file.
That sounds like a job for sed. On windows, you'd have to install a windows port and then call sed from within your batch file.
sed '<linenumber>d' filename > newfilename
To delete the 4th line:
sed '4d' filename > newfilename
If you are in windows and you want to do it in a batch file, you could do the following by brute force:
#echo off
setlocal ENABLEDELAYEDEXPANSION
SET lineNum=
SET filename=%1
SET targetLine=%2
SET targetFile=%filename%.tmp
DEL %targetFile%
FOR /F " tokens=1 delims=" %%i in (%filename%) do (
SET /a lineNum += 1
if NOT !lineNum! == !targetLine! ECHO %%i >> !targetFile!
)
REN %filename% %filename%.orig
REN %targetFile% %filename%
You pass into the batch the name of your target file and the line number you want removed. It creates a temporary file, pipes the 'good' lines from your original into the temp and then finishes up by renaming the files so that you keep your original and have the modified file in place.
Pure cmd?:
delline.cmd number filename
#echo off
setlocal ENABLEDELAYEDEXPANSION
set line=%~1
set file=%~nx2
set dSource=%~dp2
set i=0
for /F "delims=" %%l in (%dSource%%file%) do (
set /a i+=1
if not !i! EQU %line% call :BuildFile "%%l"
)
del /f "%dSource%%file%" >nul
for %%l in (%_file%) do echo %%~l>>"%dSource%%file%"
endlocal& exit /b 0
:BuildFile
set _file=%_file% %1
exit /b 0
___Notes_____
1: No checks for parameters. First need be a number, the second a file or path+file.
2: As with other answers, no temporary file either.
3: Using setlocal would allow you to integrate this code easy within a script by naming it :delline and using it like: call :delline number file
WARNING: Any blank line from the source file would be skipped/lost in the reading process (by the for..loop). A file with, says 10 lines from which 4 are blanks, would be read as 6 lines to be processed. The line number given would be applied (thus, deleted) from the 6 data lines, and not the starting 10.
Edit
Went a bit too fast, did not see akf's answer which looks real close to mine.
don't have to use any external command. you can use vbscript
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objArgs = WScript.Arguments
num = objArgs(0)
strFile = objArgs(1)
Set objFile = objFS.OpenTextFile(strFile)
Do Until objFile.AtEndOfLine
linenum = objFile.Line
If linenum = Int(num) Then
objFile.SkipLine
End If
strLine = objFile.ReadLine
WScript.Echo strLine
Loop
how to use:
C:\test>cscript /nologo removeline.vbs 4 file.txt > newfile.txt

Resources