Link to home
Start Free TrialLog in
Avatar of firstbankak
firstbankakFlag for United States of America

asked on

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?
Avatar of chhokra_expert
chhokra_expert

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
Avatar of firstbankak

ASKER

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.
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
I forgot to mention, the root directory has to be an absolute path, not relative

.. and you can remove 'endlocal' (typo)
Thanks Raymun, I will give this a try this morning and let you know how it runs.
This looks like it is working well, but I would like it to ONLY alter Application.cfm files.
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.
Yep, there are folders in the root with spaces. Thanks for your help Raymun.
ASKER CERTIFIED SOLUTION
Avatar of Raymun
Raymun

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
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
just a note.. you DON'T want to use the double quotes.. they are interpreted literally
Thankyou both for your posts. I will test out these batch files and get back with you today. Cheers.