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
need help renaming file - removing the last 21 characters of a file name Topic is solved
Re: need help renaming file - removing the last 21 characters of a file name Topic is solved
Examples:
Testing:
Usage:
Testing:
Code: Select all
FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21)
MsgBox % ">" . NewFileName . "<"
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
}
- flyingDman
- Posts: 2839
- Joined: 29 Sep 2013, 19:01
Re: need help renaming file - removing the last 21 characters of a file name
Code: Select all
FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf"
NewFileName := SubStr(FileName, 1, -21)
MsgBox % ">" . NewFileName . "<"
14.3 & 1.3.7
Re: need help renaming file - removing the last 21 characters of a file name
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!Xtra wrote: ↑17 Jan 2024, 17:28Examples:
Testing:Usage:Code: Select all
FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf" NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21) MsgBox % ">" . NewFileName . "<"
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 }
- flyingDman
- Posts: 2839
- Joined: 29 Sep 2013, 19:01
Re: need help renaming file - removing the last 21 characters of a file name
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)
Code: Select all
SubStr(FileName, 1, -21)
14.3 & 1.3.7
Re: need help renaming file - removing the last 21 characters of a file name
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 ... etcI just have to be careful with it, as there is no undo button.
Code: Select all
esc::exitapp
Re: need help renaming file - removing the last 21 characters of a file name
After testing, i need a little help to get this over the finish line.Xtra wrote: ↑17 Jan 2024, 17:28Examples:
Testing:Usage:Code: Select all
FileName := "123456789_Design-Change_L-156.pdf_096c5e428bd8be79.pdf" NewFileName := SubStr(FileName, 1, StrLen(FileName) - 21) MsgBox % ">" . NewFileName . "<"
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 }
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
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
}