Extract a variable value from a text - batch-file

it's quite complicated....using a batch script on windows platform, I need to extract a variable string from an output file, editing the final result towards another file.
Example, the file text.log comes to me filled in this way:
C:Windows\xfile\CODSSD1.XXX
C:Windows\xfile\RPDDFSS.XXX
C:Windows\xfile\XCCLDJW.XXX
I need to catch only the last word in all rows, editing them in this manner:
XXXCODSSD1
XXXRPDDFSS
XXX......
I've tried different commands but I'm not an expert.
Thank you very much!
Kind regards.
Joe

Given your output file named text.log, I would suggest something similar to the following:
#Echo Off
For /F "UseBackQ Delims=" %%A In ("text.log"
) Do For /F "Tokens=* Delims=." %%B In ("%%~xA%%~nA") Do Echo %%B
Pause
I have split the For loop over two lines only to maintain line length within 80 characters. Whilst it isn't a requirement, It's a best practice preference.
Edit
To test it at the command prompt, you can use this single line:
For /F "UseBackQ Delims=" %A In ("text.log") Do #For /F "Tokens=* Delims=." %B In ("%~xA%~nA") Do #Echo %B

This is a pretty simple one line FOR command using the /F option to read the file. You can use the FOR command modifiers to break up the file name into its individual parts. The x modifier is for the file extension. The n modifier is for the base file name.
#echo off
setlocal enabledelayedexpansion
FOR /F "usebackq delims=" %%G IN ("text.log") DO (
set "var=%%~xG%%~nG"
echo !var:~1!
)
pause

Related

Using a filename variable in a for loop

I have a directory with a file in it (actually it has a lot of files, but I figured getting one to work was the first step before building the loop to hit each file), that needs to be edited and saved with a similar filename. Instead of manually typing in the filename, I'd like to use a variable containing the filename.
Sample Input Data
FileX.txt
Text line
Text line
Text line
Desired Output Data
FileX2.txt
1 Text line
2 Text line
3 text line
I can manage this with one file at a time, I'm struggling to write one script to look through the contents of a folder and do this to each text file within the folder.
I'm on windows 7 and this is what I have so far:
#echo off
setLocal EnableDelayedExpansion
Set N=0
REM THE BELOW LINE IS THE VARIABLE I'M TRYING TO SET AND THEN HAVE PASSED AS THE FOR LOOP PARAMETER
Set F="FILENAME"
REM IF I SKIP A VARIABLE AND HARDCODE THE FILENAME IN THE BELOW LINE, IT WORKS FOR THE ONE FILE
for /f "tokens=* delims= " %%a in (FILENAMEVARIABLE.txt) do (
Set /a N=!N!+1
echo !N! %%a, >> !F!.txt
)
The opening statement of the for loop, is where I can't get the variable to take. In FILENAMEVARIABLE.txt I have tried %%F, %F, !F!, and %%~nxf none of which manage to call the correct file to start the loop. Any ideas what I'm doing wrong here?
Here's what I think you were trying to do:
#Echo Off
SetLocal EnableDelayedExpansion
For %%A In (*.txt) Do (Set "N=0"
(For /F "UseBackQ Tokens=*" %%B In ("%%A") Do (Set /A N +=1
Echo !N! %%B))>"%%~nA2%%~xA")
You could also use FindStr:
#Echo Off
For /F "Tokens=1-2* Delims=:" %%A In ('FindStr /N "^" *.txt 2^Nul'
) Do >>"%%~nA2%%~xA" Echo %%B %%C
In both of the above examples I have assumed that the source directory and script directory are the same.

How to create a batch file on Windows to write out strings from a text file based on a substring and new line?

folks!
I'm not sure if this is a very simple task or a very complicated one, but either way I'm struggling with it. Suppose I have a text file like this:
11111FOO
11111BAR
22222ZOOM
33333FOO
11111CAR
I want to figure out a command line in windows that I can plop into a batch file that will pull out text strings from this file and push them to a new file. I would pass in the leading string to search for, and it would take everything from the end of that string to the next new line.
So using the above example, if I said the leading string was 11111, I would get a new text file that looked like this:
FOO
BAR
CAR
Everything else would be ignored.
Thanks!
If there are no potential poison characters in the input file then perhaps this would suffice:
#(For /F "UseBackQ Delims=" %%A In ("input.txt") Do #(Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do #Call Echo %%an:*%%B=%%))>"output.txt"
Magoo's additions:
#Echo Off
(For /F "UseBackQ Delims=" %%A In ("q46858215.txt") Do (Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Call Echo %%an:*%%B=%%))>"Output1.txt"
If "%~1"=="" GoTo :Next
(For /F "Delims=" %%A In ('FindStr/BLC:"%~1" "q46858215.txt"') Do (
Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Call Echo %%an:*%%B=%%))>"Output2.txt"
:Next
(For /F "UseBackQ Delims=" %%A In ("q46858215.txt") Do (Set "an=%%A"
For /L %%B In (%%A 1 %%A) Do Echo %%B))>"Output3.txt"
I used a file named q46858215.txt containing OP's data for my testing.
Produces three output files:
Output1.txt : Compo's original
Output2.txt : filtered to isolate lines beginning with (first parameter to routine)
Output3.txt : Compo's original in reverse, showing the numbers isolated

New line symbol into variable batch

I am having one annoying problem while writing script in batch.
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do set LinkMulti2=!LinkMulti2!%%x
input xxtempLinkMulti3.txt looks like
www.google.com
www.yahoo.com
www.facebook.com
www.linkedin.com
Code is working fine, but when i want to echo my variable the result is:
www.google.comwww.yahoo.comwww.facebook.comwww.linkedin.com
but it should be excatly like output. (with new lines signs)(i've tried using "^" sign between !LinkMulti2! and %%x but it didn't affect on the output.
Please help.thanks. It may be other way to save from file to variable (perfectly the same, with new line signs).
This should do it:
setlocal EnableDelayedExpansion
rem get new-line into variable (note the two empty lines which are mandatory)
set NewLine=^
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do set LinkMulti2=!LinkMulti2!%%x!NewLine!
echo !LinkMulti2!
endlocal
Note that empty lines in the original file are not included.
You need delayed expansion in order to echo the string correctly.
Why you try to concatenate the lines into a single line?
for /f "Tokens=* Delims=" %%x in (xxtempLinkMulti3.txt) do echo %%x will do just fine.
And why do you need to save all of it as a variable? You can type it and it will display all the words in the text file.

How to read properties file from batch file

I have a long test.properties file that contains the following at the very top:
property1=cheese
property2=apple
property3=bread
# many more properties
I can read those properties from the command line by changing the working directory to the one containing test.properties and running the following command:
> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
IF "%A"=="property1" SET firstitem=%B
> FOR /F "tokens=1,2 delims==" %A IN (test.properties) DO
IF "%A"=="property2" SET seconditem=%B
> echo %firstitem%
cheese
> echo %seconditem%
apple
However, when I try to put this code in a batch file stored in the same directory, it fails and I cannot work out why:
FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO
(IF "%A"=="property1" SET firstitem=%B)
FOR /F "tokens=1,2 delims==" %A IN ("%~dp0\test.properties") DO
(IF "%A"=="property2" SET seconditem=%B)
Running the batch file from the command line gives me this:
> "C:\folder\testbatch.bat"
~dp0\test.properties") DO IF "B was unexpected at this time.
What can I do to read the properties using the batch file, so that they are stored in variables that can be used in the rest of the script?
EDIT: Problem solved; working code below.
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO
(IF "%%A"=="property1" SET firstitem=%%B)
FOR /F "usebackq tokens=1,2 delims==" %%A IN ("%~dp0\test.properties") DO
(IF "%%A"=="property2" SET seconditem=%%B)
There's two problems here:
In the batch file version you need two percent signs for the iterator variables.
In the batch file version you erroneously put quotes around the file name, causing the string itself to be tokenized, not the contents of the file specified by the string. Update: #Stephan correctly noted the use of the usebackq modifier for a more general solution. But since you're talking about "batch file stored in the same directory", you might as well drop the path prefix %~dp0 altogether.
Corrected version:
#ECHO OFF
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
IF "%%A"=="property1" SET firstitem=%%B
)
FOR /F "tokens=1,2 delims==" %%A IN (test.properties) DO (
IF "%%A"=="property2" SET seconditem=%%B
)
ECHO %firstitem%
ECHO %seconditem%
Returns:
cheese
apple
"What can I do to read the properties using the batch file, so that
they are stored in variables that can be used in the rest of the
script?"
That is surprisingly easy in your case if you are willing to use the strings from the file:
FOR /F "usebackq tokens=*" %%A IN ("%~dp0\test.properties") DO set %%A
echo property2 is %property2%
Note: in case, your path or filename contains spaces, you need the quotes. Tell for not to treat it as a string with usebackq

Reading a specific line in a text file to a variable in a batch file

In one of my batch files I'm attempting to write, I need to read the last line in a 5 line text file into a variable. One way I thought of doing this might be to have each line that is put into the text file overwrite the previous (as the files creation and lines are all created by a command, specifically slmgr /xpr) but I'm not sure how to do this. Any suggestions or alternate ways of doing this are appreciated!
cscript %windir%\system32\slmgr.vbs /xpr > xprtest.txt
pause
for /F "skip=4 delims=" %i in (xprtest.txt) set /p xprvar= <xprtest.txt
pause
echo %xprvar%
if you want specifically line 5:
set "xprvar="
for /F "skip=4 delims=" %%i in (xprtest.txt) do if not defined xprvar set "xprvar=%%i"
OR
for /F "skip=4 delims=" %%i in (xprtest.txt) do set "xprvar=%%i"&goto nextline
:nextline
if you want the last line regardless of the number of lines:
for /F "delims=" %%i in (xprtest.txt) do set "xprvar=%%i"
The for command is quite powerful nowadays:
for /F "skip=4 delims=" %i in (test.txt) do echo %i
Skip the first 4 lines, leaving the 5th line effectively. No delimiters, so that you get the complete line and not only the first token of that line.
Replace echo %i by anything you want to do with the 5th line.

Resources