error in for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
doscode
Posts: 174
Joined: 15 Feb 2012 14:02

error in for loop

#1 Post by doscode » 02 Jun 2012 16:03

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...

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

Re: error in for loop

#2 Post by aGerman » 02 Jun 2012 17:22

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

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: error in for loop

#3 Post by abc0502 » 02 Jun 2012 17:30

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%

Squashman
Expert
Posts: 3664
Joined: 23 Dec 2011 13:59

Re: error in for loop

#4 Post by Squashman » 02 Jun 2012 20:34

Was probably reading a Unix help page. A Backslash with a T represents a TAB.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: error in for loop

#5 Post by Fawers » 02 Jun 2012 22:22

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.

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

Re: error in for loop

#6 Post by doscode » 03 Jun 2012 01:06

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.

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

SOLVED

#7 Post by doscode » 03 Jun 2012 01:22

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!

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

Re: error in for loop

#8 Post by aGerman » 03 Jun 2012 04:52

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

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

Re: error in for loop

#9 Post by doscode » 03 Jun 2012 07:17

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: error in for loop

#10 Post by foxidrive » 03 Jun 2012 08:00

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.

Squashman
Expert
Posts: 3664
Joined: 23 Dec 2011 13:59

Re: error in for loop

#11 Post by Squashman » 03 Jun 2012 08:41

Delayed expansion is his achillies heel.

Not using it in the last for loop either.

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

Re: error in for loop

#12 Post by doscode » 03 Jun 2012 09:18

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?

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

Re: error in for loop

#13 Post by doscode » 03 Jun 2012 12:47

I realized now that CMD uses backslashes :-O

doscode
Posts: 174
Joined: 15 Feb 2012 14:02

Re: error in for loop

#14 Post by doscode » 04 Jun 2012 01:54

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: error in for loop

#15 Post by foxidrive » 04 Jun 2012 02:33

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.

Post Reply