Solved

Add lines to text files, and skip entire directories

Posted on 2006-11-22
14
292 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
topping1 challenge 7 72
Non-recursive backtracking, using a stack 1 96
Advice in Xamarin 21 47
Copy dir and files with robocopy 2 30
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
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…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

929 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

12 Experts available now in Live!

Get 1:1 Help Now