batch job to do string replacement of contents.lst file in all subfolders

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

batch job to do string replacement of contents.lst file in all subfolders

#1 Post by goodywp » 12 Jul 2018 13:35

Hi all,

I have a new task to do a batch job to replace all subfolders's contents.lst files
Here is the example:

C:\level1\level2\level3
\level3a\level4
\level4a\level5
\level3b
Now the requirement is that under level2 folder, there is a contents.lst file which mainly display the list of files and folders under level2

same as level3 subfolder, level3a, level3b... level4, level4a, level5, they all have a contents.lst files

Now I would like to replace all these contents.lst files under different subfolders after level2
the content I like to replace as below code:

Code: Select all

@echo off
set optn=T582-08878-0101
set nptn=T592-08917-0100

@echo off &setlocal
set "search=%optn%"
set "replace=%nptn%"
set "textfile=contents.lst"
set "newfile=indesnew.txt"
(for /f "delims=" %%i in (%textfile%) do (
    set "line=%%i"
    setlocal enabledelayedexpansion
    set "line=!line:%search%=%replace%!"
    echo(!line!
    endlocal
))>"%newfile%"
del %textfile%
rename %newfile%  %textfile%

Since they all have optn value in these contents.lst files before replace, after replace they all should have nptn value as the above code does.... it works as expected.

Now question will be how we can have a batch script to do them all in one script, instead of doing one level as the above code did...
Thanks
Last edited by goodywp on 16 Jul 2018 12:44, edited 2 times in total.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: new job to do a batch of replacement of contents.lst

#2 Post by Squashman » 12 Jul 2018 14:32

I would parse the output of the DIR command with a FOR /F command to get a listing of all the files.

Code: Select all

FOR /F "delims=" %%G IN ('dir /a-d /b /s contents.lst') DO........

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: new job to do a batch of replacement of contents.lst

#3 Post by goodywp » 13 Jul 2018 11:47

Squashman wrote:
12 Jul 2018 14:32
I would parse the output of the DIR command with a FOR /F command to get a listing of all the files.

Code: Select all

FOR /F "delims=" %%G IN ('dir /a-d /b /s contents.lst') DO........
Thanks and tried this following code not working as expected for all subfolders' replacement

Code: Select all

@Echo Off
call C:\auto_pkg_build\Scripts\scheme_replace\varconfig.cmd
call C:\auto_pkg_build\Scripts\scheme_replace\get_src.cmd

set optn=%optn: =%
set nptn=%nptn: =%
set opkg=%opkg: =%
set npkg=%npkg: =%

pushd C:\auto_pkg_build\Workspace\Build_pkg\%npkg%\%nptn%

FOR /F "delims=" %%G IN ('dir /a-d /b /s contents.lst') DO (

Set "search=%optn%"
Set "replace=%nptn%"
Set "textfile=contents.lst"
Set "newfile=indesnew.txt"
(
	For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
		If "%%B"=="" (
			Echo=
		) Else (
			Set "line=%%B"
			SetLocal EnableDelayedExpansion
			Set "line=!line:%search%=%replace%!"
			Echo=!line!
			EndLocal
		)
	)
)>"%newfile%"
Del "%textfile%"
Ren "%newfile%" "%textfile%"
)
How to use %%G in the DO clause in the above code??
only the current contents.lst has been replaced, all the subfolder's contents.lst not replaced at all

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: new job to do a batch of replacement of contents.lst

#4 Post by Squashman » 13 Jul 2018 17:05

You are forgetting about delayed expansion. The search, replace, newfile and textfile variables can be set outside the code block. No need to have them nested inside the FOR block.

Shouldn't you be using %%G with the FINDSTR command? That is my best guess. I would think you would better understand your own code logic

goodywp
Posts: 250
Joined: 31 Jul 2017 09:57

Re: new job to do a batch of replacement of contents.lst

#5 Post by goodywp » 16 Jul 2018 06:58

I tried use below code and end up after line 22
11
22
The system cannot find the path specified.
C:\auto_pkg_build\Workspace\Build_pkg\T592-08878-0101_Link2500_TCPX_NARInfra_GEN
_PROD\T592-08878-0101\*, Are you sure (Y/N)?

Code: Select all

@Echo Off
SETLOCAL EnableDelayedExpansion
call C:\auto_pkg_build\Scripts\scheme_replace\varconfig.cmd
call C:\auto_pkg_build\Scripts\scheme_replace\get_src.cmd

set optn=%optn: =%
set nptn=%nptn: =%
set opkg=%opkg: =%
set npkg=%npkg: =%

pushd C:\auto_pkg_build\Workspace\Build_pkg\%npkg%\%nptn%
echo 11
FOR /F "delims=" %%G IN ('dir /a-d /b /s contents.lst') DO (
echo 22
		Set "search=%optn%"
		Set "replace=%nptn%"
		Set "textfile=contents.lst"
		Set "newfile=indesnew.txt"
		(
echo 44
			For /F "Tokens=1* Delims=:" %%A In ('FindStr /N "^" "%textfile%"') Do (
				If "%%B"=="" (
					Echo=
				) Else (
echo 55
					Set "line=%%B"
					SetLocal EnableDelayedExpansion
					Set "line=!line:%search%=%replace%!"
					Echo=!line!
					EndLocal
				)
echo 66
			)
		)>"%newfile%"
		Del "%textfile%"
		Ren "%newfile%" "%textfile%"
echo 99	
	)

Post Reply