Replace Text In File Using Windows Prompt - batch-file

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
)

Related

Using a batch script, how do I count files in a remote folder using a ip list/reference file and output it in a ip + filecount format?

What I want to achieve is have an output file that lists how many files are in a specific folder on a number of different remote machines ie(127.0.0.1 394files).
The issue I'm having is the output file generated only has 1 line in it, the last IP in ip.txt
To try and explain the code below, I'm creating a variable called 'testip' which should match the ip/machine I'm calling from ip.txt , this is created purely for the echo at the end, when I tried calling %%i or %%b at an echo, all that appears in the text file is %i or %b , as if its truncating 1 of the % characters. creating a separate variable (SET testip=%%b) got around that issue.
Next loop, it looks for the IP list in the ip.txt file and performs a count on the \test_scripts\ folder.
Then it should echo the IP it ran as well as the count number.
Any idea where this is going wrong?
Any feedback appreciated.
#ECHO OFF
SETLOCAL
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "delims=" %%b in (' type "C:\Users\testuser\Desktop\test_scripts\ip.txt" ' ) DO SET testip=%%b
for /f "delims=" %%i in (' type "C:\Users\testuser\Desktop\test_scripts\ip.txt" ' ) DO (
SET count=0
for %%o IN ('type "\\%%i\c$\Users\testuser\Desktop\test_scripts\*.*" ' ) DO (
SET /A count=count + 1
)
)
echo %testip% %count% >> output.txt
ENDLOCAL ENABLEDELAYEDEXPANSION
ENDLOCAL
Try this one instead. Note how delayed expansion is used. For more on the commands, run them with /? switch on cmdline. for instance. for /?
Also you will see that command type is not used at all.
#echo off
setlocal enabledelayedexpansion
set "location=C:\Users\testuser\Desktop\test_scripts\ip.txt"
for /f "delims=" %%i in (%location%) DO (
set "remote=\\%%i\c$\Users\testuser\Desktop\test_scripts\*"
set count=0
for %%x in (!remote!) do set /a count+=1
echo !remote! !count! >> output.txt
)
This is the same method as Gerhards answer but with two modifications. I've set the remote variable at the start of the script instead of repeating that on each iteration of the loop; and I'm outputting the IP address, instead of the remote path, with the file count.
#Echo Off
SetLocal EnableDelayedExpansion
Set "location=%UserProfile%\Desktop\test_scripts\ip.txt"
Set "remote=\\%%A\C$\Users\testuser\Desktop\test_scripts"
( For /F "Usebackq Delims=" %%A In ("%location%") Do (Set "#=0"
For %%B In ("%remote%\*") Do Set /A #+=1
Echo %%A !#!))>"output.txt"
This is similar except that it uses the Dir command to retrieve the file count. The Dir command can take account of hidden and system files etc.
#Echo Off
SetLocal EnableDelayedExpansion
Set "location=%UserProfile%\Desktop\test_scripts\ip.txt"
Set "remote=\\%%A\C$\Users\testuser\Desktop\test_scripts"
( For /F "Usebackq Delims=" %%A In ("%location%") Do (Set "_=" & Set "#="
For /F %%B In ('Dir "%remote%" /A-D/D') Do Set "#=!_!" & Set "_=%%B"
Echo %%A !#!))>"output.txt"
You can easily modify your location and remote paths on lines 3 and 4, ensuring that the \\%%A\ section remains unaltered.
In have not tested either script, nor have I made any attempt at error trapping etc. I'll leave that to you, should it be required.

Batch get filename from path

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

Batch - read array

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

Batch for loop cannot set variables

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.

Parse filenames with Space and ( ) in DOS batch

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.

Resources