Batch FOR Loop Inserting a special character - windows

Im trying to run this Batch FOR Loop:
FOR %%f in (C:\folder\*.dwg) do start /wait c:\”program
files"\Autodesk\”AutoCAD 2014"\acad.exe "%%f" /b c:\Script\cgatt.scr
But for some reason when I run it instead of outputting c:"program files", it outputs a weird specialcharacter in place of the double quote:
Can anyone see what I'm doing wrong there?

The problem is with your first double quote in c:\”program
files"\Autodesk\”AutoCAD 2014"\acad.exe
It needs to be changed to a " like the other one is. The curved quotes are what is known as "smart quotes," which get added by some text editors automatically. Generally this can be avoided by coding batch scripts in text editors like Notepad or Notepad alternatives. There may also be an option in your text editor to turn smart quotes off. I highly recommend doing this, or else your scripts will continue to break.

Related

odd batch file programming error :Invalid argument/option: - 'eq'

I have a line of BAT code written like this:
tasklist /FI "IMAGENAME eq WerFault.exe"|find /C "WerFault.exe" > temp.txt
It went on well for quite a few days but suddenly I got this error message:
error :Invalid argument/option: - 'eq'
The code hasn't been changed before and after this error message, I can't figure out what's the problem.
You, or people coming here from a search, may have been trolled by text encoding madness and the borderline sabotaging behavior of cmd.exe, combined with websites such as this one.
What they did:
Omit one quotation mark
Replace the other quotation mark with Unicode character 201c
What's probably going on:
The user copy-pastes the line and gets an error
The user notices the missing quotation mark and adds it
It now looks identical to the other quotation mark, so there's no way to see what is going on.
Not only does it look identical, copy-pasting it back out, such as to Stackoverflow, replaces it with a standard, undirected double quotation mark! In other words, it is indistinguishable from the correct symbol even when copying it back out of the window for analysis!
The above is true even for copy-paste within cmd (which conveniently doesn't work with standard hotkeys) but not for bringing back a line via the up-key.
What I have once again learned from this:
Do not trust what you see when Unicode is involved.
Also, cmd.exe is generally not very safe or sane. When in doubt, re-type the command manually.
This might not be the cause here, but if others come here for the same reason as I, maybe this answer will save them a few minutes.

Space before command circumvents DOSKEY

I wanted keep my users from running "dir" in the command line, so I used DOSKEY to alias "Dir" to "CLS". The testers found out that putting a space before "DIR" will circumvent the alias.
I've tried to put a space before "DIR" when setting up the DOSKEY, but the command prompt ignores the white space.
Anyone found a way of making DOSKEY acknowledge spaces?
Thanks.
Deny your users the List Directory contents permission on all relevant locations. That's probably the easier way. That way they can run dir but it won't be of any use.
I'm not even trying to figure out why you want such a thing, though.
Regarding doskey: As you noticed, macro substituion is done literally and only at the beginning of the command line. So what do you want to do? Create macros for dir to cls with 1, 2, 3, ..., 8188 spaces before it?
Blacklisting almost never works, and it certainly isn't going to in this case. You can, for example, list the files in a directory simply by pressing TAB repeatedly.
Instead, use whitelisting. Write a console application that takes user input, checks that the input is a command that the user is allowed to run, and if so, passes that command to the shell - or, better still, implement the "approved" commands yourself, so that (a) there can't be any trickery with special characters, and (b) you can remove cmd.exe from the approved applications list - you are using software restriction policy, right?
Even if you could figure out how to make your DOSKEY macro idea work (I don't think you can), it would be pointless. Your users could easily circumvent the restriction by creating the following batch file:
#dir %*
DOSKEY macros do not work within batch files, so there is nothing to stop the batch file from executing. And your users could name the batch file anything they want, so you would have a devil of a time policing.

Windows batch script to move lines from txt file to new file

I have a load of text files with some html code in them. EG:
Some random text....
..
...
....
<tag1>some more random text</tag1>
....
...
..
I need to run a script to go through each text file and move each line between the tags to a new text file in the same folder, and remove them from the original.
So the end result would be one file with no <tag1> and another file with only <tag1>.
I hope I made myself clear enough. Is this at all possible?
IF (and that is a big, bolded, italicized, capitalized IF) you can guarantee that each <tag1>...</tag> tag appears on a single line with no other content on that same line, and there are no tag attributes to complicate things, then the answer is easy.
Edit - fixed a number of bugs, it actually works now :-)
#echo off
for %%F in (*.txt) do (
echo processing %%F
findstr /rc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.tag1%%~xF"
findstr /rvc:"^ *<tag1>.*</tag1> *$" "%%F" >"%%~nF.new%%~xF"
>nul move /y "%%~nF.new%%~xF" "%%F"
)
The solution could be extended to handle tag attributes fairly easily.
But I seriously doubt your problem is really that simple. Valid HTML content can have a tag spread accross many lines, and there can be many tags on one line.
Windows native batch is pretty lousy at text processing in general, and even worse for HTML or XML. I strongly recommend getting a third party tool like gnu sed for Windows that has robust text processing. Or better yet, get a tool that is specifically designed to process HTML.

String manipulation in Batch Files

I have a batch file question
Set "filename=C:\Documents\Example.doc"
I have a string %FILENAME% and I want to replace the C:\ with C::\, without just redefining it, can anyone help?
I'm not sure what you want to achieve here, and your variant of putting all of the set command in quotes is awkward, albeit valid, but anyway:
SET filename=%filename:C:\=C::\%
Or you just use the %filename:C:\=C::\% expression in places where you want the other value, without actually changing the content of the Filename variable.
For more details see (the output of) SET /?.

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 .

Resources