Jump to content

Replace certain characters from clipboard


Recommended Posts

Hello,

1. In my script I use copy to clipboard -> Send("^c"). I use $name = ClipGet().

-> this string might contain one bad character or several (spaces, /, \, :, >, <, etc.)

2. and then I need to use this $name in a file name (e.g. Excel)

3. For one bad character there is no problem:

If StringIsAlNum($name) = 0 Then

If StringInStr($name, "/") = 0 Then

Sleep(100)

Else

Send("c:\temp\" & StringReplace($name, "/", "_"))

4. BUT when I have a clipboard copy with sevaral bad characters, I do not know how to proceed?

I could use some help.

I am a total amateur, so please be simple!

Thanks all!

Link to comment
Share on other sites

To get a valid filename by replacing all invalid characters with an "_" I use the following code:

; Replace invalid characters from filename with underscore
$sOL_FName = StringRegExpReplace($sOL_FName, '[ /:*?"<>|]', '_')
To completely remove all invalid characters replace "_" with "". Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Thank you so far!

This is an example script I have:

-> it open notepad, and copies the text (file name) from there. It contains sevaral bad characters. Then it opens Excel, and tries to save it to c:\temp\ as "copied file name".

What is wrong with this?

MouseClick("Left", 937, 440, 2)

Sleep(1000)

Send("^a")

Sleep(1000)

Send("^c")

Sleep(1000)

$name = ClipGet()

MouseClick("Left", 937, 530, 2)

Sleep(2000)

Send("^s")

Sleep(1000)

If StringIsAlNum($name) = 0 Then

$sOL_FName = StringRegExpReplace($sOL_FName, '[ \/:*?"<>|]', '_MOD_')

Else

Send("c:\temp\" & $name)

EndIf

Sleep(3000)

Send ("{ENTER}")

Sleep(3000)

Link to comment
Share on other sites

I wouldn't automate Excel using Send commands. Use the Excel UDF that comes with AutoIt and pass the filename to function _ExcelBookSaveAs.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...