Remove Brackets from Variable (CLI-Windows) - string

Based on the code below, the output of Var is (COM19). I was wandering what command can be used to remove the brackets? Tried using sed, but would ideally want a solution that doesn't not require additional installations on Windows.
#echo off
setlocal
wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=4" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a
pause
goto :EOF

for /f "delims=()" %%a in ("%output%") do (echo %%a)

Managed to solve the issue ussing the delims, in line with the token function. Code can be seen below.
#echo off
setlocal
wmic path win32_pnpentity get caption /format:list > output.txt
for /f "tokens=2 delims=()" %%a in ('find /i "USB Serial Port" output.txt') do set var=%%a
pause

Related

Cannot set environmental variable inside a loop in a batch script

I have a batch script that works great and it does the following:
download EDI files from sftp server
generate a dynamic ftp scripts based on the filenames that were downloaded
uploads the file to an internal ftp server via FTP script
I need to modify the batch file to look at the content as some data coming in maybe missing a 0. I do have the logic on how to do this, however, I cannot add the logic to the loop I have as variables do not work.
Below is loop where I want to add the part to correct missing leading zero (that would be in position 4):
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
What I need to do is add an environment variable which will be the content of the file and below is the sample code:
set /p string =< %%A
echo Value for string is set to %string%
echo %string:~9,1% | find "ECHO is on." > nul
if not errorlevel 1 goto Found0
echo no space found
goto End
:Found0
echo found space
echo below is what the correct number needs to be
echo %string:~0,3%0%string:~3% > %%A
:End
I need this to work in batch file as security requirements does not allow PowerShell on the servers.
for /F "tokens=*" %%A in (c:\scripts\permitsin.txt) do (
for /F "delims=" %%S in (%%A) do (
setlocal enabledelayedexpansion
set "string=%%S"
if "!string:~9,1!"==" " echo !string:~0,3!0!string:~3!> %%A
endlocal
)
echo put %%A /qsys.lib/MAEPFTDTA.lib/PERMITIN.file/%%A >> c:\scripts\ftp-to-scoopsoft.ftp1
)
should do what you appear to need. Test on sample data first, of course.
Here's the how : Stephan's DELAYEDEXPANSION link
And it saves a lot of work if you tell us what you want to do rather than how you want to do it.

How do i capture a variable from a text file with a batch script?

Hi I'm trying to write a batch script that reads a long text file, finds a line that says:
Location: xxxxxxxx
And saves xxxxxxxx (only) as a variable that i can use later in the script.
xxxxxxxx is an ID and there is nothing else on that line.
Can someone help?
This works, too.
for /f "tokens=2" %a in ('type longtextfile.txt ^|findstr /b "Location"') do set location=%a
echo %location%
EDIT: Adding Aacini's more efficient way of doing this:
for /f "tokens=2" %a in ('findstr /b "Location" longtextfile.txt') do set location=%a
echo %location%
There are just a few standard Windows commands required for this simple task:
#echo off
set "TextFile=C:\Temp\Text file to search for location.txt"
if not exist "%TextFile%" echo Can't find file: "%TextFile%"& goto :EOF
for /F "usebackq tokens=1* delims=: " %%I in ("%TextFile%") do (
if /I "%%I" == "Location" (
set "Location=%%J"
goto FoundLocation
)
)
echo No location found in file: "%TextFile%"
goto :EOF
:FoundLocation
echo Location is: %Location%
On second line the name of the text file with path must be defined.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
echo /?
for /?
if /?
goto /?
set /?

Batch Scripting - Search for value in text file and copy word next to it

Currently writing a batch program but I seem to be having a difficult time with string manipulation. I need help scanning through the contents of the Output.txt file below, in search of the the word ssudmdm. wherever the word is found, i then need to retrieve the com value on the same line, to the right and store it in a variable.
I am aware that there are a few examples on the site, however I have have tried implementing them with no luck. Any help would be appreciated.
Code:-----------------------------------------------
#ECHO OFF
:: Use local environment copy
SETLOCAL
::regedit.exe /S VM_Putty_Settings.reg
:while1
reg QUERY "HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM" > Output.txt
#start /wait FIND /C /I "ssudmdm" Output.txt
if %ERRORLEVEL% == 1 (
timeout /t 2 >nul
goto :while1
)
for /f "tokens=3" %%a in ('find /i "ssudmdm" output.txt') do set var=%%a
reg add "HKEY_CURRENT_USER\Software\SimonTatham\PuTTY\Sessions\VM_Putty_Settings" /V SerialLine /T REG_SZ /D "%var%" /F
pause
start Putty.exe -load VM_Putty_Settings < nul >NUL 2>NUL
ENDLOCAL
GOTO:EOF
Output file----------------------------------
HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM
\Device\Serial0 REG_SZ COM1
\Device\Serial1 REG_SZ COM2
\Device\ssudserd0000 REG_SZ COM5
\Device\ssudserd0001 REG_SZ COM6
\Device\ssudmdm0000 REG_SZ COM7
Here's how to do it all in one statement, without your intermediate file.
for /f "skip=2 tokens=3" %%a in ('reg query hklm\hardware\devicemap\serialcomm /v *ssudmdm* ^| find /i /v "end of search"') do set var=%%a
If you must have the intermediate file for some reason:
for /f "tokens=3" %%a in ('find /i "ssudmdm" output.txt') do set var=%%a

Create Spreadsheet in Excel of Name and File Type Using Script

I have a bunch of excel files and other types of files that I need to populate into a spreadsheet to keep track of them. I was what the batch script would look like for such task. Also I want to be able to pin point which folders I want to get the file names from.
Thanks in advance
Dir does do multiple folders.
dir c:\windows "c:\program files\e*.txt" /a
Here's doing My Docs.
for /f "skip=2 tokens=3" %A in ('Reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Personal"') do set doc=%A
for /f "usebackq tokens=2* delims= " %i IN (`dir "%doc%" /a /s ^|findstr /i /v "\/"^|findstr /l /v "Pictures Music Video"`) DO #echo %j&echo.

Batch file to search files in a folder

I need a bat file to search excel files in a folder. It should do the following
search in a folder
looks for excel files after created date
if it is older than system date deletes the excel file
if not it runs a converter.js
Can any one help.
Here you go. Remove the echo's from the script once you see good output on the screen. Also, I'm not sure how you are running converter.js so you'll need to change that line to however you do it and if your file dates are in a different format, you'll have to re-arrange the "%MM%/%DD%/%YYYY%" too.
#echo off
setlocal enabledelayedexpansion
pushd "C:\location\of\files\"
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
for /f "tokens=*" %%a in ('dir /o-d /b /tc *.xls') do (
for /f "tokens=1" %%b in ("%%~ta") do (
if "%%b" NEQ "%MM%/%DD%/%YYYY%" (
echo del "%%a"
) ELSE (
echo run converter.js
)
)
)
popd

Resources