?
Solved

Add lines to text files, and skip entire directories

Posted on 2006-11-22
14
Medium Priority
?
297 Views
Last Modified: 2010-04-16
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?
0
Comment
Question by:firstbankak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
14 Comments
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 17998835
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
 

Author Comment

by:firstbankak
ID: 17999017
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
 
LVL 4

Expert Comment

by:Raymun
ID: 18000762
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 4

Expert Comment

by:Raymun
ID: 18000787
I forgot to mention, the root directory has to be an absolute path, not relative

.. and you can remove 'endlocal' (typo)
0
 

Author Comment

by:firstbankak
ID: 18020428
Thanks Raymun, I will give this a try this morning and let you know how it runs.
0
 

Author Comment

by:firstbankak
ID: 18020555
This looks like it is working well, but I would like it to ONLY alter Application.cfm files.
0
 
LVL 4

Expert Comment

by:Raymun
ID: 18030725
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
 

Author Comment

by:firstbankak
ID: 18031148
Yep, there are folders in the root with spaces. Thanks for your help Raymun.
0
 
LVL 4

Accepted Solution

by:
Raymun earned 1000 total points
ID: 18031646
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
 
LVL 4

Assisted Solution

by:chhokra_expert
chhokra_expert earned 1000 total points
ID: 18038584

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
 
LVL 4

Expert Comment

by:chhokra_expert
ID: 18038643
just a note.. you DON'T want to use the double quotes.. they are interpreted literally
0
 

Author Comment

by:firstbankak
ID: 18038860
Thankyou both for your posts. I will test out these batch files and get back with you today. Cheers.
0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Progress
Starting up a Project
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question