Computer Hope

Microsoft => Microsoft DOS => Topic started by: cellyn on December 02, 2016, 03:01:21 AM

Title: Problem using batch script
Post by: cellyn on December 02, 2016, 03:01:21 AM
hi, i am new user of windows batch script. I am having problem to write a script to automate the repetitive programme. In the batch script, I need to the replace the input filename by another filename, can it be read directly from the filename itself or we do need to specify in the script? Because there are a lot of files, I can't specify one by one. Anyone can help?
Thank you.
Title: Re: Problem using batch script
Post by: Squashman on December 02, 2016, 08:08:34 AM
Are you saying you have a list of file names you need to process with a program?

Please post any code you have so far so we can understand the context of what you are trying to accomplish.
Title: Re: Problem using batch script
Post by: cellyn on December 02, 2016, 08:54:37 PM
@echo off &setlocal
set "search= "xyz"
set "replace="abc"
set "textfile=text123.txt"
set "newfile="text321.txt"


(for /f "delims=" %%i in ('findstr /n "search" "%textfile%"') do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=%line:xyz=abc%"
    echo(%line%
    endlocal
))>"%newfile%"

Thanks for your reply, Squashman. This is the code I am still trying on. Yes, I have a list of filenames which need to goes through the same process, so required to replace the filename in the batch one by one and produce a bunch of output files. Is it possible to do that? I've tried to replace just the filename in the script and save to a new file but it is failed. Can you help me on these? I have no idea how the batch script work and still trying hard to learn. Thank you.