I have a tricky problem: there is a file (version.txt) that contains two lines with version numbers. I parsed this with
FOR /F "tokens=*" %i IN (c:\install\version.txt) DO #echo %i
but now the plan is to put the output as a filename and add the computername in
front, like
%computername%_contens-from-version-file.txt
can someone help me?
thanks
uwe
Use this batch file.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET MAJOR=
SET MINOR=
FOR /F "tokens=*" %%i IN (c:\install\version.txt) DO (
IF "!MAJOR!" == "" (
SET MAJOR=%%i
) ELSE (
IF "!MINOR!" == "" SET MINOR=%%i
)
)
ECHO Version is %MAJOR%.%MINOR%
SET computername=just_example
SET FILENAME=%computername%%MAJOR%.%MINOR%.txt
ECHO File name is %FILENAME%
ECHO File contents>"c:\install\%FILENAME%"
Related
I have text file (2.txt) with even number of lines. Odd lines are current names of the files that I need renamed (odd lines contain file extension). Even lines are new names of the corresponding files (even lines DO NOT contain file extension).
E.g. of such file:
001.mp3
First song.
002.mp3
I am a song, too!
003.mp3
He's the one who will rename me...
(end of file)
I want to loop through file, read the line into variable, check if it contains extension (mp3). If yes (odd line), then save it to "name" variable. If not (even line), then save it to "line" variable with dot and extension added to the end (e.g., First song..mp3) and rename "name" file into "line" file. Thus "001.mp3" will be renamed to "First song..mp3".
I tried to combine different parts of the code from different sources, but something goes wrong.
FOR /F "tokens=*" %%i IN (2.txt) DO (
set var=%%i
if not x%var:mp3=%==x%var% set name=%%i
if x%var:mp3=%==x%var% (
set line=%%i.mp3
ren name line
)
)
pause
This is some tricky code. Hopefully you will understand it.
I used this as my 2.txt file
001.mp3
First song
002.mp3
I am a song
003.mp3
last song
004.mp3
my final! song
And here is my code.
#echo off
setlocal enableExtensions enableDelayedExpansion
for /F "tokens=1* delims=:" %%a in ('findstr /N "^" "2.txt"') do set "lines=%%~a"
set /a "lines=lines / 2"
< 2.txt (
for /L %%l in (1, 1, %lines%) do (
set "line1="
set "line2="
set /P "line1="
set /P "line2="
echo rename "!line1!" "!line2!.mp3"
)
)
endlocal
Pause
My output
rename "001.mp3" "First song.mp3"
rename "002.mp3" "I am a song.mp3"
rename "003.mp3" "last song.mp3"
rename "004.mp3" "my final! song.mp3"
Press any key to continue . . .
Remove the ECHO before the rename command if you feel the output on the screen looks good.
Try like this :
#echo off
setlocal enabledelayedexpansion
for /f "delims=" %%a in (2.txt) do (
set "$Line=%%a"
If /i [!$Line:~-4!]==[.mp3] (
set "$FileMp3=%%a"
) else (
echo ren "!$FileMp3!" "%%a.mp3"
)
)
)
If the output is correct remove the echo
recently I was making a batch file I ran into a problem,
when I made a for /f loop, I used a variable in "tokens", like this example:
for /f "delims= tokens=!count2!" %%a in ('type test.txt 2^>nul') do (
like you can see, i have the variable !count2!, in the loop.
and when i tested it, it displayed !count2!" was unexpected at this time. And I dont know why?
can anyone help?
here is the full code of what I have tried:
#echo off
setlocal enableextensions enabledelayedexpansion
set "count1=0"
set "count2=0"
for /l %%b in (1 1 10) do (
set /a "count2+=1"
set /a "count1+=1"
for /f "delims= tokens=!count2!" %%a in ('type test.txt 2^>nul') do (
set "str!count1!=%%a"
)
)
echo !str1! !str2! !str3! !str4!
pause
by the way, test.txt contains test hello # ###
and, I want to set test, hello, # and ### as their own variable (str1, str2, str3 and str4).
and yes I have tried to do it with % instead of !
tell me if i wasn't clear enough!
thanks for all help :)
why bothering with tokens?
#echo off
setlocal enabledelayedexpansion
set /a count=0
for /f "delims=" %%i in (test.txt) do for %%j in (%%i) do (
set /a count+=1
set str!count!=%%j
)
set str
I'm new in Windows batch programming and I have found problems in the variable assignment. This is my code:
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video=%Video%+1
#echo Video
set file=%%a
#echo file
)
If FILEMEDIA has two lines I would like to obtain Video=2 and the line in file variable. However, at the end I obtain Video=1 and an error when I tried to print file (echo is off).
Some kind of duplicate with How do I increment a DOS variable in a FOR /F loop?
Variables that should be delay expanded are referenced with !VARIABLE! instead of %VARIABLE%.
#echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
set /a Video+=1
#echo !Video!
set file=%%a
#echo file
)
endlocal
I've question about this variables.How can I store result of a command into a variable? For example: saving drive serial into location variable.
thanks.
E.g:
#ECHO OFF
SET location=vol
ECHO We're working with %location%
for /f "delims=" %%x in ('your command and parameters') do set "var=%%x"
where the 'single quotes' are required around the command from which you wish to save output.
Edit - now we know which command.
Comment - one variable can contain up to ~8,000 characters.
Here's a routine that will set the values in $1...$whatever and the entire set in $all with each field enclosed in angle-brackets.
#ECHO OFF
SETLOCAL
SET "$all="
FOR /f "tokens=1*delims=:" %%a IN (
'systeminfo 2^>nul^|findstr /n /r "$"'
) DO (
SET "$%%a=%%b"
FOR /f "tokens=*" %%c IN ("%%b") DO CALL SET "$all=%%$all%%<%%c>"
SET /a max=%%a
)
SET $
pause
FOR /l %%z IN (1,1,%max%) DO CALL ECHO %%$%%z%%
GOTO :EOF
thank you...please see my batch file contents:
for /f "delims=" %%x in ('systeminfo') do set "var=%%x"
command_line.exe %var%
only last line of 'systeminfo' saved in var variable and it is:
"data execution perevention available: yes"
I want to store all contetnt of systeminfo into variable!
thank you.I'm too basic
do you mean result of systeminfo is into all variable? at the end of your code can I add this command:
start command_line.exe $%all%
is this true?
can you put complete code for my batch file?
I need to get a part of some lines(Myfile) out of the 'for' loop either into two separate variables or into an array.My goal is to use each other for constituting path of the following lines result*.txt. I write what you are seing below but it's visible that my 'a' variable is only echoing 'C:\Temp\FR'(the end of line matching token in Myfile).
Please, Is there any way to get all matching tokens in a variable/array? For example, in my case, I need to get 'C:\Temp\USA' and 'C:\Temp\FR' out of the loop for? Thanks
for /f "tokens=2 delims==" %%x in (Myfile) do (
set a=%%x
)
echo %a%
'MyFile
DIR1= C:\Temp\USA
DIR2= C:\Temp\FR
result1.txt
result2.txt
Try this:
#echo off&setlocal
for /f "tokens=1* delims== " %%x in (Myfile) do (
if /i "%%x"=="dir1" set "path1=%%y"
if /i "%%x"=="dir2" set "path2=%%y"
)
echo %path1%
echo %path2%
Output is:
C:\Temp\USA
C:\Temp\FR
There are several ways to do that. If you want read a few lines from the beginnng of the file, the easiest way is to directly read them in a couple variables:
< Myfile (
set /P var1=
set /P var2=
)
Note that these variables contain the complete lines, so it is necessary to extract the part after the equal sign.
In your example you just want the lines with equal sign, so we may use findstr "=" Myfile in a FOR command to just process they. If the number of lines is undefined, you must save all desired lines in an array as you suggested:
setlocal EnableDelayedExpansion
set n=0
for /F "tokens=2 delims==" %%a in ('findstr "=" Myfile') do (
set /A n+=1
set var[!n!]=%%a
)
rem Show all array elements
for /L %%i in (1,1,%n%) do (
echo !var[%%i]!
)
rem Show just the second element
echo %var[2]%
However, in your particular case the lines you want have the form of variable assignments. This means that if you execute such lines with a SET command, the values in the lines will be "automatically" saved:
for /F "delims=" %%a in ('findstr "=" Myfile') do set %%a
After previous FOR , you may directly get the values of DIR1 or DIR2 variables, for example:
echo %DIR1%
echo %DIR2%
This method is very simple and don't require any testing on the individual variables being loaded.
If you want to remove the space after the equal sign in the value, you may adjust FOR options to do so:
for /F "tokens=1* delims== " %%a in ('findstr "=" Myfile) do set %%a=%%b