I can't seem to write certain characters (inverted question marks, fancy single quote marks, ampersands) to a text file, and then to search that file for those characters. For example the following findstr doesn't find the upside down question mark item in .txt:
#echo off
echo "Cato Event - GO Beyond GDP. What Really Drives the Economy¿">c:\test.txt
findstr /I /N /C:"Cato Event - GO Beyond GDP. What Really Drives the Economy¿" c:\test.txt
pause
::chcp 1254
I've tried with various chcp commands also to no avail.
Any help appreciated.
That is a known issue with FINDSTR - Some characters provided on the command line with ANSI byte codes > 127 are transformed into a different character by FINDSTR, prior to doing the search, which causes the search to fail.
The solution is to put the search string(s) in a file and use the /L and /G options.
See the section titled "Character limits for command line parameters - Extended ASCII transformation" at What are the undocumented features and limitations of the Windows FINDSTR command?
The only other option (assuming you want to stick with native batch commands) is to use FIND instead. It has much less functionality, but it does not have the character translation issue, and I believe it should work for your simple literal search.
find /I /N "Cato Event - GO Beyond GDP. What Really Drives the Economy¿" c:\test.txt
The line numbers at the beginning of each matching line will look like [123] instead of 123:.
Related
This is how I have always remembered processing the CONTENTS of a file line-by-line in a batch file (backed up by web search):
for /F "delims=" %%N in ("del files.txt") do del "%%N"
It now deletes "del files.txt" and #echo %%N gives "del files.txt"
Does it have something to do with the name needing to be quoted because of the space? WTF am I missing?
("string") is used to parse strings. ("filename") can be used with usebackq. It seems you confused them in your search. So, you can write:
for /f "usebackq delims=" %%N IN ("del files.txt") do whatever you want.
Please don't accuse me of not reading help.
for /? (first thing I did) yields:
FOR /F ["options"] %variable IN (file-set) DO command [command-parameters]
FOR /F ["options"] %variable IN ('string') DO command [command-parameters] MAKE NOTE THAT 'string' IS IN SINGLE QUOTES!!!
FOR /F ["options"] %variable IN (command) DO command [command-parameters]
because I just happened to lose the "if usebackq..." line because it just happened to get lost coinciding withe the "press any key..." line in my command prompt window because it just happened to be sized unfortunately (and still doesn't show file-set in standard quotes).
I was asking why quotes wouldn't be used in (file-set) when they are required for spaces and used in (file-set) without /F (i.e. for \%f in ("c\:path w spc\*.txt") do...)
I have used that exact syntax in other working batch files. though I went and checked an in each case, %1 or an environment var were in () which apparently settled how quotes were addressed.
#1 google search result: ss64.com/nt/for_f.html which in my frustration I misread the usebackq in reverse and tried ` because it went on to say
"The backquote character ` is just below the ESC key on most
keyboards"
, indicating to me that I was supposed to use that (once or twice) as quotes as seems rational to me if I'm giving direction to use that character.
I am providing this clarification in case it helps others experiencing my frustration.
Those who have "graded me down" for what I believe is legitimate confusion by doing so also prevent me from asking a different question in a totally unrelated area and I would appreciate if they would re-evaluate. I have certainly learned my lesson about unfortunate series' of events.
I understand the reasons that underlie the policy but am very frustrated that 1 time in the years I have been a member being criticized, just for poor luck IMHO, for a question should penalize me apparently 4 or 5 days.
(It's also confusing since since the message says
It's been 2 days since you asked your last question. We ask that you wait 2 days before asking again. Use this time to revisit your previous questions, editing to address any issues that folks have pointed out in comments.
Does that mean 2 more days or the two I have waited?)
So I guess if people want to pound on me for this protest, how many more days can it cost me?
And a further Thanks! to double-beep for the useful answer even if it wasn't a complete explanation.
Well, back to the penalty box...
I have the following entry in the file Build.aip. I need to write a batch file that searches for "PackageFileName and prints the value assigned for that in the file. In this case, I need to print MyPackageName on the console:
<ROW BuildKey="DefaultBuild" BuildName="DefaultBuild" BuildOrder="1" BuildType="0" PackageFolder="C:\Build\Build.aip" PackageFileName="MyPackageName" Languages="en" InstallationType="4">
May you please give me some examples how I can do that? I seen in some forums that this can be done using FINDSTR.
Thanks in advance.
findstr "PackageFileName" Build.aip
If you want to make it case insensitive, add the /i argument.
For more details, type findstr /?
Updated for an example to use the for statement
FOR /F "token=2" %i in (`FINDSTR "PackageFileName" Build.aip`) do SET var=%i
A couple things to keep in mind here:
This version is based on Windows 8.1; it may work differently in different versions of Windows.
The token=2 is an example assuming that the word you are looking for is the second word in the line
If PackageFileName appears more than once in Build.aip, this code will break.
The findstring command is surrounded by back-ticks, not single quotes.
I haven't tested it; the SET may not actually survive the for loop. So test!
If you use it in a batch file, you must double all the % signs.
So basicly, is there any way to make it so a Batch file doesnt need an exact answer to do an action. For example:
if %c%=images goto :images
The above means that I must type in the exact word, images, and if I add any more words it wont go to the :images label. How would I make it to go to a label using just a word from a sentence that you input.
For example if it asks me:
Where would you like to go?
And I say:
The image section please.
Then it takes me to the image section without the need for just saying image.
Is this possible?
You could use find.
echo %c% | find /i "image" >NUL && goto images
That'll also provide case-insensitive matching. Furthermore, you can similarly use findstr to match based on a regular expression if you need finer tuning of your match.
If you're curious about how the | and && above do their magic, see command redirection for more info.
There's a similar question here: Batch file: Find if substring is in string (not in a file)
The accepted answer could work in your case:
if /i "%c:image=%" neq "%c%" goto images
This technique uses string substitution to replace the word image with nothing (strip the word from the string you're searching), then compare it to the original string. If they're different, then the substring was found.
I have to run script that find find partition with more than 1 GB free space.
I wrote the following:
#echo on
set isComplete = 0
set last = 0;
DISKPART /s .\script.txt > .\log.txt
for /f "tokens=3" %%f in ('.\log.txt') do (
FIND /C /I "GB" .\log.txt > %isComplete%
IF %isComplete% > 0
%last% = %%f
)
Dispart call to another script that prints to log.txt all the availble partitions.
Log.txt equals to:
Partition ### Type Size Offset
Partition 1 Primary 74 GB 1024 KB
Partition 2 Primary 300 MB 74 GB
now, i want to check which parttion has more than 1 GB free space, so I look foe "GB" word.
but I always get: > "was unexpected at this time." error.
can anyine help me to figure out what the problem? or suggest me a better way to do that?
thanks!!!
Ouch.
You've got a number of problems.
The output of LIST PARTITION gives the size and offset of each partition. I'm not very familiar with DISKPART, but I don't believe the size gives the amount of free space within the partition. It indicates how large the partition is, but gives no indication of how much space has already been allocated to existing files. The offset indicates the location of the partition. So it seems you are off on the wrong foot right from the get-go.
That being said, you have some major syntax problems in your script.
It looks like you are trying to read a file in your FOR statement, but in reality your syntax is attempting to execute a command because you enclosed the file name in single quotes. You need to type HELP FOR from the command line and read carefully the description of the FOR command.
You want either
for /f "tokens=3" %%f in (log.txt) do (
or, if the filename contains spaces (which yours doesn't), then
for /f "usebackq tokens=3" %%f in ("file name with spaces.txt") do (
Your line FIND /C /I "GB" .\log.txt > %isComplete% is attempting to write the output of the FIND command to a file named "0". I doubt that is your intention. The FIND command will print out the complete text of any line that contains your search string of "GB". It will also set the ERRORLEVEL to 0 if it finds at least one matching line, or 1 if it fails to find a matching line. I'm not sure what you are attempting to do.
Your IF statement is a mess. Here again you need to type HELP IF from the command line and read carefully how to use it. Your existing attempt has many errors
You can't extend an IF statement accross multiple lines unless you either use line continuation ^, or parentheses.
You can't use > as a comparison operator, you need to use GTR
I can't figure out exactly what comparison you are trying to make.
I don't know how to point you in the right direction because I don't understand what your overall mission is. Perhaps if you describe in more detail what you are trying to do, someone can provide a script that will accomplish the task, and you can learn from that example.
DISKPART is a potentially dangerous tool. I don't see how you can do harm with the LIST command, but other commands could create problems if misused. While you are learning the rudiments of batch scripting, I suggest you avoid DISKPART. I would hate for you to accidently corrupt your hard drive because of a batch programming mistake.
By using the command from a folder in D: drive
for /f "delims=" %%d in ('cd') do set pathdrv=%%d
echo %pathdrv%
I get "d:\some folder".I want to write batch commands to create autorun file in the root of the drive. Please help me to strip the drive letter "d:" so the output is "\some folder" and what extra change do i do to strip "\".
Short answer: Use the substring syntax to strip the first two characters from the %cd% pseudo-variable:
%cd:~2%
To remove the first backslash too:
%cd:~3%
This reliably works even with Unicode paths when the console window is set to raster fonts.
Longer answer, detailing some more options (none of which work well enough):
For arguments to the batch file you can use the special syntax %p1, which gives you the path of the first argument given to a batch file (see this answer).
This doesn't work the same way with environment variables but there are two tricks you can employ:
Use a subroutine:
call :foo "%cd%"
...
goto :eof
:foo
set result=%~p1
goto :eof
Subroutines can have arguments, just like batch files.
Use for:
for %%d in ("%cd%") do set mypath=%%~pd
However, both variants don't work when
The console is set to "Raster fonts" instead of a TrueType font such as Lucida Console or Consolas.
The current directory contains Unicode characters
That have no representation in the current legacy codepage (for Western cultures, CJK is a good choice that doesn't fit). Remember that in this case you'll only get question marks instead of the characters.
The problem with this is that whil environment variables can hold Unicode just fine you'll get into problems once you try setting up a command line which sets them. Every option detailed above relies on output of some kind before the commands are executed. This has the problem that Unicode isn't preserved but replaced by ?. The only exception is the substring variant at the very start of this answer which retains Unicode characters in the path even with raster fonts.
Drive letter:
%CD:~0,1%
Full drive name (incl. colon):
%CD:~0,2%