Link to home
Start Free TrialLog in
Avatar of WO2015
WO2015

asked on

Create .bat File

Hello,

I am a beginner at working with and understanding .bat files. I need one to do a simple find and replace. I have googled and cant seem to figure it out. Here is what I am looking for it to do. I have a .txt file "testing1.txt". Within that file there are spaces and a quote mark I have to replace with a code. For example below is the line:
20170316  ''    ID'D CUST FIRST &:n

I need to have the spaces and quote mark to be replaced with *CC:W122. So it should read as I have below:
20170316*CC:W122ID'D CUST FIRST &:n

And the final step is I need to rename the "testing1.txt" to "example_MI.txt" and remove the "testing1.txt".

Thank you.
Avatar of ste5an
ste5an
Flag of Germany image

This is not possible without help of third-party tools. The Windows command shell does not have a REPLACE function or tool. Imho the "easiest" approach is to use CScript with VBA and do it there.
Avatar of WO2015
WO2015

ASKER

I am not sure what that means, can you help me create something that will work?
SOLUTION
Avatar of sarabande
sarabande
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WO2015

ASKER

Thank you Sara.  That for sure renamed the file but for some reason I have a file with hundreds of lines, rename it testing1.txt and run the program. It renames is but only brings back one line, and doesn't replace the correct thing. It for sure did something, but I am not sure what.
Avatar of WO2015

ASKER

I can easily go into my text file, use find and replace and take care of all instances (hundreds) in seconds. The thing I want though is not to have it be manual and have a .bat (or some type) of file be able to run.
I have a file with hundreds of lines
the sample above assumes the file testing1.txt has only 1 line.

and doesn't replace the correct thing.
i used the sample contents you posted above. the for command extracts first token %%i which should be 20170316 if the following letter is a space. the next token is %%j which should contain only " the final token %%k is all the rest of the line (in the sample beginning with D'D).

if the separators is not space and not tab or if there are many lines with different syntax, you surely should refine your requirements.

if the file may contain binary data the batch file can't read it properly.

Sara
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WO2015

ASKER

Thank you both for your reply. I have attached the testing1.txt, example_MI.txt results file, and the .bat file.  Changing to ">>" worked for multiple lines. It looks to replace the first blank section though and replace some data. You will see in the file where the "20170320  ''    DATE GOES HERE" is at. This will always be in the same column and that space space " space space space space is what needs to be *CC:W122.
Avatar of WO2015

ASKER

@echo off
REM abc.bat
for /f "tokens=1,2*" %%i in (testing1.txt) do if "%%i" NEQ "" echo %%i*CC:W122%%k  >>example_MI.txt
if exist example_MI.txt del testing1.txt
example_MI.txt
testing1.txt
Not sure if this is what you're after...

@echo off
REM abc.bat
for /f "tokens=1,2*" %%i in (testing1.txt) do if "%%i" NEQ "" echo %%i          *CC:W122     %%j             %%k  >>example_MI.txt

Open in new window

(1) i assume the 5 columns left of the date must be retained at theier fixed

(2) i also assume that the lines where the place holders are already replaced must be omitted from being updated again but must be retained in the new file as well?

(3) in the testing1.txt it looks as if the double quotes " actually are 2 single quotes '

if so, it is not

   
space space " space space space space

Open in new window



but

   
space space ' ' space space space space

Open in new window



is that correct?

generally, you can't do string handling pretty well with cmd statements.

it probably is easier to write a little c/c++ tool or cscript which does the job.

nevertheless i would try to find a solution if you could answer my questions and give a complete description of your requirements.

Sara
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of WO2015

ASKER

Thank you Bill,

That works PERFECT with replacing the data needed. The one problem I have is the command comes up and ask If I want to delete, when I select "Y" it deletes any files I have in that folder rather than just the testing1.txt.
Avatar of WO2015

ASKER

Never mind, I figured it out. I changed the last line to "del "%InFile%""
Avatar of WO2015

ASKER

THANK YOU ALL.
Welcome, glad that was useful.

~bp