Page 1 of 1

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

Posted: 17 Jan 2024, 15:23
by sargo1942
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:

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

Posted: 17 Jan 2024, 17:28
by Xtra
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
}

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

Posted: 17 Jan 2024, 17:38
by flyingDman

Code: Select all

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

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

Posted: 17 Jan 2024, 19:57
by sargo1942
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!

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

Posted: 17 Jan 2024, 21:00
by flyingDman
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.

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

Posted: 18 Jan 2024, 08:28
by garry
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

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

Posted: 01 Feb 2024, 20:09
by sargo1942
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!

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

Posted: 02 Feb 2024, 01:32
by Xtra
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
}