How to properly cut result from command in batch? I wrote something like that:
#ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
#ECHO %%F%:~7,4%
)
ENDLOCAL
In my case "whoami" returns "europe\archont". After running this batch I receive "europe\archont:~7,4" instead of "arch"
Where I did mistake?
If you have no further functions to perform within your For loop:
#Echo Off
For /F "Delims=" %%A In ('WhoAmI') Do Set "IAm=%%~nxA"
Echo(%IAm:~,4%
Timeout -1
#ECHO OFF
FOR /F "tokens=* USEBACKQ" %%F IN (`whoami`) DO (
set "str=%%F"
#ECHO !str:~7,4!
)
ENDLOCAL
instead of relying on fixed string length, just use the \ as delimiter:
for /f "tokens=2 delims=\" %%F in ('whoami') do set "im=%%F"
when needed, you can cut that:
echo %im:~,4%
Related
I tried the code below but it does not work:
for /f "usebackq tokens=*" %%a in (`CSCRIPT "C:/../myvbs.vbs"`) do set num=%%a
echo %num%
In the myvbs.vbs code, I am passing the number as follows:
wscript.echo number
Apart from the forward slashes which should be backward slashes, you've stipulated to use back quotes without using them.
for /f "usebackq tokens=*" %%a in (`CSCRIPT "C:\..\myvbs.vbs"`) do…
Although technically they shouldn't be needed
for /f "tokens=*" %%a in ('CSCRIPT "C:\..\myvbs.vbs"') do…
try this:
myvbs.vbs
returnIntegerValue = 5
WScript.Quit returnIntegerValue
test.bat
cscript.exe myvbs.vbs
echo %ERRORLEVEL%
I would like to make batch file read from a .txt and have the output be entered as a command. I have written this code, but it won't work when the command has a spaces in it.
#echo off
:READ
for /f "delims= tokens=*" %%c in (C:\file.txt) do (set command=%%c)
%command%
goto READ
What do I do to make it work?
I suppose this is what you want to do:
#echo off
for /f "tokens=*" %%c in (C:\file.txt) do %%c
I would suggest using this if you need to have the command variable to do other stuff (beside executing) with it. If this is not the case #Henrik's answer would suffice:
#echo off
setlocal EnableDelayedExpansion
:READ
for /f "tokens=* usebackq delims=" %%c in ("C:\file.txt") do (
set "command=%%c"
!command!
)
pause
I have a file foo which contains the below content.
Exception/BatchUpdateException/SQLException 755BJX/5268501774913
Exception/BatchUpdateException/SQLException 5DL9E4/5268501583930
Exception/BatchUpdateException/SQLException 5NHBDO/5268501765579
I need only the below output from the content.
755BJX,5268501774913
5DL9E4,5268501583930
5NHBDO,5268501765579
Could you please help on this.
Another one:
for /F "tokens=4,5 delims=/ " %%a in (test.txt) do echo %%a,%%b
#echo off
for /f "tokens=2" %%i in (c:\temp\log.txt) do call :parse %%i
:parse
set text=%1
echo %text:/=,%
explaination :
We read each line of the logfile, take the second part ( right side of the space, as is a default delimiter.
Then we assign this to a variable and we use in place substitution to convert /to ,
FOR /F "tokens=2" %%I IN (test.txt) DO FOR /F "tokens=1,2 delims=/" %%J IN ("%%I") DO #echo %%J,%%K
I'm trying to find words on a first column of a CSV or XLS file, and replace them with words in the second column of the CSV of XLS. I have made something like that but it doesn't work.
Can you help me? For each line, the first column in a variable called ita and the second column in a variable called eng, and then find Ita and replace Eng. As you can imagine I need to translate a web page, starting from the csv with a language for each column. My csv file structure is:
ita1;eng1
ita2;eng2
etc...
This is my wrong script:
#echo off
setlocal enableextensions enabledelayedexpansion
set host=%COMPUTERNAME%
echo Host: %host%
pause
for /f "tokens=1 delims=;" %%Ita in (index.csv) do (
SET ita=%%Ita
echo %ita%
pause
for /f "tokens=2 delims=;" %%eng in (index.csv) do (
set eng=%%eng
echo %eng
pause
(for /f "delims=" %%i in ('findstr /n "^" "index.txt"') do (
set "transl=%%i"
set "transl=!line:%ita%=%eng%!"
echo(!line!
endlocal
))>"index2.txt"
type "index2.txt"
)
)
)
(for /f "tokens=1,2 delims=;" %%a in (index.csv) do echo(%%b;%%a)>index2.txt
type index2.txt
#echo off
setlocal enableextensions enabledelayedexpansion
(for /f "tokens=* usebackq" %%l in ("index.txt") do (
set "line=%%l"
for /f "tokens=1,2 delims=; usebackq" %%a in ("index.csv") do (
set "line=!line:%%a=%%b!"
)
echo !line!
)) > index2.txt
But this approach doesn't care of substring match.
Try using awk
awk -F';' 'NR==FNR {a[$1]=$2;next} { for(x in a) gsub(x,a[x]) } 1' index.csv index.txt
I try to rename a file with a variable myVar that I have set in the for loop below. The problem is that rename is not working. Can anybody tell me why?
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2%myVar%.txt
I could bet that your code is placed inside parentheses like this one:
if some == comparison (
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2%myVar%.txt
)
If this is the case, you need to use Delayed Expansion in order to get the value of a variable that was modified inside the block:
setlocal EnableDelayedExpansion
if some == comparison (
For /F %%A in ('"type tmpFile2.txt"') do set myVar=%%A
ren file1.txt file2!myVar!.txt
)
For further details, search this or other sites for "delayed expansion".
Hi Please try this code.
for /F "tokens=*" %%i in (myfile.txt) do (
set %filename% = %%i
ren file1.txt file2%filename%.txt
)
Your code is working, maybe the problem is with the tmpfile2.txt content (The token),
or maybe the variable value has spaces and thats the reason why is not working,
without the content of the tmpfile2 we can't know why is not working.
Try this way to see what is happenning:
For /F "delims= usebackq" %%# in ("tmpFile2.txt") do (Echo "myVar=%%#" & set "myVar=%%#")
Echo Rename "file1.txt" "file2%myVar%.txt"
Rename "file1.txt" "file2%myVar%.txt"
Or this else:
For /F "delims= usebackq" %%# in ("tmpFile2.txt") do (Rename "file1.txt" "file2%%#.txt")