DosTips.com

A Forum all about DOS Batch
It is currently 06 Feb 2016 23:38

All times are UTC-06:00




Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next
Author Message
 Post subject: error in for loop
PostPosted: 02 Jun 2012 16:03 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
Hello,
what is wrong with my loop:

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


Top
   
 Post subject: Re: error in for loop
PostPosted: 02 Jun 2012 17:22 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1933
Location: Germany
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


Top
   
 Post subject: Re: error in for loop
PostPosted: 02 Jun 2012 17:30 
Offline

Joined: 26 Oct 2011 22:38
Posts: 1007
Location: Egypt
I'm not sure but change the red parts and try it:
Quote:
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%


Top
   
 Post subject: Re: error in for loop
PostPosted: 02 Jun 2012 20:34 
Offline

Joined: 23 Dec 2011 13:59
Posts: 2979
Was probably reading a Unix help page. A Backslash with a T represents a TAB.


Top
   
 Post subject: Re: error in for loop
PostPosted: 02 Jun 2012 22:22 
Offline

Joined: 08 Apr 2012 17:11
Posts: 187
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.


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 01:06 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
Hi guys,
that is what I tried before I sent the post:

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


Top
   
 Post subject: SOLVED
PostPosted: 03 Jun 2012 01:22 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
I searched google and found my own thread where we solved the problem:

Code:
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!


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 04:52 
Offline
Expert

Joined: 22 Jan 2010 18:01
Posts: 1933
Location: Germany
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


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 07:17 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
I would like to ask Aacini because it was his solution.

Can you please yet check my code?

Code:
@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


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 08:00 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5355
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.


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 08:41 
Offline

Joined: 23 Dec 2011 13:59
Posts: 2979
Delayed expansion is his achillies heel.

Not using it in the last for loop either.


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 09:18 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
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:
  ) 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?


Top
   
 Post subject: Re: error in for loop
PostPosted: 03 Jun 2012 12:47 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
I realized now that CMD uses backslashes :-O


Top
   
 Post subject: Re: error in for loop
PostPosted: 04 Jun 2012 01:54 
Offline

Joined: 15 Feb 2012 14:02
Posts: 163
Aacini comfirmed that aGerman is right. But

Code:
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:
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


Top
   
 Post subject: Re: error in for loop
PostPosted: 04 Jun 2012 02:33 
Offline

Joined: 10 Feb 2012 02:20
Posts: 5355
Quote:
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.


Top
   
Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 19 posts ]  Go to page 1 2 Next

All times are UTC-06:00


Who is online

Users browsing this forum: Yahoo [Bot] and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited