The task is to iterate through each line in a file named alts.txt. Then I grab the line and split it at the semicolon and print out the text before the semicolon and after the semicolon.
My file looks something like this...
username:password
username2:password2
username3:
My current code is this:
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=*" %%A in (%file%) do (
set str=%%A
set "username=%str::="^&REM #%
set "pass=%str:*:=%"
echo username=%username% pass=%pass%
)
pause
If someone would be kind enough to show me my error and EXACTLY how to fix the error it would be greatly appreciated.
#echo off
setlocal ENABLEDELAYEDEXPANSION
set file=alts.txt
for /f "tokens=1,2 delims=:" %%A in (%file%) do (
set "$user=%%A"
set "$pass=%%B"
echo username=!$user! pass=!$pass!
)
pause
Be careful using %username%. It's a system variable. You can test writing echo %username% in the CMD prompt. You better choose another name for the Variable like i did.
Related
Hi I don't have much experience in batch-programming and have a problem. I have a .bat script that reads a file with a list of paths and i want to get the filename of these paths. I use the script in cygwin.
My code in the Script:
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("%value%") DO SET MYPATHFILE=%%~nxI
)
When i run the Script %value% is empty.
Value of error1.txt:
a/b/c/d/TextIWant
you need delayed expansion or you can directly use %%a:
for /F %%a in (error1.txt) do (
FOR /F %%I IN ("%%a") DO SET MYPATHFILE=%%~nxI
)
or
setlocal enableDelayedExpansion
for /F %%a in (error1.txt) do (
set value=%%a
FOR /F %%I IN ("!value!") DO SET MYPATHFILE=%%~nxI
)
It looks like you will need Delayed Expansion.
The current problem is, that you want to use a variable in the same set of brackets where you changed the value in (the surrounding For-Loop).
Add setlocal EnableDelayedExpansion to your code at the top and change the %value% to !value!
You can test the problem yourself with this code:
#echo off
setlocal EnableDelayedExpansion
set foo=bar
For /L %%a (1,2,1) do (
set foo=foobar
echo.old value %foo%
echo.new value !foo!
)
I hope it helped :)
Greetings
geisterfurz007
I have a problem witch .bat file
I have somethis like this:
#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
for /F "tokens=*" %%a in (somefile.txt) do (
set key=a
echo /!key!/ /!arr[%key%]!/
)
And code above not work correctly. I mean that the key is display correct but value of !arr[%key%]! is empty (i don't know why).
When i try it like this:
#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
set key=a
for /F "tokens=*" %%a in (somefile.txt) do (
echo /!key!/ /!arr[%key%]!/
)
Code above work good. No idea why first code doesn't work and second work. Any ideas ?
#echo off
setlocal EnableDelayedExpansion
set arr[a]=1
set arr[b]=2
for /F "tokens=*" %%a in (q27861004.txt) do (
set key=%%a
echo /!key!/ /!arr[%%a]!/
)
GOTO :EOF
I used a file named q27861004.txt containing this data for my testing.
a
b
Your problem is that %kay% is evaluated at parse-time, replacing %key% with its value at that time, being empty, hence displaying !arr[]! also empty
I know there have been previous posts about this but none of them have worked for me. I want to do a find and replace string in a text file using the windows command prompt. No parameters, all hard coded. Here is what I have so far:
..........
setlocal enabledelayedexpansion
set SEARCHTEXT=oldtext
set REPLACETEXT=newtext
for /f "tokens=1 delims=" %A in ( C:\in.txt) do (
set string=%A
echo set string:%SEARCHTEXT%=%REPLACETEXT% >> C:\out.txt)
..............
This code just writes "set string:oldtext=newtext" to out.txt for each line in in.txt.
How can I get it to actually replace oldtext with newtext?
Thanks.
Test this:
#echo off
setlocal enabledelayedexpansion
set "SEARCHTEXT=oldtext"
set "REPLACETEXT=newtext"
for /f "usebackq delims=" %%A in ("C:\in.txt") do (
set "string=%%A"
set "string=!string:%SEARCHTEXT%=%REPLACETEXT%!"
>>"C:\out.txt" echo !string!
)
remove the echo.
It is there for testing the code without really destroying anything.
If the output is what you need, just remove it.
EDIT: ah wait - there is a logical failure in the code. It should obviously look like:
...
for /f "tokens=1 delims=" %%A in ( C:\in.txt) do (
set string=%%A
set string=!string:%SEARCHTEXT%=%REPLACETEXT%!
echo !string!>> C:\out.txt
)
I just want to echo a variable which is defined with ENABLEDELAYEDEXPANSION. It doesn't work.
Here's a small part of my long script on the issue
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%x in (%1) do (
set "D=%%x"
echo %%~nD
)
I have also written echo !~nD! but it doesn't work either.
my file (%1) only contains relative paths as so:
VENDOR\ford1.car
VENDOR\bmw.car
and my goal is to echoing 'ford1.car' or 'bmw.car' because I have to use them in the next steps of my script, that is only the file complete name.
Please some help and explanations. Thanks
Try:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
echo %%~nxa
)
For only does substitution on the variable used in the for. You can't set a variable within and use substitution on it.
%%~na would only give you the file name without the extension. You have to use %%~nxa to get the file name and the extension.
If you want to set the file name to a variable and do something with each file, you have to use DelayedExpansion like this:
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
set d=%%~nxa
echo !d!
Do something with !d!
)
Or you could create a subroutine and not have to use a variable at all
#echo off&setlocal enabledelayedexpansion
for /f "tokens=*" %%a in (%1) do (
call :sub %%~nxa
)
goto :eof
:sub
%1 = your file name so do some processing on it.
How can I run this batch script on filenames with space and "(" ")"?
:Start
#Echo off
Set _SourcePath=C:\tifs\*.tif
Set _OutputPath=C:\txts\
Set _Tesseract="C:\Program Files\Tesseract-OCR\tesseract.exe"
:Convert
For /F "usebackq delims=" %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% %%A %_OutputPath%%%~nA
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
You have 2 problems:
1) You need some additional quotes.
2) You are using the wrong form of FOR. Your code is using the /F option with an unquoted IN() cluase. This attempts to read the contents of a file, which can't possibly work because your name includes a wildcard. I think you want a listing of .TIF files which is best done using the simple form of FOR (no /F option).
for %%A in (%_SourcePath%) do echo Converting "%%A"...&%_Tesseract% "%%A" "%_OutputPath%%%~nA"
I would change it to something like this:
:Start
#Echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
Set _SourcePath=C:\tifs\*.tif
Set _OutputPath=C:\txts\
Set "_Tesseract=C:\Program Files\Tesseract-OCR\tesseract.exe"
:Convert
For /F "usebackq delims=" %%A in (%_SourcePath%) Do Echo Converting %%A...&%_Tesseract% "%%A" "%_OutputPath%%%~nA"
:End
Set "_SourcePath="
Set "_OutputPath="
Set "_Tesseract="
Now, my answer might not work but I think it might give you enough hints to figure it out.