Solved

Add lines to text files, and skip entire directories

Posted on 2006-11-22
14
291 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 250 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 250 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A short article about a problem I had getting the GPS LocationListener working.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

708 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now