Add lines to text files, and skip entire directories

Hi, I've had this batch file on here a couple times, but I need another addition to it:

setlocal
for /r k:\ %%a in (application.cfm) do if exist %%a type line.txt >> "%%a"
goto :eof

This will search through a directory tree (here specified as the k:\ drive). It will add the text in line.txt to the eof of every Application.cfm that already exists. Now, I need an addition to this script to entirely skip specific directories. ie, I don't want this script to change anything in the CFIDE directory. Any ideas?
firstbankakAsked:
Who is Participating?
 
RaymunConnect With a Mentor Commented:
Not a problem. It now works with spaces is the folder names. Try this:

@echo off
setlocal enabledelayedexpansion

set Tmp=%1\tmp
set FolderName=%2
set DirsToSkip=dirs.txt
set FilesToModify=files.txt
set LineToAdd=blah

:Loop
if not %3.==. (
      set FolderName=!FolderName! %3
      shift /2
      goto :Loop
)

for /f "tokens=1,2 delims=." %%a in ('dir /b "!FolderName!"') do (
      echo %%a>%Tmp%
      findstr /i /x /g:%DirsToSkip% %Tmp% > nul
      
      if not !errorlevel!==0 (

              if %%b.==. (
                           call %1\test.bat %1 !FolderName!\%%a
                ) else (
                  echo %%a.%%b>%Tmp%
                  findstr /i /x /g:%FilesToModify% %Tmp% > nul

                           if !errorlevel!==0 (
                          echo %LineToAdd%>>!FolderName!\%%a.%%b
                           )
                )
           )
)

if exist %Tmp% del %Tmp%

endlocal

The parameters are different now. To run it, enter:

x.bat %cd% k:
0
 
chhokra_expertCommented:
how about this?

dir /s/b application.cfm > fileslist.txt
find /v CFIDE < fileslist.txt > fileslist_filtered.txt
for / f %%a in (fileslist_filtered.txt) do type line.txt >> %%a
0
 
firstbankakAuthor Commented:
chhokra_expert, almost there. The filelist_filtered.txt file contains all the files that I want to edit. This works perfectly. However the syntax of the for loop is generating some errors. Also, I would like to change this for loop s.t. instead of specifying line.txt, I can simply insert the text in the for loop that I want added to said files.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RaymunCommented:
Hello, try this:

@echo off

set LineToAdd=blah

for /f "tokens=1,2 delims=." %%a in ('dir /b %1') do (
      if not %%a==CFIDE (
            if %%b.==. (
                  call %2\test.bat %1\%%a %2
            ) else (
                  if %%b==cfm (
                        echo %LineToAdd%>>%1\%%a.%%b
                  )
            )
      )
)

endlocal

You have to give it two parameters. The first parameter is the root directory of where to start searching for the .cfm files. The second is the directory of the batch file itself. Let's say the batch file is called x.bat. So to run it, enter in the command line:

x.bat k: %cd%

It is recursive. So if your k: directory tree is many levels deep it may eat up a chunk of your resources.

-Ray
0
 
RaymunCommented:
I forgot to mention, the root directory has to be an absolute path, not relative

.. and you can remove 'endlocal' (typo)
0
 
firstbankakAuthor Commented:
Thanks Raymun, I will give this a try this morning and let you know how it runs.
0
 
firstbankakAuthor Commented:
This looks like it is working well, but I would like it to ONLY alter Application.cfm files.
0
 
RaymunCommented:
Whoops I missed that part. Try this:

@echo off
setlocal enabledelayedexpansion

set Tmp=%2\tmp
set DirsToSkip=dirs.txt
set FilesToModify=files.txt
set LineToAdd=blah

for /f "tokens=1,2 delims=." %%a in ('dir /b %1') do (
      echo %%a>%Tmp%
      findstr /i /x /g:%DirsToSkip% %Tmp% > nul
      
      if not !errorlevel!==0 (

              if %%b.==. (
                  echo 1
                           call %2\test.bat %1\%%a %2
                ) else (
                  echo %%a.%%b>%Tmp%
                  findstr /i /x /g:%FilesToModify% %Tmp% > nul

                           if !errorlevel!==0 (
                                echo %LineToAdd%>>%1\%%a.%%b
                           )
                )
           )
)

if exist %Tmp% del %Tmp%

endlocal

Create dirs.txt and files.txt in the same directory as the batch. In dirs.txt, list all the folder names you want to skip, one per line. So just list CFIDE. In files.txt, list all the file names you want to modify, one per line. So just list application.cfm. It is not case-sensitive, so Application.cfm, APPLICATION.CFM, etc will get modified as well. Lastly, if there are folders in the root directory with spaces in the folder name, it will crash. If this is an issue, just let me know and I'll change it. Good luck.
0
 
firstbankakAuthor Commented:
Yep, there are folders in the root with spaces. Thanks for your help Raymun.
0
 
chhokra_expertConnect With a Mentor Commented:

so the problem that was happening in the for was that there is a "space" between the / and f that shouldn't have been there. sorry bt that.

Also, to specify the string directly, use echo instead of type

-------------- begin copy here --------------
dir /s/b application.cfm > fileslist.txt
find /v CFIDE < fileslist.txt > fileslist_filtered.txt
for /f %%a in (fileslist_filtered.txt) do echo "put your desired string here" >> %%a
--------------- end copy here ---------------

if ur calling the last line from the shell directly, then you don't need the extra % in front of the vars
for /f %a in (dir_list.txt) do @echo && @echo "yeah baby" >> %a

ps: i like the intermediate file because it helps me debug and track back if something got messed up ;)
and i think this should be faster because it avoids interpreting braches in loops

cheers,
kg
0
 
chhokra_expertCommented:
just a note.. you DON'T want to use the double quotes.. they are interpreted literally
0
 
firstbankakAuthor Commented:
Thankyou both for your posts. I will test out these batch files and get back with you today. Cheers.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.