script reading from .txt then pass along

October 12, 2010 at 14:25:18
Specs: Windows XP
I have a text file consist of a list of file names with relative path. I am trying to write a for loop that goes through each line then parse it and pass it onto an awk script.

filelist.txt:
Folder x\A\file1
Folder x\A\file2
Folder x\B\file3
...

run_script.bat:
awk -f awk_script %1.txt > \TST\%2\%3.tst
I have
for /f "delims=," %%l in (filename.txt) do call run_script %%l TEST %%~nl (something else should go in place of TEST)

I'd like the output of each file from a specific folder to go to another new folder.
e.g. Final result:
\TST\FromA\file1.tst
\TST\FromA\file2.tst
\TST\FromB\file3.tst

I have 2 questions:
1.
The for loop is outputing the correct information: %%f = Folder x\A\file1
However when %%f is passed to run_script the space in the folder name is causing problem. I tried putting "" around %%l (call run_script "%%l" TEST %%~nl) but that does not work either. I prefer not having to remove all white space from my folder names.
2.
How to do some sort of IF statement to change the destination folder name?
If %%l contains "A", %outfolder = "FromA"
etc

Thank you very much.



See More: script reading from .txt then pass along

Report •


#1
October 12, 2010 at 14:43:02
Are you using %%f of %%l? double check, you stated both in your question.
If you still have no luck try setting a variable as %%f.

As for your second question, try this:

ECHO %%f | FIND "a" >Nul
IF "%ErrorLevel%"=="0" (
do something
) ELSE (
do something else
)


Can I put a question here?


Report •

#2
October 13, 2010 at 08:27:19
Thanks.

They are %%f (I was tweeking the variable name and posted in some middle state), it is consistent in my actual script.

Can you explain what does "|" do? I see it in many scripts but am unable to find what it is for.

This is my script so far:
FOR /f "delims=" %%f IN (filelist.txt) DO (
ECHO %%f | find "Orca" > Nul
IF "%Errorlevel%"=="0" (
CALL run_script %%f OrcaArt_Folder %%~nf
) ELSE (
CALL run_script %%f TEST_FOLDER %%~nf
)
)

However the IF statement is always true. screen is showing IF "0" == "0"

And back to the other question, is there a way to pass %f to another script when %f is a path with space in directory name (e.g. "A FOLDER\SOME NAME\test.txt") as arguement to another script and keep the entire path intact.

Thank you very much.


Report •

#3
October 13, 2010 at 08:55:27
Very strange. If I go through the FIND and IF statement line by line myself it works perfectly. but not running the script.

It seems to be dependent on the order of input.
Using this script:
FOR /f "delims=" %%f IN (filelist.txt) DO (
ECHO %%f | find "Orca"
ECHO Errorlevel = %ERRORLEVEL%
IF ERRORLEVEL 0 (
ECHO FOUND
) ELSE (
ECHO NOT FOUND
)
)

Output is this:
C:\Data Conversion Scripts\Part1>files

C:\Data Conversion Scripts\Part1>FOR /F "delims=" %f IN (filelist.txt) DO (
ECHO %f | find "Orca"
ECHO Errorlevel = 1
IF ERRORLEVEL 0 (ECHO FOUND ) ELSE (ECHO NOT FOUND )
)

C:\Data Conversion Scripts\Part1>(
ECHO Data\folder\Orca\anotherfile | find "Orca"
ECHO Errorlevel = 1
IF ERRORLEVEL 0 (ECHO FOUND ) ELSE (ECHO NOT FOUND )
)
Data\folder\Orca\anotherfile
Errorlevel = 1
FOUND

C:\Data Conversion Scripts\Part1>(
ECHO Data\folder\morefolder\morefolder\morefolder\somename | find "Orca"
ECHO Errorlevel = 1
IF ERRORLEVEL 0 (ECHO FOUND ) ELSE (ECHO NOT FOUND )
)
Errorlevel = 1
FOUND

It appears to me that the the execution order of the statement is not correct. Does the statement inside DO not execute line by line after each line's completion?

Thanks.


Report •

Related Solutions

#4
October 13, 2010 at 13:33:32
Ahh, sorry for the delay. I was at school =).

The "|" is called a pipe. It is used to pass a command on to another. For example, checking variables for a certain letter.

 ECHO %Variable% | FIND "a" 

It write the variable, then passes it along to FIND to check for the specified letter.

----

For your next question, use delayed expansion.

SETLOCAL EnableDelayedExpansion

Stick this somewhere before the FOR command.

Good luck.

Can I put a question here?


Report •

Ask Question