Saving Output to Text File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Freshman
Posts: 2
Joined: 07 Sep 2017 04:54

Saving Output to Text File

#1 Post by Freshman » 07 Sep 2017 05:01

Hi all,

I have the following batch file which identifies my current RS232 port number.

Instead of echo-ing the result (either "Warning" or the Com Port, I would like for the result to be written into a txt file so that I can read it out and work with it from within my Access VBA app.

So if %SerialPort% could be written inside say C:\RSPort.txt

Thanks a lot

Code: Select all

@echo off
setlocal EnableExtensions
set "HardwareID=VID_067B&PID_2303"
set "RegistryPath=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USB"
set "ProductName=RS232 cable"
set "DeviceFound=0"

cls

for /F "delims=" %%I in ('%SystemRoot%\System32\reg.exe QUERY "%RegistryPath%\%HardwareID%" 2^>nul') do call :GetPort "%%I"

if "%DeviceFound%" == "0" echo WARNING

echo.
endlocal

pause

goto :EOF

:GetPort
set "RegistryKey=%~1"
if /I not "%RegistryKey:~0,71%" == "%RegistryPath%\%HardwareID%\" goto :EOF

for /F "skip=2 tokens=1,3" %%A in ('%SystemRoot%\System32\reg.exe QUERY "%~1\Device Parameters" /v PortName 2^>nul') do (
    if /I "%%A" == "PortName" set "SerialPort=%%B" && goto OutputPort
)
goto :EOF

:OutputPort
%SystemRoot%\System32\reg.exe query HKLM\HARDWARE\DEVICEMAP\SERIALCOMM | %SystemRoot%\System32\findstr.exe /E /I /L /C:%SerialPort% >nul
if errorlevel 1 goto :EOF
set "DeviceFound=1"
set "DeviceNumber=%RegistryKey:~-1%"
cls
echo %SerialPort%

goto :EOF

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Saving Output to Text File

#2 Post by SIMMS7400 » 07 Sep 2017 06:50

Hi There -

I'm a little bit perplxed. Are you unsure how to direct output to a text file?

>>sometextfile.txt


1 carrot will delete all content from the file before redirecting
2 carrots will append

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Saving Output to Text File

#3 Post by Compo » 07 Sep 2017 07:08

:?

Code: Select all

            ___
       \   /   ````~~~~""---...,__
    `-._\ /                       `~~"--.,_
   ------>|                                `~~"--.,_
    _.-'/ \                            ___,,,---""~~``'          ____
       /   \____,,,,....----""""~~~~````                 `-._\ /     `~~"--.,_
                                                        ------>|              `~~"--.,_
                                                         _.-'/ '.____,,,,----"""~~```'
:lol:

Freshman
Posts: 2
Joined: 07 Sep 2017 04:54

Re: Saving Output to Text File

#4 Post by Freshman » 09 Sep 2017 13:58

SIMMS7400 wrote:Hi There -

>>sometextfile.txt


Sorry for the late reply. I did not have notifications set to auto email.
Thanks a lot - the above worked!

Post Reply