Jump to content

auotmatic renaming of files


Tarte
 Share

Recommended Posts

Does anyone have a script that already does this? I have a process that creates files with random names. I want an autoit script to run and change the name of every file in a folder to the time and date that the file was created.

:P

Link to comment
Share on other sites

Use FileGetTime Function to find the date on which it was created and then FileCopy (as AutoIT has no File Rename function!!)

actually, FileMove() is what you're looking for, FileCopy() will create a new file with the name and leave the old one, so you either have duplicates, or have to use an extra step to delete the original. FileMove() can be used to rename, just by moving it to the same directory with a different name.
Link to comment
Share on other sites

actually, FileMove() is what you're looking for, FileCopy() will create a new file with the name and leave the old one, so you either have duplicates, or have to use an extra step to delete the original. FileMove() can be used to rename, just by moving it to the same directory with a different name.

The logic seems right but where I am still confused is, the command seems to want a specific file.

My current situation is I have a folder with about a thousand files that I want all renamed to their time (down to the minute - to the second is not necessary).

Link to comment
Share on other sites

The logic seems right but where I am still confused is, the command seems to want a specific file.

My current situation is I have a folder with about a thousand files that I want all renamed to their time (down to the minute - to the second is not necessary).

one sec i'll write a little script to accomplish it, do you want the same thing done to files in sub folders? or just one folder?
Link to comment
Share on other sites

maybe this

$folder = "C:\test\" 
$search = FileFindFirstFile($folder & "*.*")

While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    If Not $file = Int($file) Then
        $Nfile = FileGetTime($folder & $file,0,1)
        FileMove($folder & $file, $folder & $Nfile)
    EndIf
WEnd

8)

NEWHeader1.png

Link to comment
Share on other sites

maybe this

$folder = "C:\test\" 
$search = FileFindFirstFile($folder & "*.*")

While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    If Not $file = Int($file) Then
        $Nfile = FileGetTime($folder & $file,0,1)
        FileMove($folder & $file, $folder & $Nfile)
    EndIf
WEnd

8)

you forgot to remove the seconds from the get time, what if he wants subfolders done too? you're also renaming the subfolders... and no log file to say what was changed from what to what? I'm very dissappointed. I would have expected better from you... :P
Link to comment
Share on other sites

you forgot to remove the seconds from the get time, what if he wants subfolders done too? you're also renaming the subfolders... and no log file to say what was changed from what to what? I'm very dissappointed. I would have expected better from you... :P

neat. this is my 1866'th post, and i'm wasting it just to let everyone know

NEWHeader1.png

Link to comment
Share on other sites

ok.... it has never been my intension here to create or be in the middle of tossing crap at each other

so....

the user is off-line and has not responded to you or I regarding how well my script worked or if there are subfolders and etc... like you asked

besides....

veiwing your last posts i dont see any sub-folder or log files for the changes your scripts created and...

this was just an idea... not a finished product .... for sure

8)

NEWHeader1.png

Link to comment
Share on other sites

ok.... it has never been my intension here to create or be in the middle of tossing crap at each other

so....

the user is off-line and has not responded to you or I regarding how well my script worked or if there are subfolders and etc... like you asked

besides....

veiwing your last posts i dont see any sub-folder or log files for the changes your scripts created and...

this was just an idea... not a finished product .... for sure

8)

i was just playing with my mock critique of your code, feigning bitterness that you beat me to it while i was waiting on a response from him to start mine. I've no problems with you, or your code, and i thought you knew that (not sure why i assumed that in retrospect though but anyway...).
Link to comment
Share on other sites

I copied several hundred files into a newlly created c:\test folder. Then I copied the script into a new AU3 file and ran it (with the AU3 file located in a seperate folder & with the AU3 file within the C:\test folder). Nothing happened.

I assume the $folder is setting where the work will be done.

I wonder if the naming convention of the original files is causing the error (2005-12-13_1833221213.jpg). The application let's me pic the originating name "2005-12-13" but because so many files get created what seem to be random numbers like '1833221213' get appended.

Your help is greatly appreciated.

Tim

Link to comment
Share on other sites

Donot know what went wrong there. But here for a better version.

Fixed bad folder var usage.

-Just move script to folder where the files need to change name.

-Run it

-Smile happy when your files are all changed and you cannot know which file was what.

If you want some name in front of the file change $Nameadditive

*Only one bug the file has no extension (this could be fixed easily using another regexp)

edit Bug allready fixed

$Nameadditive = ""
$folder = @scriptdir; Why make it so difficult;) 
$search = FileFindFirstFile($folder & "*.*")

While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    If Not $file = Int($file) Then
        $Nfile = FileGetTime($folder & $file,0,1)
          $Nfile = StringRegExpReplace ( $Nfile,"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "\1-\2-\3_\4_\5")
      ;fix for lazy scripting nice regexp thingy
        $extension = StringRegExpReplace ( $file,".*?(\.[^.])","\1")
        FileMove($folder & $file, $folder & $Nameadditive & $Nfile)
    EndIf
WEnd

Maybe you just wanted to add the date in front of the original name. (2005-12-18_04_04happy.jpg)

then this (ow yeah here the file extension is kept)

$folder = @scriptdir; Why make it so difficult;) 
$search = FileFindFirstFile($folder & "*.*")

While 1
    $file = FileFindNextFile($search)
    if @error Then ExitLoop
    If Not $file = Int($file) Then
        $Nfile = FileGetTime($folder & $file,0,1)
        $Nfile = StringRegExpReplace ( $Nfile,"(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})", "\1-\2-\3_\4_\5")
; fix for lazy scripting nice regexp thingy
        FileMove($folder & $file, $folder & $Nfile & $file)
    EndIf
WEnd
Edited by MrSpacely
Link to comment
Share on other sites

I copied several hundred files into a newlly created c:\test folder. Then I copied the script into a new AU3 file and ran it (with the AU3 file located in a seperate folder & with the AU3 file within the C:\test folder). Nothing happened.

I assume the $folder is setting where the work will be done.

I wonder if the naming convention of the original files is causing the error (2005-12-13_1833221213.jpg). The application let's me pic the originating name "2005-12-13" but because so many files get created what seem to be random numbers like '1833221213' get appended.

Your help is greatly appreciated.

Tim

Tim,

I wrote a Dos script that you can run in Autoit that will do this here:

(it's actually a two liner, including the variable) woot)

$dir = "c:\test\"
runwait(@comspec & " /c " & "for /f %I in ('dir " & $dir & " /b') do ren " &$dir&"%I %I_%date:~4,2%%date:~7,2%%date:~10,4%","",@sw_hide)

If you want to practice on some dummy files try this script. It will create however many dummy files you want, just change the $dummy_number variable.

$dir = "c:\test\"
$dummy_number = 40
for $i = 1 to $dummy_number
    $file = "testfile" & $i
    _FileCreate($dir & $file)
Next

hope that helps.

Please let me know.

Edited by blademonkey

---"Educate the Mind, Make Savage the Body" -Mao Tse Tung

Link to comment
Share on other sites

Tim,

I wrote a Dos script that you can run in Autoit that will do this here:

(it's actually a two liner, including the variable) woot)

$dir = "c:\test\"
runwait(@comspec & " /c " & "for /f %I in ('dir " & $dir & " /b') do ren " &$dir&"%I %I_%date:~4,2%%date:~7,2%%date:~10,4%","",@sw_hide)

If you want to practice on some dummy files try this script. It will create however many dummy files you want, just change the $dummy_number variable.

$dir = "c:\test\"
$dummy_number = 40
for $i = 1 to $dummy_number
    $file = "testfile" & $i
    _FileCreate($dir & $file)
Next

hope that helps.

Please let me know.

This script:

$dir = "c:\test\"

runwait(@comspec & " /c " & "for /f %I in ('dir " & $dir & " /b') do ren " &$dir&"%I %I_%date:~4,2%%date:~7,2%%date:~10,4%","",@sw_hide)

renames the file from 2005-12-13_0711081213.jpg to 2005-12-13_0711081213.jpg_12182005 (which seems to me only appends the current time the script was run to the end of the existing file).

Thank you everyone for your help! Further attempts at a working script are greatly appreciated!

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...