Ahk Scrpit that copy text from notepad and rename files Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 14 May 2022, 06:49

Hi,
I am currently working on script that will copy text from notepad and rename all the files accordingly to the notepad names.
naming can be like
Abc_abc_aaa
If anyone can guide me on this it will very Helpful.
Thanks

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 14 May 2022, 07:38

Your description is vague. See :arrow: FileMove. Example

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 14 May 2022, 15:10

So it is something like this take names from notepad and renames the file
I think i can try using Filemove for renaming but i not sure how to store notepad file names (I can add Number 1,2,3 before name if that helps with the logic to store names)

image.png
image.png (145.98 KiB) Viewed 1064 times

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 14 May 2022, 17:29

Code: Select all

dir := A_ScriptDir
ControlGetText, list, Edit1, ahk_exe notepad.exe
For fileNum, fn in StrSplit(list, "`r`n") {
 SplitPath, fn,,, ext, fnBare
 FileMove, %dir%\%fn%, %dir%\%fnBare%-%fileNum%.%ext%
}
MsgBox, 64, Done, Done!

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 14 May 2022, 19:32

Thanks for help But the code is not working i have checked it is taking the name from notepad but unbale to rename the file

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 14 May 2022, 19:39

You can display the values of the variables in the loop, so that you know what they are. That will enable you to debug your script. FileExist will tell you whether the file exists. The script worked in my test, but I used my Notepad file rather than yours, which is not posted.

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 15 May 2022, 13:30

Hi,
Yes I have check by display all the values And i found it is not taking path of images that is available in folder generally we can find that using "A_LoopFilePath" but since we are not using loop i cannot use that. That is why FileMove is not working
Since there is no "SourcePattern".
Also This line

Code: Select all

FileMove, %dir%\%fn%, %dir%\%fnBare%-%fileNum%.%ext%
Is it suppose to remove the number. because the Output i get from that is "C:\root\1 Abc_aaa-1.png"
%fnBare% = Name from notepad i.e 1 Abc_aaa
%fileNum%.%ext% = 1.png

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 15 May 2022, 14:03

You can post your complete script, your Edit1 contents or sample file, and the path to the directory where these files reside. Include a description of how each file should be renamed.

When you used MsgBox to display the values of dir and fn in the script that I posted, what did you see?

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 15 May 2022, 16:11

As i have mentioned I am looking to rename file based on notepad file
image.png
image.png (81.17 KiB) Viewed 897 times
So the script will takes names from notepad and rename the file in the folder one by one
After rename result should be something like this
image.png
image.png (79.01 KiB) Viewed 897 times
From this code

Code: Select all

dir := A_ScriptDir
ControlGetText, list, Edit1, ahk_exe notepad.exe
For fileNum, fn in StrSplit(list, "`r`n") {
 SplitPath, fn,,, ext, fnBare
 FileMove, %dir%\%fn%, %dir%\%fnBare%-%fileNum%.%ext%
}
MsgBox, 64, Done, Done!
From %List% i am getting name from notepad
Edit1 is coming blank
%dir% = C:\Users\New folder\New folder
%fn% = 1 Abc_test.png
%fnBare% = 1 Abc_test
fileNum = 1,2,3
ext = .png

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 15 May 2022, 16:16

Is Notepad the name of your saved text file, or are you saying that you have the Microsoft Notepad program open, and you have text displayed in that program?

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 15 May 2022, 16:25

I have Microsoft Notepad program open and have all text name as shown in image.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 15 May 2022, 19:06

Hmm. Let's back up and just see if we can get the text.

Code: Select all

#IfWinExist ahk_exe notepad.exe
F3::
ControlGetText, list, Edit1
MsgBox, 64, List, %list%
Return
#IfWinExist
Is more than one instance of Notepad open?

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 16 May 2022, 05:16

Yes I am able to get the text and no there is only one instance of notepad is running.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 16 May 2022, 06:17

OK. I am confused. You indicated earlier, "Edit1 is coming blank". What did you mean by that, and how did you determine it? Are you testing a script that is not actually posted here? If so, please post it below.

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 16 May 2022, 06:29

ohh sorry Edit1 is still coming blank I meant list is showing all the names.
image.png
image.png (142.41 KiB) Viewed 770 times
Edit1 Input
image.png
image.png (138.93 KiB) Viewed 770 times

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 16 May 2022, 07:17

You have highlighted the bug in your script, but you have not yet provided a written description of how to rename your files-- i.e., what the new names should be. "naming can be like Abc_abc_aaa" provides one file name but does not explain anything.

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 16 May 2022, 14:11

So i was thinking like this first image that is in the folder to be renamed with first name in notepad and so on.
Lets say first image name is like this
"Knight_Templer"
So replace this name to the name from notepad like 1 Rename_Test. so final name would be "Rename_Test"

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 16 May 2022, 14:44

As you describe it, the first line in Notepad should correspond to the first file in your folder. What is your description of how to get from that Notepad line to your final file name? I see an example but not a description of the process.

mickey12
Posts: 38
Joined: 28 Apr 2022, 19:36

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mickey12 » 16 May 2022, 17:00

From the Code you have provided in %fn% we can able to get names from notepad and i know I can get file name by A_LoopFilePath but i not sure how to incorporate these 2 output in one place.

User avatar
mikeyww
Posts: 26599
Joined: 09 Sep 2014, 18:38

Re: Ahk Scrpit that copy text from notepad and rename files

Post by mikeyww » 16 May 2022, 17:05

You must have an idea of how you get from "1 Rename_Test" to "Rename_Test", right? Remove one digit? Skip text before the first space? Other?

Post Reply

Return to “Ask for Help (v1)”