Simple way to delete one line from file using command prompt - batch-file

I am writing a simple batch script, and I need to delete one line from the file that gets downloaded by the script. What is the easiest way to do this using ONLY the command prompt? I have come across several various suggestions, but nothing to very simply delete one string that is constant across all files that are downloaded with the script.

This is a very platform dependant question. If you are using a *nix environment (Linux / Mac environment using bash / shell), you can accomplish this with sed
sed '/${regular_expression_that_matches_line_to_be_removed}/d' yourFile.txt > newFile.txt
This will generate a new file called newFile.txt that will contain the output. You can also do this in place (it modifies the file it's using as input), but I recommend against that because if you mess up your regex, you've lost your input.
If you're using a Windows environment (which I assume you are due to your batch-file tag), try looking at this Delete certain lines in a txt file via a batch file

Related

Stata .do file execution by CMD, but fail due to accented characters in the path

I am trying to run a Stata do file from windows command line but it doesn't execute as the directory name contains space and accented characters. Unfortunately, I can't rename the directory as it is a corporate standard.
You can see the command here:
cmd
"T:/Stata14/StataMp-64.exe" /e do "Y:/do/mydofile.do"
The "mydofile.do" contains the following simple code:
clear
set more off
sysuse auto
save "Y:/állé test/test.dta"
The script is working perfectly fine using Stata GUI, but Windows seems to be more sensitive to special characters (If I change the save location to "Y:/alle_test/test.dta" also works, but as I said it is prohibited).
Is there any solution to that?

Is it possible to embed an executable inside a batch file?

There are many programs which help me write more efficient code (NirCmd etc.), but they can't run properly if they aren't installed in the computer. So is there a way to, for example, in the temp folder, extract the program from a batch program and use it.
I tried reading the executables with a hex editor, putting the hex code into another file and saving it as an executable. But this failed. So, is there any way to efficiently store an executable inside a batch file, create it and then run it?
Yes, there is one that I know of and works perfectly.
It is a program called bhx.exe (link to its site here).
It can also embed other file types.
The usage is quite simple:
(optional) Create a cabinet (.cab) file from the original .exe using this command: makecab yourexe.exe yourexe.cab. For better compression you can use the /D switch in this way: makecab /D CompressionType=LZX yourexe.exe yourexe.cab
CD to the directory bhx is in and do this: bhx yourexe.cab. Other switches are described in the website.
There you go, the mybin.cmd file is generated.

FOR /f in DOS alternative

As it appears there is no version of DOS (6.22 to WinME "DOS") or FreeDOS that allows you to take part of a text file and make it a variable, I'm going to just keep collecting the data I get in DOS mode into one very large file but I can't think of a way to get each asset and UUID and add them together in a third file... Here is what I get at the moment:
SMBIOS.TXT
~~~~~~~(usually 27 lines of stuff I don't need)~~~~~~~
Asset Number: ABC12345
~~~~~~~(usually 37 lines of stuff I don't need)~~~~~~~
UUID: ABCDEF12345678901234567890
~~~~~~~(usually 4 lines of stuff I don't need, complicated by a # symbol in there too)~~~~~~~
^Repeated many times
I need to add both the Asset Number and the UUID together in a CSV format so I was previously hoping (before I exhausted all attempts at doing for /F in DOS) just echoing the two variables I was creating as follows:
ECHO !Asset!,!UUID!>>Results.CSV
Which again works in Windows command prompt just not DOS, the script I'm using however only gets the first variable of each so I need to do them in order and keep repeating through the file in the manner?
Alternatively is there any other way I could use DOS to get the info I need out of the two text files on the fly? It's all running from a USB stick so I don't have any size constraints at least.
Aaron
You may be able to get it done with the DOS command line text stream editor EDLIN.
I am assuming this has to be done in DOS and Windows is not an option.
PDGREPPE is an MS DOS command line GREP search and replace utility.
Maybe you can find a DOS Text Editor with Macros
Lotus 123?
DBase?
Write an app in DOS BASIC.

Find multiple files from the command line

Description:
I am searching a very large server for files that is on a different server. right now I open command prompt and type
DIR [FILE NAME] /S/4
This returns the server location of the file with some other stuff that is not really needed.
Question:
I have a lot of files to search and one by one input into the above command could take forever. Is there a way I could input all of the names of all the files and only search once and the search results would only need to show file name and location?
First, I hope you don't mean DOS, but rather Windows cmd or batch.
You can certainly write a script that will run your DIR command once per file being sought.
But what you most likely want instead is to search once and print the path of each file found. For this you can use PowerShell's FindChildItem or the improved one posted here: http://windows-powershell-scripts.blogspot.in/2009/08/unix-linux-find-equivalent-in.html
It will be something like:
Find-ChildItem -Name "firstfile.txt|secondfile.txt|..."
Another approach is to install msys or cygwin or another Linux tools environment for Windows and use the Linux find command.

Combining files in Notepad++

Is there a Notepad++ plugin out there that automatically combines all currently opened files into a single file?
Update: Yes, I am very aware of copy and paste :) I'm working with lots of files, and I want a solution that makes this step in the process a little bit faster than several dozen copy and pastes.
I'm aware of utilities for combining files, but I want the convenience of combining specifically the files that are currently opened in my text editor.
If there isn't a plugin out there already, I'll write one myself; I was just wondering if it exists already to save me the time of developing one.
I used the following command in DOS prompt to do the merge for me:
for %f in (*.txt) do type "%f" >> output.txt
It is fast and it works. Just ensure that all the files to be merge are in the same directory from where you execute this command.
http://www.scout-soft.com/combine/
Not my app, but this plug in lets you combine all open tabs into one file.
I installed the Python Script plugin and wrote a simple script:
console.show()
console.clear()
files = notepad.getFiles()
notepad.new()
newfile = notepad.getCurrentFilename()
for i in range(len(files) - 1):
console.write("Copying text from %s\n" % files[i][0])
notepad.activateFile(files[i][0])
text = editor.getText()
notepad.activateFile(newfile)
editor.appendText(text)
editor.appendText("\n")
console.write("Combine Files operation complete.")
It looks at all the files currently opened in Notepad++ and adds them to a new file. Does exactly what I need.

Resources