0

I have recently asked a question on how to perform a neat little 'Find & Replace' task on my CSV media database files (a task i have to do at work on a daily basis). The question was answered here if anyone is interested in the details.

I now want to be able to simply drag and drop my CSV file onto a droplet on the desktop that performs the Find and Replace Notepad++ task quickly and easily, so i can delegate this task to more junior members of staff at times when i'm away.

Question:

How do I create a droplet in Win 7 that runs the following 'Find & Replace' command on any CSV file dropped onto it?

enter image description here

Text:

Find what:

^([^,]*,(\d\d)_[^,]*,[^,]*)(?<!_\d\d),

Replace with:

$1_$2,

Check: Wrap around

Check: Regular expression

Run: Replace All

I am imagining that this could be a batch script (or any script of sorts) that will receive my CSV file as the input file by simply drag dropping it onto the script.

  • What exactly is a 'droplet'? Is it the same as a Shortcut? – Berend Aug 28 '18 at 10:56
  • Hi, i'm referring to a script that can receive the input file by dragging and dropping my CSV file onto it. I'm thinking this could be a batch script that would receive file "%~1" and open in Notepad++ to run the command I've requested, however i am really not knowledgeable enough in this area to know what the best thing to do here is? i will edit my question further clarify. Thanks in advance. – Myles Aug 28 '18 at 11:02
  • I get it. As far as I know Notepad++ does not have such an option (docs.notepad-plus-plus.org/index.php/Command_Line_Switches), so you may need to use other tooling such as AutoIt or a windows version of sed. I'm not an expert on either of these. – Berend Aug 28 '18 at 11:06
  • I'm open to any and all suggestions. Thanks everyone for all contributions towards this! – Myles Aug 28 '18 at 11:14
  • I don't know if the Notepad++ 'Find and Replace' function can be called from command line but i have recorded what i need to do as a macro within Notepad++. So i'm not sure if perhaps these macro's can be called in command line instead? – Myles Aug 28 '18 at 11:44
1

Windows 7 comes with PowerShell v2 which supports Regular Expressions with negative look behinds.

If I interpret your RegEx correctly you want to apply the two digits 99_ leading the 2nd field to trail also the 3rd field, provided there isn't already a _99 number. And leave the other lines/remainder of the line as is.

A batch file serving as a drop target (also handling multiple files):

:: SO_1352996.cmd
@Echo off
:Loop
If "%~1" equ "" goto :Eof
If not exist "%~1" (shift & goto :Loop)
Ren "%~f1" "%~n1_Original%~x1"
:: Use PowerShell as a tool to do the replace.
Powershell -NoP -C "(Get-Content '%~dpn1_Original%~x1') -replace '^([^,]*,(\d\d)_[^,]*,[^,]*)(?<!_\d\d),','$1_$2,' | Set-Content '%~f1'
Shift
Goto :Loop

file.csv before

test,12_blah,blubb,anything
test,34_blah,blu_34,doesn't matter
test,56_foo,bar,nevermind

and after dropping on the above batch.

test,12_blah,blubb_12,anything
test,34_blah,blu_34,doesn't matter
test,56_foo,bar_56,nevermind

The original file is saved with the additional extension .bak

  • Hi LotPings, I just copy pasted your code and saved as a batch script. When i try drag dropping the csv file it opens a cmd window and repeats the following 'A duplicate filename exists, or the file cannot be found'. Thanks in advance for helping me! – Myles Aug 28 '18 at 14:27
  • Maybe the modified or original file can be saved out with '_ORIGINAL' or '_MODIFIED'? Not sure how to change the script to do that. – Myles Aug 28 '18 at 14:28
  • 1
    Sorry there is a missing shift as the second last line, Corrected. – LotPings Aug 28 '18 at 14:28
  • 1
    Incorporated the _Original appendix to the file name – LotPings Aug 28 '18 at 14:33
  • That seems to have worked in modifying the database file. I had to copy the file to the desktop to do it though because the command window tells me 'UNC Paths not supported'. The place where all the CSVs are stored is a network location. Any way around that? I'm not too fussed if it's too much of a troubleshoot to deal with. – Myles Aug 28 '18 at 14:35

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.