Sign in to follow this  
Followers 0
nnps

Pad file name with zeros (fixed length)

5 posts in this topic

I have been a lurker for a while and have picked up some great tips from all of the experts!!

Now I just want an opinion or maybe a tidy up of my code!

What I am trying to do is pad a file name with zeros. It will always need to be 12 characters, but

the original file name may be something like 5.jpg. I need it to be 000000000005.jpg.

This is what I have so far and works....

#include <string.au3>

HotKeySet("#t", "test")

While 1

Sleep(50)

WEnd

func test()

$search = FileFindFirstFile("c:\temp2\*.*")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

$len = StringLen($file)

if $len = 5 Then

FileMove ( "c:\temp2\" & $file, "C:\temp20000000000" & $file , 1)

EndIf

If $len = 6 Then

FileMove ( "c:\temp2\" & $file, "C:\temp2000000000" & $file , 1)

EndIf

If $len = 7 Then

FileMove ( "c:\temp2\" & $file , "C:\temp200000000" & $file , 1)

EndIf

If $len = 8 Then

FileMove ( "c:\temp2\" & $file , "C:\temp20000000" & $file , 1)

EndIf

If $len = 9 Then

FileMove ( "c:\temp2\" & $file , "C:\temp2000000" & $file , 1)

EndIf

If $len = 10 Then

FileMove ( "c:\temp2\" & $file , "C:\temp200000" & $file , 1)

EndIf

If $len = 11 Then

FileMove ( "c:\temp2\" & $file , "C:\temp20000" & $file , 1)

EndIf

WEnd

FileClose($search)

EndFunc

any input to tidy this up, or is this OK?

Thanks from an extreme noob :P

jp

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I have been a lurker for a while and have picked up some great tips from all of the experts!!

Now I just want an opinion or maybe a tidy up of my code!

What I am trying to do is pad a file name with zeros. It will always need to be 12 characters, but

the original file name may be something like 5.jpg. I need it to be 000000000005.jpg.

This is what I have so far and works....

#include <string.au3>

HotKeySet("#t", "test")

While 1

Sleep(50)

WEnd

func test()

$search = FileFindFirstFile("c:\temp2\*.*")

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

$len = StringLen($file)

if $len = 5 Then

FileMove ( "c:\temp2\" & $file, "C:\temp20000000000" & $file , 1)

EndIf

If $len = 6 Then

FileMove ( "c:\temp2\" & $file, "C:\temp2000000000" & $file , 1)

EndIf

If $len = 7 Then

FileMove ( "c:\temp2\" & $file , "C:\temp200000000" & $file , 1)

EndIf

If $len = 8 Then

FileMove ( "c:\temp2\" & $file , "C:\temp20000000" & $file , 1)

EndIf

If $len = 9 Then

FileMove ( "c:\temp2\" & $file , "C:\temp2000000" & $file , 1)

EndIf

If $len = 10 Then

FileMove ( "c:\temp2\" & $file , "C:\temp200000" & $file , 1)

EndIf

If $len = 11 Then

FileMove ( "c:\temp2\" & $file , "C:\temp20000" & $file , 1)

EndIf

WEnd

FileClose($search)

EndFunc

any input to tidy this up, or is this OK?

Thanks from an extreme noob :P

jp

What you really want is a string length of 16 (12 + '.jpg')

If StringLen($File) < 16 Then
   Do
      $File = '0' & $File
   Until StringLen($File) = 16
EndIf

FileMove ( "c:\temp2\" & $file , "C:\temp20000" & $file , 1)

Edit: Added the If Statement

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

FileMove ( "c:\temp2\" & $file , "C:\temp20000" & $file , 1)

Edit: Added the If Statement

Damn! Much cleaner :P

I had to change one thing from your code.

While 1

$file = FileFindNextFile($search)

If @error Then ExitLoop

$file2 = $file <--------------------------------------------

Do

$File = '0' & $File

Until StringLen($File) = 16

MsgBox(0,"",$file)

FileMove ( "c:\temp2\" & $file2 , "C:\temp2\" & $file , 1)

WEnd

I had to assign a variable to equal $file before the "do loop" changed the file name.

The FileMove was not working without this???

I really appreciate the feedback!

jp

Share this post


Link to post
Share on other sites

do you know StringFormat?

$iLen = 10
$iNumber = 123
ConsoleWrite(StringFormat("%0" & $iLen & "d",$iNumber))
; Output: 0000000123

This looks extremely clean also. I think that is what I really love about autoit,

many ways to get the same thing done (some cleaner than others - see my original post) :P

I will test this variation out tomorrow. I will run all three on a large number of files (500) in the

near future to see which one gives the best performance.

Thanks again!

jp

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0