Command Line Input Adaptation to Batch File

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
SteveBubendorf
Posts: 3
Joined: 22 May 2010 13:20

Command Line Input Adaptation to Batch File

#1 Post by SteveBubendorf » 22 May 2010 13:27

for %f in (*.lsp) do find /i "defun C:" %f>>defunC.txt

I have been using the line of code above at the Command prompt. I have many folders and it is proving very time consuming to start a command prompt in each and then to cut and paste the code to the command line. Can someone help me to turn it into a batch file which can use drag n drop of the required folders one at a time?

Thank you !!

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Command Line Input Adaptation to Batch File

#2 Post by aGerman » 22 May 2010 16:25

You could try something like that

Code: Select all

@echo off &setlocal
pushd "%~1"||goto :eof
for %%f in (*.lsp) do find /i "defun C:" "%%f">>%~dp0defunC.txt
popd


Regards
aGerman

SteveBubendorf
Posts: 3
Joined: 22 May 2010 13:20

Re: Command Line Input Adaptation to Batch File

#3 Post by SteveBubendorf » 22 May 2010 19:50

aGerman,

Thank you! It was the second percent sign required with the variables in the batch file that I was missing. I actually had it working before your response. I don't fully understand the remainder of your batch file, but it worked for me, so I am using yours, assuming that there is good reason for the code which you included that I didn't have.

Thanks, again !! :D

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Command Line Input Adaptation to Batch File

#4 Post by aGerman » 23 May 2010 14:04

SteveBubendorf wrote:[...]I don't fully understand the remainder of your batch file[...]


@echo off &setlocal
This turns off the command prompt and limits changing of the environment to the current batch window.

pushd "%~1"||goto :eof
pushd changes the working directory. In %~1 you will find the first argument (without enclosing quotes). In case you drag'n'drop a folder onto the batch file it contains the folder name. ||goto :eof means if pushd would fail then the batch processing would jump to the end of file, so it will quit.

for %%f in (*.lsp) do find /i "defun C:" "%%f">>%~dp0defunC.txt
Well, think you will know how it works. In %~dp0 you will find the drive and path of the own batch file.

popd
pushd and popd belong together. pushd changes the working directory and saves the origin directory. popd jumps back to the saved directory and flushes the memory for this value.

Hope that helps.
aGerman

SteveBubendorf
Posts: 3
Joined: 22 May 2010 13:20

Re: Command Line Input Adaptation to Batch File

#5 Post by SteveBubendorf » 23 May 2010 15:50

Thank you for the explanation! I was vaguely familiar with most of what you explained, just not enough so to have ever come up with them on my own. The "%~dp0" portion, however, I had never seen used before, and I had absolutely no idea what that portion was doing whatsoever. Thank you for taking the time to help me learn!

I am always amazed when I go to this, or most any other forum I've queried, how great people are to help beyond my wildest expectations. It seems like the "virtual world" is kinder and more helpful than the "real world" I live in.

Thanks, again!

Post Reply