Need advice on renaming files

Get help with using AutoHotkey and its commands and hotkeys
omar
Posts: 419
Joined: 22 Oct 2015, 17:56

Need advice on renaming files

10 Apr 2016, 15:30

How would I rename files in a directory and add a number extension?
Can I do this in AHK or should I use DOS?
What command sets would I need?

Let's say I have abc.csv, xyz.csv and pqr.csv.
I would want to rename as abc.1.csv, xyz.2.csv, and pqr.3.csv.

Thanks


Omar
omar
Posts: 419
Joined: 22 Oct 2015, 17:56

Re: Need advice on renaming files

10 Apr 2016, 16:28

@HinkerLoden thanks - that gives me a good start.
in filecopy, i couldn't see anything that lets me rename - only copy or move.
for this job, doesn't matter too much - but would prefer to rename if it was an option.

thanks.
User avatar
JoeWinograd
Posts: 1545
Joined: 10 Feb 2014, 20:00

Re: Need advice on renaming files

10 Apr 2016, 16:34

You want FileMove, not FileCopy:
https://autohotkey.com/docs/commands/FileMove.htm

A FileMove into the same folder is a rename (it is also possible with FileMove to move a file to a different folder both with and without renaming it). Regards, Joe
User avatar
HinkerLoden
Posts: 93
Joined: 23 Mar 2016, 07:50
GitHub: HinkerLoden

Re: Need advice on renaming files

10 Apr 2016, 18:21

here we go - just fit in your things. There is one little thing. Maybe you can find it.

Code: Select all

; F9		- Get Filelist 
;=============================================================

F9::

FileListNew =
Loop, Files, *.* 			; *.csv | *.doc 
	{
	StringTrimRight, Line, A_LoopFileName, 4
	FileListNow =%FileListNow%%A_LoopFileName%`n
	FileListNew = %FileListNew%%Line%_%A_INDEX%.%A_LoopFileExt%`n
	
	;MsgBox, Old_: %FileListNow% `n`nNew_: %FileListNew%
   
	}
MsgBox, Old_: %FileListNow% `n`nNew_: %FileListNew%

Return





; Hotkey2
;=============================================================

F10::
Loop, Files, *.*
{
	
	StringTrimRight, Line, A_LoopFileName, 4
	
	MsgBox, "%A_LoopFileName%"  -->  "%Line%_%A_INDEX%.%A_LoopFileExt%"
	
	FileCopy, %A_LoopFileName%, %Line%_%A_INDEX%.%A_LoopFileExt%   ;replace copy with Move
	
	
}



Return

Complete

Code: Select all

;************************************************
; AHK _ SCRIPT STRUCTURE TEMPLATE v1.1
; by HinkerLoden
; 25/03/2016 
; OS  			--> 	Win 10 / etc. 
;AHK Version	-->		1.1.23.05 - Ansi32
;************************************************

;************************************************
;Script Global Settings
;************************************************

#NoEnv						; Clear All Systemvariables 
#Persistent 				;Keeps a script permanently running until ExitApp execute

#SingleInstance force		;The word FORCE skips the dialog box and replaces the old instance automatically, which is similar in effect to the Reload command.

;************************************************
;Performance PARAMETERS - if you need speed 
;************************************************
;SetBatchLines, -1   		
;Process, Priority, , L  ;A  - Max Speed
;************************************************
; Input PARAMETERS
;************************************************
SendMode Input					;Default = Event , Play - for Speed -> Set Keydelay to -1 & Setmousedely to -1 
;----
SetKeyDelay, 10, 10   			; for speed -1, -1, 
SetMouseDelay, 25				;0 recommend   /  -1 for max speed 
SetDefaultMouseSpeed, 0			;0-100 
;************************************************
;History Protocols 
;Switch setting for more Speed 
;************************************************
#KeyHistory 1					;  0 - No Keyhistory 
ListLines  On  					; Off  - for more speed 
;************************************************
;Window detection
;************************************************
SetTitleMatchMode, 2
SetTitleMatchMode Fast			;slow detect hidden windows

SetWinDelay, 200  				;0 - for more speed 

;StringCaseSense, On|Off|Locale
;DetectHiddenWindows, On		; | Off   
;i double the standard settings to be on the save side 
;#######################				#######################
; Script Parameter
;#######################				#######################

SetWorkingDir %A_ScriptDir%  	;Ensures a consistent starting directory.

;#Include %A_ScriptDir%\YourLibrarys2integrate.ahk

CoordMode, Pixel, Screen		;relative/Window - active Window
CoordMode, Mouse, Screen

;WinMinimizeAll					;Optional

;#######################				#######################
; Try to avoid pure numbers inside the code - adress all Vars here
;=============================================================
; 						Variable Section
;=============================================================
;#######################				#######################
;global / static
;EnvUpdate ; --> Notifies the OS and all running applications that environment variable(s) have changed. 
;#######################				#######################

;===============================  TimerVars
Variable_01	=	
Variable_02	=	
Variable_03	=	
Variable_04	=	
Variable_05	=	
Variable_06	=	
Variable_07	=	
Variable_08	=	
Variable_09	=	
Variable_10	=	
;#######################				#######################
;=============================================================
; 						CODE 
;=============================================================
;#######################				#######################

 
 
;=============================================================
; 						HOTKEYS 
;=============================================================




; F9		- Get Filelist 
;=============================================================

F9::

FileListNew =
Loop, Files, *.* 			; *.csv | *.doc 
	{
	StringTrimRight, Line, A_LoopFileName, 4
	FileListNow =%FileListNow%%A_LoopFileName%`n
	FileListNew = %FileListNew%%Line%_%A_INDEX%.%A_LoopFileExt%`n
	
	;MsgBox, Old_: %FileListNow% `n`nNew_: %FileListNew%
   
	}
MsgBox, Old_: %FileListNow% `n`nNew_: %FileListNew%

Return





; Hotkey2
;=============================================================

F10::
Loop, Files, *.*
{
	
	StringTrimRight, Line, A_LoopFileName, 4
	
	MsgBox, "%A_LoopFileName%"  -->  "%Line%_%A_INDEX%.%A_LoopFileExt%"
	
	FileCopy, %A_LoopFileName%, %Line%_%A_INDEX%.%A_LoopFileExt%   ;replace copy with Move
	
	
}



Return



; Exit 
;=============================================================
Exit:
!x::  ; Hotkey ALT+x.
GuiEscape:
GuiClose:
 ExitApp


 
;#######################				#######################
;=============================================================
; 						FUNCTIONS 
;=============================================================
;#######################				#######################

omar
Posts: 419
Joined: 22 Oct 2015, 17:56

Re: Need advice on renaming files

27 Apr 2016, 08:32

@HinkerLoden OMG - thank u.
that code is just sooo awesome.
i haven't had a chance to test until now.

the complete code, you have many other settings - is this what you use for your boiler plate code for all your ahk coding?

EDIT: i think there's a small bug in the code. my filenames are like this 12-20-16.15.5.43.csv
when i first run the code, the first file gets renamed twice.
let's say we have 7 files and the above filename is the first file...
it get's renamed to 8.1.12-20-16.15.5.43.csv

i made a small change to your code so that i would have the number put at the beginning and a period following.
this is what i have:

Code: Select all

F10::
Loop, Files, *.csv
{
	StringTrimRight, Line, A_LoopFileName, 4
	FileMove, %A_LoopFileName%, %A_INDEX%.%Line%.%A_LoopFileExt%   ;replace copy with Move
}
i wasn't sure why this happens.
any ideas?

thanks

Return to “Ask For Help”

Who is online

Users browsing this forum: A_AhkUser, Bing [Bot], boiler, Google [Bot], majstang, neo256, Tomo2407 and 52 guests