| Author |
Message |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 error in for loop
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...
|
| 02 Jun 2012 16:03 |
|
 |
|
aGerman
Expert
Joined: 22 Jan 2010 18:01 Posts: 1395 Location: Germany
|
 Re: error in for loop
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 s třed, wasn't it. You should use the %TAB% variable instead of \t Regards aGerman
|
| 02 Jun 2012 17:22 |
|
 |
|
abc0502
Joined: 26 Oct 2011 22:38 Posts: 1006 Location: Egypt
|
 Re: error in for loop
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%
|
| 02 Jun 2012 17:30 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1002
|
 Re: error in for loop
Was probably reading a Unix help page. A Backslash with a T represents a TAB.
|
| 02 Jun 2012 20:34 |
|
 |
|
Fawers
Joined: 08 Apr 2012 17:11 Posts: 187
|
 Re: error in for loop
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.
|
| 02 Jun 2012 22:22 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 Re: error in for loop
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.
|
| 03 Jun 2012 01:06 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 SOLVED
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!
|
| 03 Jun 2012 01:22 |
|
 |
|
aGerman
Expert
Joined: 22 Jan 2010 18:01 Posts: 1395 Location: Germany
|
 Re: error in for loop
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
|
| 03 Jun 2012 04:52 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 Re: error in for loop
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
|
| 03 Jun 2012 07:17 |
|
 |
|
foxidrive
Joined: 10 Feb 2012 02:20 Posts: 2478
|
 Re: error in for loop
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.
|
| 03 Jun 2012 08:00 |
|
 |
|
Squashman
Joined: 23 Dec 2011 13:59 Posts: 1002
|
 Re: error in for loop
Delayed expansion is his achillies heel.
Not using it in the last for loop either.
|
| 03 Jun 2012 08:41 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 Re: error in for loop
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?
|
| 03 Jun 2012 09:18 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 Re: error in for loop
I realized now that CMD uses backslashes :-O
|
| 03 Jun 2012 12:47 |
|
 |
|
doscode
Joined: 15 Feb 2012 14:02 Posts: 156
|
 Re: error in for loop
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
|
| 04 Jun 2012 01:54 |
|
 |
|
foxidrive
Joined: 10 Feb 2012 02:20 Posts: 2478
|
 Re: error in for loop
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.
|
| 04 Jun 2012 02:33 |
|
|