Jump to content
Sign in to follow this  
routama

Replace certain characters from clipboard

Recommended Posts

routama

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!

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
hannes08

You can use StringReplace() to replace a single character.

Edit:

Well, too late ;)

Edited by hannes08

Regards,Hannes[spoiler]If you can't convince them, confuse them![/spoiler]

Share this post


Link to post
Share on other sites
routama

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)

Share this post


Link to post
Share on other sites
water

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 2018-12-03 - Version 1.4.11.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
routama

I need to do it like described originally.

This previous instruction sounds perfect, but I do not know how to use it.

; Replace invalid characters from filename with underscore

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

Share this post


Link to post
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
Sign in to follow this  

×