Page 1 of 2

error in for loop

Posted: 02 Jun 2012 16:03
by doscode
Hello,
what is wrong with my loop:

Code: Select all

CHCP 1250
SET "TAB= "

FOR /F "tokens=1,2 delims=\t" %%A IN (files.txt) DO (
      echo "%%A"
      echo "%%B"
      pause
      REM move "%bpath%/%file%" "%mypath%/%file%"
)


Generates:
Aktivní znaková stránka: 1250
Akce: MOVE
Pokračujte stisknutím libovolné klávesy...
" Jižní Čechy s"
"řed"

It is first line from the file. Problem is that the text should be on same line and that the "ř" is interpreted as tabulator (?) or why it is broken and why there is the tab on beginning? Do to remove it?

Note:
It is not parsed at all

If I make change to first line of input file, so

"* Jižní Čechy s"
"řed"

The asterisk should be 1st column...

Re: error in for loop

Posted: 02 Jun 2012 17:22
by aGerman
doscode wrote:FOR /F "tokens=1,2 delims=\t" %%A IN (files.txt) DO (

You have defined the delimiter to be either back slash or t. Why do you think the delimiter would be a tab?
I don't speak Czech but I guess the origin word was střed, wasn't it.
You should use the %TAB% variable instead of \t :wink:

Regards
aGerman

Re: error in for loop

Posted: 02 Jun 2012 17:30
by abc0502
I'm not sure but change the red parts and try it:
CHCP 1250
SET "TAB= "

FOR /F "tokens=1,2 delims=\t" %%A IN (files.txt) DO (
echo "%%A" "%%B"
pause
REM move "%bpath%\%file%" "%mypath%\%file%"
)


and as aGerman said use the %TAB%

Re: error in for loop

Posted: 02 Jun 2012 20:34
by Squashman
Was probably reading a Unix help page. A Backslash with a T represents a TAB.

Re: error in for loop

Posted: 02 Jun 2012 22:22
by Fawers
Since his %tab% has only one space, he doesn't even need to store it in a variable, nor define a delimiter, as a blank space is the default delimiter in a FOR /F.

Re: error in for loop

Posted: 03 Jun 2012 01:06
by doscode
Hi guys,
that is what I tried before I sent the post:

Code: Select all

    FOR /F "tokens=1,2 delims=%TAB%" %%A IN (files.txt) DO (
      echo "%%A %%B"
    ....


And that did not work. So I created %TAB% bariable, tested it as delimiter and then I added %%A %%B on one line ... with no success. So I then again made the changes and sent it to you.

Edit:
The structure of the command is whole bad, I need to parse lines first and then the tabs. I forgot it because some time did not spent with CMD.

SOLVED

Posted: 03 Jun 2012 01:22
by doscode
I searched google and found my own thread where we solved the problem:

Code: Select all

SET "TAB= "
for /F "skip=4 delims=pR tokens=1,2" %%a in ( 'reg query hkcu\environment /v temp' ) do set TAB=%%b

To create TAB instead 1st line use 2nd line to create correct tab delimiter.

This work to me fine!

Re: error in for loop

Posted: 03 Jun 2012 04:52
by aGerman
As far as I know XP is the last Windows version wherer REG QUERY returns the values tab-separated. That means your solution doesn't work for Vista and Win7.

Instead you should use your first line. If your text editor replaces tabs with spaces by default then you should open your code in notepad. The tab key creates a tab character in notepad.

Regards
aGerman

Re: error in for loop

Posted: 03 Jun 2012 07:17
by doscode
I would like to ask Aacini because it was his solution.

Can you please yet check my code?

Code: Select all

@echo off 
CHCP 1250 > NUL

SET action=PRINT
SET ext=bmp
SET bpath=Temp
SET mypath=INF
SET "TAB= "
for /F "skip=4 delims=pR tokens=1,2" %%a in ( 'reg query hkcu\environment /v temp' ) do set TAB=%%b
 
if "%1%"=="DELETE" SET action=DELETE
if "%1%"=="MOVE" SET action=MOVE
if "%1%"=="MOVE" SET ext=inf

echo Akce: %action%

if not exist INF/files.txt (
echo files.txt not found
pause
exit
)
if "%action%"=="MOVE" (
  FOR /F "eol= tokens=1,2 delims=%TAB%" %%A IN (./INF/files.txt) DO (
    if "%%A"=="*" (
     SET directory=%%B
     REM THIS FAILS:
     echo %directory%
     Echo I will move *.inf into... %%B     
     break
    )
    REM move "%bpath%/%file%" "%mypath%/%file%"
  )
)
 
pause

for %%I in (%bpath%/*.%ext%) do (   
 SET file=%%~nxI
 if "%action%"=="DELETE" (
  echo I delete
  echo %%~nxI
  del "%bpath%/%file%" /Q
  ) else if "%action%"=="MOVE" (   
    echo TARGET: %%B/%file%
    move /temp/*.inf ./INF/%%B
   ) else if "%action%"=="PRINT" ( echo %file%
          ) else (
              echo This program move files
              break
          )
 
 )
pause



I cannot display the %directory%, instead %%B works, but not outsite of the loop

Re: error in for loop

Posted: 03 Jun 2012 08:00
by foxidrive
You need to use delayed expansion and
!directory! instead of %directory%


If you text contains ! characters then you will almost certainly need to change the logic.

Re: error in for loop

Posted: 03 Jun 2012 08:41
by Squashman
Delayed expansion is his achillies heel.

Not using it in the last for loop either.

Re: error in for loop

Posted: 03 Jun 2012 09:18
by doscode
Thanks. I see. Because the variable is in the loop it must be delayed-expanded

What is this:

Action: MOVE
Files .inf will be move into... Jižní Čechy střed
SOURCE: "./Temp/16_000000.inf"
TARGET: "./INF/Jižní Čechy střed/16_000000.inf"
System cannot find the path
Press any key to continue:

Code: Select all

  ) else if "%action%"=="MOVE" (    
    echo SOURCE: "./%tpath%/!file!"
    echo TARGET: "./INF/!directory!/!file!"
    move "./%tpath%/!file!" "./INF/!directory!/!file!"
   )


As I check the path, so it should be OK. There is Chcp 1250, and the text is displayed OK. So where is problem?

Re: error in for loop

Posted: 03 Jun 2012 12:47
by doscode
I realized now that CMD uses backslashes :-O

Re: error in for loop

Posted: 04 Jun 2012 01:54
by doscode
Aacini comfirmed that aGerman is right. But

Code: Select all

REM Tab character
SET TAB=" "


Using normal %TAB% doesn't work for me in XP.
Do you normally use this technique on Windows 7?

Also, can you help me to replace extension?

Code: Select all

SET ext="bmp"
SET directory="temp"
SET exttochange="jpg"
SET directorytochange="jpegs"
SET path=".\%directory%\test.%ext%"
SET new_target = ????
Echo "%new_target%"

Should print:
/jpegs/test.jpg
instead of
/temp/test.bmp

Re: error in for loop

Posted: 04 Jun 2012 02:33
by foxidrive
SET ext="bmp"
SET directory="temp"
SET exttochange="jpg"
SET directorytochange="jpegs"
SET path=".\%directory%\test.%ext%"
SET new_target = ????
Echo "%new_target%"


You can simply change the variable names. Your example doesn't show what the problem is.

SET filepath=".\%directorytochange%\test.%exttochange%"



Don't use PATH for a variable name. It is a system variable.