need help renaming file - removing the last 21 characters of a file name Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sargo1942
Posts: 3
Joined: 17 Jan 2024, 14:57

need help renaming file - removing the last 21 characters of a file name

17 Jan 2024, 15:23

not sure the best way to do this, but it seems simple. Hoping someone can help me
for work we are asked to download bulk files that have extraneous garbage posted to the end of the file. Then we are tasked with manually going in and deleting the junk on the end. These are all PDF's but for some reason and underscore and 16 random characters with a .pdf are added to the end.

an example would be:
123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf

The underscore, sixteen characters, and the extra .pdf all need to be deleted. its basically 21 characters that just need to be deleted off the end of the file. Since there is already a .pdf just before those 21 characters. I'd like to be able to active this command and just have it run on the files in a specific folder. Its fine if it outputs a separate version of the files. I don't have admin right so i would to run this via an .ahk file on my desktop.

thanks in advance! you guys rock :clap:
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: need help renaming file - removing the last 21 characters of a file name  Topic is solved

17 Jan 2024, 17:28

Examples:

Testing:

Code: Select all

FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21)
MsgBox % ">" . NewFileName . "<"
Usage:

Code: Select all

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileMove, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Move Error, % A_LoopFileLongPath
}

; or:

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileCopy, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Copy Error, % A_LoopFileLongPath
}
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: need help renaming file - removing the last 21 characters of a file name

17 Jan 2024, 17:38

Code: Select all

FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, -21)
MsgBox % ">" . NewFileName . "<"
14.3 & 1.3.7
sargo1942
Posts: 3
Joined: 17 Jan 2024, 14:57

Re: need help renaming file - removing the last 21 characters of a file name

17 Jan 2024, 19:57

Xtra wrote:
17 Jan 2024, 17:28
Examples:

Testing:

Code: Select all

FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21)
MsgBox % ">" . NewFileName . "<"
Usage:

Code: Select all

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileMove, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Move Error, % A_LoopFileLongPath
}

; or:

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileCopy, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Copy Error, % A_LoopFileLongPath
}
super useful, i used the FileMove top loop code you wrote and that worked wonders. I just have to be careful with it, as there is no undo button. Not for the general population, but will be fine for my limited usage...and saving me from carpal tunnel. :) thanks!
User avatar
flyingDman
Posts: 2832
Joined: 29 Sep 2013, 19:01

Re: need help renaming file - removing the last 21 characters of a file name

17 Jan 2024, 21:00

SubStr docs wrote:You can also specify a negative Length to omit that many characters from the end of the returned string (an empty string is returned if all or too many characters are omitted).

Code: Select all

SubStr(FileName, 1, StrLen(FileName) - 21)
is not wrong but

Code: Select all

SubStr(FileName, 1, -21)
is just a better way to write it.
14.3 & 1.3.7
garry
Posts: 3787
Joined: 22 Dec 2013, 12:50

Re: need help renaming file - removing the last 21 characters of a file name

18 Jan 2024, 08:28

I just have to be careful with it, as there is no undo button.
maybe can use ESC-button to quit the script / or also add sleep to make slower or add a msgbox > want you really rename this file ? see filename before/after ... or use filecopy ... etc

Code: Select all

esc::exitapp
sargo1942
Posts: 3
Joined: 17 Jan 2024, 14:57

Re: need help renaming file - removing the last 21 characters of a file name

01 Feb 2024, 20:09

Xtra wrote:
17 Jan 2024, 17:28
Examples:

Testing:

Code: Select all

FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21)
MsgBox % ">" . NewFileName . "<"
Usage:

Code: Select all

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileMove, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Move Error, % A_LoopFileLongPath
}

; or:

Loop, Files, C:\My Folder Path\Goes Here\*.pdf
{
	FileCopy, % A_LoopFileLongPath, % SubStr(A_LoopFileLongPath, 1, StrLen(A_LoopFileLongPath) - 21)
	if (ErrorLevel)
		MsgBox, 4096, File Copy Error, % A_LoopFileLongPath
}
After testing, i need a little help to get this over the finish line.

I will have my script and files in a starting folder, i would like this script, if it is successful to copy the converted files into a a FINISH folder. That way we have a record of the original and the converted files.

so it would look like

SCRIPT.AHK
..\ORIG\
..\FINAL\

appreciate your help!
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: need help renaming file - removing the last 21 characters of a file name

02 Feb 2024, 01:32

Give this a try:

Code: Select all

Loop, Files, % A_ScriptDir . "\ORIG\*.pdf"
{
	FileCopy, % A_LoopFileLongPath, % A_ScriptDir . "\FINAL\" . SubStr(A_LoopFileName, 1, -21)
	if (ErrorLevel)
		MsgBox, 4096, File Copy Error, % A_LoopFileName
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: kaka2, SmithyZoomZoom and 148 guests