Windows Command Line: Search and replace value - windows

I'm trying to find and replace the value of something in a text file and am having trouble figuring out how I would do that.
For example, I would like to go into a file that has android:versionName="1.53" and replace it with android:versionName="1.54".
I know how to do this if I just want to replace 1.53 with 1.54 for example, using a program called FART (find and replace text) but I want to be able to replace whatever is in between the speech marks with 1.54, i.e. without having to specify 1.53 as this could change each time.
Another similar example would be replacing #define DLS_VERSION (1540) with #define DLS_VERSION (1600).
I was just wondering if there's a standard way or utility to do this please?
Thanks,
Chris.

Any regular expression (regex) search and replace utility should work just fine. There are many free options for Windows, though none native to batch.
One good option is REPL.BAT - a hybrid JScript/batch utility that works on any Windows machine from XP forward, and does not require download or installation of any executable.
Assuming REPL.BAT is in current directory, or better yet, somewhere within your PATH:
type "yourFile.txt"|repl "(android:versionName=\q).*?(\q)" "$11.54$2" x >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul
type "yourFile.txt"|repl "(#define DLS_VERSION \().*?(\))" "$11600$2" >"yourFile.txt.new"
move /y "yourFile.txt.new" "yourFile.txt" >nul
Full documentation is embedded within the REPL.BAT script. A simple web search can find many sites that explain regular expressions.

Related

What means tilde in windows file pattern

I have pattern to search. Say "*.txt".
Now I have some files I do not want to list there. I believe they do not match this pattern.
But on windows, they do.
I know tilde character is used to make short form of legacy 8.3 filename. That is LongFilename.json might be LONGFI~1.JSO. But I did not know they are handled somehow on windows in file search patterns. They are. I cannot find any documentation about what they mean and how to match files my way.
My problem is NOT with short forms. Or I think it is not directly related to it.
I have file "A.txt". Now I wanted temporary file and used "A.txt~". It is unix backup files that is not usually visible. But on windows, they should not have special meaning by itself. Only for my application.
Now I want list of "*.txt" files. Command
dir *.txt
returns to my surprise also all .txt~ files in the same directory. And I do not want them. I use FindFileFirst from Win32 API. I did not find anything about tilde character in documentation. FindFileFirst(".txt", handle) returns also files "A.txt~". Can I use some flag to exclude them? I know I can make special condition, like I have for "." and "..". How does ~ operator work? A.txt~1 is also matched. Is everything after tilde ignored? Is that feature or bug?
I am testing that on Windows 7 Professional, 64 edition, if that changes anything.
FindFirstFile also includes short names for legacy reasons so the pattern *.txt will include anything with an 8.3 representation ending in *.txt which includes *.txtANYTHING , not just the ~ character (see dir /xfor what's being matched against).
You will need to filter in your FindNext enumeration.
If you are searching for .txt files for example, you can use "kind:text" option in windows to exclude txt~ and similar files since they are not a recognized type anymore.
That's something that works on regular windows search. I'm not 100% sure about the API, but it should also be there.

BATCH: How to capture the extension of the file, or how to do a pattern test like regexp

I'm writing a batch file for windows command prompt, I want to loop through files in a directory recursively using FOR /R .
However, inside the loop I want to check if the file of extension .txt or possibly other extensions in order to skip.
How can I do that? in FORFILES there is #ext what to use with for?
Also, is there something similar to regexp in command prompt?
%~xV will expand variable V to an extension only. If you think you need regexp, then ditch the batch altogether, and use e.g. Python, otherwise you're only going to be writing unmaintainable mess.
Also, for the love of Cthulhu, console window is not even remotely related to MS-DOS.
Complementing Cat++ answer, for the second question, FINDSTR command accepts regular expressions as search strings.
Although it is not a full featured regexp command, for search and replace, it is pretty useful in many simple situations.
Try HELP FINDSTR
Or for an introduction and a list of the supported regex expressions by FINDSTR see this doc http://technet.microsoft.com/en-us/library/bb490907.aspx .

Registry does not like long filename for shell commands or verbs

(if not applicable to SO, please refer to another appropriate place, thanks).
When using the registry to associate file extensions and application, I put in the full filename of my application, but that does not work well, only if I use the 8.3 filename.
for example ( taken from the registry) this works:
[HKEY_CLASSES_ROOT\Toto.Document\shell\myVerb\command]
#="C:\\my\\path\\bin\\Debug\\bin\\myexe_~1.EXE /dde"
[HKEY_CLASSES_ROOT\Toto.Document\shell\myVerb\ddeexec]
#="[myVerb(\"%1\")]"
but this does not work :
[HKEY_CLASSES_ROOT\Toto.Document\shell\myVerb\command]
#="C:\\my\\path\\bin\\Debug\\bin\\myexecutable.EXE /dde"
[HKEY_CLASSES_ROOT\Toto.Document\shell\myVerb\ddeexec]
#="[myVerb(\"%1\")]"
The action is called by right-clicking on the file in Explorer, I get the error :
"Windows cannot find 'c:\users\me\desktop\tata.toto'. Make sure you typed the name correctly, and then try again".
I'm creating the keys programatically with CRegKey and using GetModuleFileName to get the application path.
2 questions :
- I'm probably missing something in my registry entry ? (i've tried quoting the paths, but does not work)
- Can I get the "short" filename ? (searching a little bit seems that GetShortPath should work, but not always!)
Thanks.
Max.
(edit 22/03/2011)
I tried using quotes but it did not work (with /dde)
I decided to use normal parameters instead of /dde and it seems to work nicely with the normal path (not shortened like stated above).
I'm still not certain why when creating a simple MFC SDI project it will write out registry values with the old short name instead of the long name.
Thanks again.
Max.
Try creating the key with another couple of double-quotes (note between .EXE and /dde:
#="C:\\my\\path\\bin\\Debug\\bin\\myexecutable.EXE" "/dde"

Executable Files - how to identify them in ASCII

It looks like all EXE files begin with MZ when they are opened in ASCII mode, is there an ASCII identified for vbs, com and bat files as well? i can't seem to find a pattern...
Or maybe there's another way to identify them? aside from just the extension...
No, not really (Windows executables can have PE or PK at the beginning instead of MZ - see this for other possible formats).
For other types of files, there are certain heuristics you can use (e.g. GIF files start with "GIF89", Bash shell scripts usually start with #!/bin/bash, BAT files often execute #echo off at the beginning, VBS scripts use apostrophe at the start of line as a comment marker), but they aren't always 100% reliable (a file can be both a BAT script and a Bash shell script; or a file that's both a valid ZIP archive and a valid GIF image (like that stegosaurus image), for example).
See e.g. this article for further reading.
TrID seems to have a "standalone" application you could probably use and pass the file in and read the contents out and see what file it is. It prides itself on the ability to pass it a generic file (extension or without) and it uses the headers of the file to discover what file type it actually is.
See if this tutorial is helpful (How to detect the types of executable files 3 part series). He has even presented a step by step algorithm on how to do this.
Also see this post: How to determine if a file is executable?

Is it possible to use special characters to represent a space in Windows?

We are using a proprietary application for inventory management and have discovered this application is unable to interpret spaces in file paths. For example:
C:\Google Drive\Invoices
Does not work, whereas
C:\Google\Invoices
does work.
Is there a special way to represent a space in Windows much like a URL string can use %20? For example C:\Google%20\Drive\Invoices.
Use 8.3 short name.
Try dir /x c:\
Google Drive should have a short name, probably like GOOGLE~1
Then you can use C:\GOOGLE~1\Invoices
You can use short names if supported. Type dir /x and it's in the middle column.
However it only works if short names aren't turned off. If short names aren't available the only way is making a junction point or a symbolic link1.
Run cmd as admin and type either of the following
mklink /J C:\ggdrive "C:\Google Drive"
mklink /D C:\ggdrive "C:\Google Drive"
This will create a link from ggdrive to the real Google Drive folder and now you can access Google Drive as ggdrive
However it's highly probable that you've used the path incorrectly. In some places you need to quote paths with spaces like this "C:\Google Drive\Invoices". But if an application in the last 15-20 years doesn't support long file names then it is rubbish anyway. Use a better program or report to the developer to fix it.
1 The differences between them is like this
What is the difference between NTFS Junction Points and Symbolic Links?
“directory junction” vs “directory symbolic link”?

Resources