So I have been using Notepad++ to do some little clean-up tasks and now I am left with the biggest task..
I have a file called Artists.txt which looks like
Butta Mohamed
Daler Mehndi
Daljit Mattu
Darshan Khela
Davinder Deep
Davinder Deol
etc...
I have another file called Keywords.txt (located in hundreds of other folders). The folders are named like below and they all contain a text file called Keywords.txt
butta-mohamed-lyrics
daler-mehndi-lyrics
daljit-mattu-lyrics
darshan-khela-lyrics
davinder-deep-lyrics
davinder-deol-lyrics
The Keywords.txt contains the text _1 (several instances within the Keywords.txt).
What I would like to do is get each line from Artists.txt and have the _1 replaced. The folders are in the same order as Artists.txt.
So read Artists.txt get first line Butta Mohamed get first folder butta-mohamed-lyrics edit Keywords.txt find _1 replace (all) with Butta Mohamed. Save changes. Rinse and repeat so read Artists.txt get next line Daler Mehndi get next folder daler-mehndi-lyrics edit Keywords.txt find _1 replace (all) with Daler Mehndi. Save Changes.
Wondering if something like this is possible? Otherwise it would take me a week to manually do this via copy/pasting or even the replace function in Notepad++
I've tried the Macro function in Notepad++ but CTRL-V rather then pasting whats in the clipboard the macro seems to replace the CTRL-V function with whatever text the macro was recorded with has.
So just adding some extra information...
I don't have Notepad++ installed as my favorite text editor is UltraEdit (shareware).
Although Stack Overflow is not a free code writing service and we expect that the questioner shows us some programming efforts already made to solve a task, it was very easy for me to write the little UltraEdit script for this task and therefore here is an UltraEdit script for this task.
C:\\Temp\\Test\\ at top of the script must be replaced by path of parent folder for the *lyrics folders. UltraEdit scripts are executed with the JavaScript core engine. Strings in UltraEdit scripts are therefore JavaScript strings where backslash is the escape character. So it is necessary to escape each backslash in parent folder path by one more backslash.
To run this script in UltraEdit, open Artists.txt as first file in UltraEdit.
As second file create a new ASCII file with Ctrl+N, copy and paste the lines below into this new file, edit the parent folder path/name in script code and save this script for example with name KeywordsReplace.js into any folder.
Now run the script by clicking in menu Scripting on command Run Active Script.
You can see after script finished in automatically showed output window how many replaces have been made in which Keywords.txt files.
if (UltraEdit.document.length > 0) // Is any file opened?
{
// Parent folder containing all the *lyrics folders.
var sParentFolder = "C:\\Temp\\Test\\";
// Define environment for this script.
UltraEdit.insertMode();
UltraEdit.columnModeOff();
// Select everything in first file.
UltraEdit.document[0].selectAll();
// Is first file not an empty file?
if (UltraEdit.document[0].isSel())
{
// Determine line terminator type for first file.
var sLineTerm = "\r\n";
if (UltraEdit.document[0].lineTerminator == 1) sLineTerm = "\n"
else if (UltraEdit.document[0].lineTerminator == 2) sLineTerm = "\r"
// Get all lines of first file into an array of strings
var asArtists = UltraEdit.document[0].selection.split(sLineTerm);
// Remove last string if it is empty because file ended with
// a line termination.
if (!asArtists[asArtists.length-1].length) asArtists.pop();
// Define once the parameters for all the replace in files executed
// below in the loop with changing directory and replace strings.
UltraEdit.frInFiles.filesToSearch=0;
UltraEdit.frInFiles.searchSubs=false;
UltraEdit.frInFiles.ignoreHiddenSubs=false;
UltraEdit.frInFiles.openMatchingFiles=false;
UltraEdit.frInFiles.searchInFilesTypes="Keywords.txt";
UltraEdit.frInFiles.regExp=false;
UltraEdit.frInFiles.matchCase=true;
UltraEdit.frInFiles.matchWord=false;
UltraEdit.frInFiles.logChanges=true;
UltraEdit.frInFiles.useEncoding=false;
UltraEdit.frInFiles.preserveCase=false;
// Run for each artist a replace of all occurrences of _1
// in the artists lyrics folder by name of the artist.
for (nArtist = 0; nArtist < asArtists.length; nArtist++)
{
// Build folder name by converting artists name to
// lower case and replacing all spaces by hyphens.
var sFolder = asArtists[nArtist].toLowerCase().replace(/ /g,"-");
// Define directory for replace in files by appending
// additionally the string "-lyrics" to folder name.
UltraEdit.frInFiles.directoryStart = sParentFolder + sFolder + "-lyrics\\";
UltraEdit.frInFiles.replace("_1",asArtists[nArtist]);
}
// The output window contains the summary information
// about the replaces made and therefore open it.
UltraEdit.outputWindow.showWindow(true);
}
}
Script was tested with the provided data with each Keywords.txt containing exactly 3 times _1 in the 6 *lyrics folders. Result of output window was:
Running script: C:\Temp\KeywordsReplace.js
============================================================
C:\Temp\Test\butta-mohamed-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daler-mehndi-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\daljit-mattu-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\darshan-khela-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deep-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
C:\Temp\Test\davinder-deol-lyrics\Keywords.txt, 3
3 items replaced in 1 files.
Script succeeded.
In case of downloading and installing UltraEdit is not acceptable for you, you have to wait for another answer providing a batch file solution or a Notepad++ macro solution, or you make the necessary code writing by yourself.
Related
I have created cmd batch file to run several R script and each script have their own log files in same folder like below :
coc_prod_xgb.log
ds_prod_xgb.log
ccpa_prod_xgb.log
pletb_prod_xgb.log
and many more
Then I merge all text files into 1 log file
copy *.log all_log.log
The problem is sometimes there are errors on different jobs, so I need to know on which log this error occurs. Currently I have to open each log file one by one, because in the merged log file, I can't identify which log file that has this error.
How to modify above copy code so it will write file names on the 1st row then the next row will be log information and append the same process to next file
I can't find any option for cmd copy code to write file name, so i find another solution from this question.
Easiest way to add a text to the beginning of another text file in Command Line (Windows)
so it serve my needs, eventhough i have to write each block of code for each log file. but only for 1 time effort
Good evening,
I am currently working on a programm that takes information from a file into a Database, for testing purposes I used to open Testfiles in the classical way via IO:
function reader (file, delimeter)
local f = io.open(file)
for line in f:lines() do
lines[count] = splitty(line, delimeter)
count = count + 1;
end
end
(this part also containes the first part of a splitter)
But in the actual environment, the database programm imediatly moves the file in another directory with a name change to, for example this:
$30$15$2016$09$26$13$27$24$444Z$.Pal.INV.csv
Now I know the directory but I can't really predict the name, so I wanted to know if there might be a way to open files without knowing their name.
(and delete them after reading them)
I had ideas to use a modified link:
local inputFile = "D:\\Directory\\(*all)"
but it failed.
Other aviable information:
The system is until now only planned on Windows PCs.
The directory will always only contain the one file that is to ready, no other files.
You can use the lfs.dir iterator from LuaFileSystem to iterate through the contents of the directory. A small example:
local lfs = require("lfs")
local path = "D:\\Directory\\" -- Your directory path goes here.
for filename in lfs.dir(path) do
print(filename) -- Work with filename, i will just print it
end
If you keep a record of the files you will be able to know which one is the new one. If it is only one file, then it will be easier, you can just check the extension with a string function. From what i remember the iterator includes .. and .. lfs documentation can be found here.
-- directory name and file name should consist of ASCII-7-bit characters only
local dir = [[C:\Temp\New Folder]]
local file = io.popen('dir /b/s/a-d "'..dir..'" 2>nul:'):read"*a":match"%C+"
if not file then
error"No files in this directory"
end
-- print the file name of first file in the directory
print(file) --> C:\Temp\New Folder\New Text Document.txt
I am writing a batch script that works as follows:
Goto certain location (for example C:/Temp/1)
Open the first 5 sub-directories of 1 (for example if 1 contains directories called 10,20,30, open 10. Then open 10 and if 10 also contains 10,20,30 open 10 as well, and goes on till --> 1/10/10/10/10/10)
List item copy all of the contents of the current folder(10) back to C:/Temp
Go back to C:/Temp and delete the first folder's files (1)
Now, i encounter a problem that i don't know how to open the directories
inside 1, since i don't know their names.
I am sure there is a way of opening the first folder, since i saw
a close example here
Thanks guys!
I am new to Powershell but have had experience with various programming languages including dBase III, basic, Fortran (shows my age). I am a musician and launch MP3 files as an accompanyment when I perform live, somewhat like karaoke. I have about 45 different MP3s that I use. I like to change the sequence of the songs that I perform, from time to time and this requires renaming the MP3 file names with a preceeding sequence number. The file names take on the form: "01 songnameA.mp3", "02 songnameB.mp3", "03 songnameC.mp3", etc. I also repeat songs so there might be an "09 songnameA.mp3" in the folder that I play back from. To do this, I typically create a text file that lists the songs in the order that I want to perform them. Each line in the text file has the form "01 Songname X" but no MP3 extension. I then manually copy the mp3 files into a folder and then edit the names applying a sequence number according to the text file. This is time-consuming.
I have created a Powershell script (version 2) that creates an array of the text file content and an array of the unnumbered MP3 song files. The script creates a 3rd array containing numbered MP3 filenames according to the sequence in the text file. This array does work and I can easily display the list of items in it which have the form "01 SongnameX.mp3". However, I have been unable to copy this array of MP3 filenames with a preceeding sequence number into another folder. I don't know how many variations of the "Copy-Item" statements I have tried but nothing works. The name of the array that contains the filenames is $nsfarray (new song file array). The command I am presently using:
$nsfarray | copy-item -Destination C:\temp
Returns the following error message:
Copy-item : Cannot find path 'C:\Users\My HP\Documents\My Scripts\01 A Good Time.mp3' because it does not exist.
The path is the default path that I use to run Powershell but somehow the MP3 file names get appended to it. The "01 A Good Time.mp3" is the first item in the $nsfarray. I know I am missing something here. Any suggestions would be appreciated.
This is really crude but the issue I see with your input file is that it obviously does not exactly match the real file name that you are looking to copy. So with that in mind lets try this on for size.
$musicPath = "c:\music"
$destinationPath = "c:\songset"
$nsfarray = "01 Age of Aquarius"
$nsfarray | ForEach-Object{
If($_ -match '^\d+ *(?<BaseName>.*)'){
Copy-Item "$musicPath\$($Matches.BaseName).mp3" -Destination "$_.mp3"
}
}
Work with explicit paths: $musicPath and $destinationPath so that we do not have to rely on our current location in PowerShell. Then we navigate each value in the array, which I have populated with one example. Now we need to extract the real file name away from your set list number. Using regex we return the part of the string that is not the beginning numbers and spaces and take everything after that.
After we simply copy the file that exists in the $musicPath folder that has the name "Age of Aquarius.mp3", which should be the real file, and copy it to the $destinationPath as "01 Age of Aquarius.mp3", which is how you want the file for your gig.
Clarification
After answering this and looking back at the question I think there is a chance I didn't understand it right. Please update the question if this is not the right way to address this. The core of your problem, if nothing else, is that you are not specifying a proper file path for the mp3s. You need to use the proper source folder and append the mp3 to the string.
I am trying to load data from text files to database. My source files contain null character NUL somehow (Picture1).
I just make all the fields as one column (delimited with {CR}{LF}). Then I do the preview of the data.
The data is just what we need.
But then when I run the package, the data changed, not like what I see in data preview. I added a data viewer to see the data.
The number 1 disappear in the first row (see the red). It seems that flat file reading ends at NUL character. But my Row delimiter is {CR}{LF}, it doesn't make sense the number 1 in the end disappear. Can anyone tell me why is that?
Reproducing the error
First of all, I would like to show the steps to reproduce this error using Notepad++ editor.
I created a text file called TestNUL that contains data similar to the screenshot posted in the question (commas are placed where NUL objects should be):
Now, Go To Edit menu strip >> Character Panel
Now the ASCII character panel is shown, double click on the NULL value in order to add it to the text:
Now the text file will looks like:
You can use the following link to download the file:
TestNUL.txt
Removing NUL character using Notepad++
To remove this character you can simply open Notepad++, Click Ctrl + H to open the Find and Replace dialog. Then select to use Regular Expressions and replace \x00 with an empty string:
All NUL characters are removed:
Find and replace in multiple file
If you are looking to find and replace this character in multiple files, then you can use notepad++ to do this using Find in Files feature:
How to find and replace line(s) in multiple files using Notepad++?
How to Find and Replace Words in Multiple Files
Automating the process Within SSIS
Since the issue occurs at run-time not while previewing data, you can simply add a Script Task before the data flow task to replace all \x00 values with an empty string. You can read the text file path from the flat file connection manager or you can store it in a variable. You can use a similar C# code:
public void Main()
{
string FilePath = Dts.Connections["SourceConnection"].ConnectionString;
string text = System.IO.File.ReadAllText(FilePath);
text = text.Replace(Convert.ToChar(0x0).ToString(), "");
System.IO.File.WriteAllText(FilePath, text);
Dts.TaskResult = (int)ScriptResults.Success;
}
If you are working with large text files then you can use System.IO.StreamReader and System.IO.StreamWriter classes to read the file line by line using ReadLine() function.
How to read a large (1 GB) txt file in .NET?
How can I read, replace and write very large files?
Experiments
I created a package and added two flat file connection manager, the source reads from TestNUL.txt file and the destination create a new TestNUL_edited.txt file with the same structure. I added a Script Task with the code above and added a data viewer in the Data Flow Task, the following screenshot shows how the rows are not corrupted:
Also the following screenshot shows how the NUL values are removed from the source file after running the Script Task:
References
Notepad++ showing null values after crash
How to Insert a Null Character (ASCII 00) in Notepad?
What does \x00 mean in binary file?
Find/Replace nul objects in Notepad++
Removing "NUL" characters
How to Find And Replace Text In A File With C#
Map every field in his column (using Tab {t} as column delimiter I suppose, and {CR}{LF} as row delimiter) and try again.