Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Closing Wordpad via Powershell

  • 27-03-2019 4:30pm
    #1
    Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭


    Hi,

    I am working with a script which parses a document and prints its output into a Wordpad file.
    I have elaborated on this to allow it to iterate over a number of documents. It all works fine.

    I would like to improve on the implementation as there is a flaw in that the original script which I picked up on opens an instance of Wordpad, populates it, but then leaves it open. This is an issue as my modified version of this script can be iterating over hundreds of documents.
    pushd %~dp0
    ...
    powershell start wordpad %outputFile%
    popd
    

    How do I close each instance of the document?


    Thanks.


Comments

  • Registered Users, Registered Users 2 Posts: 10,906 ✭✭✭✭28064212


    powershell (get-process wordpad).CloseMainWindow()
    
    Although that won't work if you have unsaved changes in the wordpad document

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    Thanks - I've almost got it complete now, just need to tidy some parts up.
    for /f "delims=" %%x in (C:\Work\tmp.txt) do set docTitle=%%x
                    
    REM Trim space and parenthesis characters from document title string
    echo Document Title is %docTitle%
    
    set docTitle=%docTitle:(=%
    set docTitle=%docTitle:)=%
    set docTitle=%docTitle:/=%
    set docTitle=%docTitle:\=%
    set docTitle=%docTitle:-=%
    set docTitle=%docTitle: =%
    

    What the above does is it reads tmp.txt into a variable called docTitle. The subsequent steps are to remove characters that cause the filename to be rejected.
    Is there any single line command that will remove all non alphanumeric characters from the string. There are some odd ones being returned (y with an umlaut) which I am struggling to remove - they cause the script to fail, so I need to remove them.

    Thanks.


  • Registered Users, Registered Users 2 Posts: 10,906 ✭✭✭✭28064212


    Much easier in Powershell
    set docTitle='test This!é{}.txt'
    for /f "delims=" %a in ('powershell -command "\"%doctitle%\" -replace '[^a-zA-Z0-9\.]',''"') do Set "doctitle=%a"
    echo %doctitle%
    
    The last line will print
    testThis.txt
    

    Boardsie Enhancement Suite - a browser extension to make using Boards on desktop a better experience (includes full-width display, keyboard shortcuts, dark mode, and more). Now available through your browser's extension store.

    Firefox: https://addons.mozilla.org/addon/boardsie-enhancement-suite/

    Chrome/Edge/Opera: https://chromewebstore.google.com/detail/boardsie-enhancement-suit/bbgnmnfagihoohjkofdnofcfmkpdmmce



  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    Great thanks - I'll try that in the morning.


  • Registered Users, Registered Users 2 Posts: 8,593 ✭✭✭funkey_monkey


    That seems to be working - thanks.
    I'm running it from within a batch file so I had to double up on the % within the for do command.


  • Advertisement
Advertisement