1.

Solve : Find the row location of a text?

Answer»

Hi,

I am very new to MS DOS. I am trying to do the following:

I have multiple files in folder C:\Misc. I need to move all files from C:\Misc to C:\Misc\Option that have the word "Application" in the first ROW.

E.g.
File A:
User
Application

File B:
Application
Game

In this example I want to move File B from C:\Misc to C:\Misc\Option as it has the word "Application" in the first row.

Can someone please show me how to write a dos COMMAND that will do the above? I am very new to DOS and thus looking for help.

Thanks
Rohan
Quote from: rohanverma on September 06, 2008, 01:04:04 PM

the word "Application" in the first row.

I will work on this tomorrow. In the meantime, you should know that the correct word is "line", not "row".
for /f "tokens=1*" %a in ('findstr /M /I Application *') do move %a %systemdrive%\yourfolder\%a


ok , the command you want is findstr , using the /M switch to display only the filename of the files that contain a match , using the /I switch to signify that is not case-sensitive, Application is your search term, * signifys all files in the current folder , using the FOR Command , with the /F switch .. , you are looking for %a in the output of findstr /M /I Application * the next part.. of the output that is a match, move %a (the matching output filename) to %systemdrive% witch is normally C: , \yourfolder\%a , %a is the filename.. so if you had a file with Application in it called myfile.txt , %a would equal myfile.txt , so the above will copy all matching files with Application Written in it, its important to remember.. using %a in a batch file would need an additonal percentile symbol so instead in a batch file the command would look similar to this..
for /f "tokens=1*" %%a in ('findstr /M /I Application *') do move %%a %systemdrive%\yourfolder\%%adiablo, the OP specified that "application" should be in the first "row" (line) in order for the move to happen, and in FACT gave examples illustrating that if "application" was present in a file, but not in the first line, then that file should not be moved.
hello everyone,

thank you for helping me out. Yes, the word "Application" should be in first line in order to move the file. Any ideas on how I can accomplish that?

Your post seems to have gotten lost in the shuffle.

This may help you out:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%i in (C:\Misc\*.txt) do (
set /p var=<%%i
if /i !var!==Application move %%i C:\Misc\Option\%%~nxi
)

You didn't mention an extension for your files. The code uses .txt, but it can be changed.

thanks Sidewinder, I tried using your code but it didn't work. I created a bat file with your code. Am i doing something wrong?Quote
I tried using your code but it didn't work. I created a bat file with your code. Am i doing something wrong?

Hard to tell from here. Did you re-type the code or copy/paste it? Any error messages? What are they? What extensions do your files have? The code was tested and checked out on a XP machine. Should run easily on Win2003.

Try removing the @echo off line from the batch file. You may be able to debug yourself or post the console output and we'll take a look.



PS. I tested with the FileA and FileB exactly as you posted. Is that not correct?hi sidewinder,

I tried to use your code in the command prompt but the file transfer doesn't work.

Here is your code modified for command prompt:

C:\misc\test1>for %i in (*.txt) do (set /p var=<%i if /i !var!==Application
move %i c:\va\%~nxi)

Here is what I get in the command prompt window on running the above code:

C:\misc\test1>(set /p var= if /i !var!==Application move testing.txt c:\misc\t
esting.txt 0if /i !var!==Application move testing.txt c:\misc\testing.txt

C:\misc\test1>(set /p var= if /i !var!==Application move testing2.txt c:\misc\
testing2.txt 0if /i !var!==Application move testing2.txt c:\misc\testing2.txt

It looks like the code is unable to identify if the word "Application" is in the first line or not.I KEEP telling myself to be more precise. The posted code was not meant to be run at the command prompt. It was DESIGNED as a batch file.

Quote
I created a bat file with your code.
So what happened when you ran the code as a batch file?

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%i in (C:\Misc\*.txt) do (
set /p var=<%%i
if /i !var!==Application move %%i C:\Misc\Option\%%~nxi
)

You should be able to copy and paste the code into your favorite editor. Save the script with a bat extension and run from the command prompt as scriptname.bat



thanks Sidewinder, this code worked. Now what if I had the same file but the first line had multiple columns separated by a tab how would we have to modify the code. e.g.

Application3586NameTgi

How would I change the code to use this? Your code is extremely helpful.

A big thanks once again.
As written, var takes on the value of the first line of the file, including the spaces. This works if application is always the first word and starts in the first position:

Code: [Select]@echo off
setlocal enabledelayedexpansion
for %%i in (C:\temp\*.txt) do (
set /p var=<%%i
set var=!var:~0,11!
if /i !var!==Application move %%i C:\temp\Option\%%~nxi
)

If you are scanning the line for the word application in any position, you'll need to set up a loop within the for loop to do the scan. There are other scripting languages that make this easy. Batch code, not so much.





thank you thank you thank you Sidewinder.

Your code did exactly what I was looking for, thank you so much for all your help. It works just fine.





Discussion

No Comment Found