AutoHotkey Community

It is currently May 27th, 2012, 5:54 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: file loop anomaly
PostPosted: November 15th, 2005, 3:27 am 
Offline

Joined: July 30th, 2005, 3:49 pm
Posts: 42
Hi

Perhaps someone can help.

I've got a loop that changes the names
of a directory's files, prepending the
digit 2 to the current names.

Code:
   Loop, *.jpg
      {
      newName = 2%A_LoopFileName%
      FileMove, %A_LoopFileName%, %newName%
      }

What's happening is that the first (alphabetically)
file in the directory is being hit twice by this loop.

Example: A directory initially contains these
three files:

0012.jpg
0013.jpg
0014.jpg

After passing thru the loop, it contains these three files:

20013.jpg
20014.jpg
220012.jpg

I put some debugging code into the loop, like this:

Code:
   Loop, *.jpg
      {
      OutputDebug, index is %A_Index% -- filename is %A_LoopFileName%
      newName = 2%A_LoopFileName%
      FileMove, %A_LoopFileName%, %newName%
      }

For the preceding example, the debugging output
looks like this:

index is 1 -- filename is 0012.jpg
index is 2 -- filename is 0013.jpg
index is 3 -- filename is 0014.jpg
index is 4 -- filename is 20012.jpg

Note that, although there are just 3 files,
the body of the loop executes four times;
thus the doubled 2-ing of the first file.

Have I bumped into an AHK file loop bug ?
Or is something else going on that my AHK-newbie
mind has not grokked correctly ?

tia

-- stan


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 9:12 am 
Offline

Joined: January 31st, 2005, 9:50 am
Posts: 3910
Location: Bremen, Germany
I can't reproduce the problem. It is working on my machine if the code is in the same dir as the files.
I use AHK 1.0.40.06 on WinXP Pro SP2

Your code could be shorter:
Code:
Loop, *.jpg
    FileMove, %A_LoopFileName%, 2%A_LoopFileName%


If you want to have the code in a different dir:
Code:
Loop, C:\AHK\*.jpg
    FileMove, %A_LoopFileDir%\%A_LoopFileName%, %A_LoopFileDir%\2%A_LoopFileName%

_________________
Ciao
toralf
Image


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 12:54 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
Although I can reproduce this, I think it's normal behavior due to a quirk in the Windows API. Apparently, MoveFile() can cause FindNextFile() to find the same file twice, once with its old name and again with its new name.

I don't see a way to easily fix this in the program, so I'd suggest a work-around such as the following, which builds a list of files and then parses it line by line afterward:
Code:
FileList =
Loop *.jpg
   FileList = %FileList%%A_LoopFileName%`n
Loop, parse, FileList, `n
   FileMove, %A_LoopField%, 2%A_LoopField%


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 1:50 pm 
Offline

Joined: February 7th, 2005, 11:11 am
Posts: 192
Location: Munich, Germany
Chris wrote:
Code:
FileList =
Loop *.jpg
   FileList = %FileList%%A_LoopFileName%`n
Loop, parse, FileList, `n
   FileMove, %A_LoopField%, 2%A_LoopField%


There's another advantage to chris' code: The order in which Loop reads the files is not neccessesary the one you are seeing or expecting. It may happen that you see File1, File2, File3 ... but Loop is reading File3, File1, File2, ...
This happens with FAT partitions under Win 98 sometimes. If you want to be sure that the files are read in ascending order use a Sort before:
Code:
FileList =
Loop *.jpg
   FileList = %FileList%%A_LoopFileName%`n
Sort, FileList
Loop, parse, FileList, `n
   FileMove, %A_LoopField%, 2%A_LoopField%

_________________
Peter

Wisenheiming for beginners: KaPeGe (German only, sorry)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 3:42 pm 
Offline

Joined: July 30th, 2005, 3:49 pm
Posts: 42
Hi Toralf

Interesting that the problem did not show up
on your system.

RE the shorter code: that was my original
code, but I two-lined it just in case
there was some sort of filename change
timing problem related to having
%A_LoopFileName% twice in the renaming line.

-- stan


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 3:49 pm 
Offline

Joined: July 30th, 2005, 3:49 pm
Posts: 42
Hi Chris

Thanks for the workaround code.

I'd assumed that the AHK code was doing what
the workaround does -- build a file list -- when
it hit the

Code:
loop, *.jpg


statement.

-- stan


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 15th, 2005, 3:51 pm 
Offline

Joined: July 30th, 2005, 3:49 pm
Posts: 42
Hi Peter

Thanks for the sort variant.

-- stan


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 3 guests


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

Search for:
Powered by phpBB® Forum Software © phpBB Group