How to filter txt file by string with using batch - windows

I know this kind of problem will be very easy for you but I am total beginner in batch mode.
The question is how bat file code should look like to generate a new txt file from specific one .
for example .
I have report txt
Displaying status for license file: 7788#Server01
License Server: server01
License In Use Free
------- ------ ----
Design* 1 6
(user1#host1) 127 server01 7788 4402
Assembly* 0 4
Pro 0 15
AdvSE 2 3
(user2#host2) AdvSE server01 7788 2706
(user3#host3) AdvSE server01 7788 1503
SingleSite_License 1 3
(user4#host4) SingleSite_License server01 7788 2003
Intra_CLIENT_License 1 4
(user2#host2) Intra_CLIENT_License server01 7788 2003
CAD 1 32
^(user2#host2) CAD server01 7788 501
* = License Extensions - Available only on startup.
^ = Borrowed License.
Press any key to continue . . .
What I want to have in new file from this one are only lines :
SingleSite_License 3
Intra_CLIENT_License 9
but both SingleSite_License and Intra_Client_License should be taken from 1st found string -other are not necessary.
report txt can be different and licenses can be showed in different order .
if it not possible - other solution can be just only Free value will be written in new txt file - f.e 3 and 9 . so the last string in line which contains specific words
thank you for any tips

Not sure where you got the figure of 9 for the Intra_CLIENT_License (souldn't it be 4?), but this script will print out the free licences for those two product codes:
#echo off
setlocal enableextensions enabledelayedexpansion
for /f "tokens=1,3" %%a in ('type infile.txt') do (
if "x%%a"=="xSingleSite_License" (
echo %%a %%b
)
if "x%%a"=="xIntra_CLIENT_License" (
echo %%a %%b
)
)
endlocal
I basically cut and pasted your transcript into the infile.txt file and ran this script to get:
SingleSite_License 3
Intra_CLIENT_License 4
(you can replace type infile.txt with whatever command you need to generate that output).
Breakdown:
The setlocal just sets up cmd.exe to allow extensions and delayed expansion of environment variables. All my scripts start with this since it's so useful.
The for loop basically processes one line at a time, grabbing tokens 1 and 3 (a and b) from each.
Then it's a simple matter of checking a for the required licence values and, if it matches, outputting that along with b.

Write a C application. Batch is unnecessarily kludgy and probably can't even do this kind of stream manipulation.

Download UnxUtils and use the Unix stream processors. Might be possible to do with Powershell too.

Related

Fail to continue after for ..() do xyz command in Windows command shell

I have two Windows batch files, the first one contains a FOR looping command:
echo off
for /f %%1 in (somefile.txt) do something %%1
echo then more commands...
Then the second file something.bat contains a GCP BigQuery command, such as below. Notice the replaceable %%1 parameter supplied to it by the first batch file.
call bq query --use_legacy_sql=false "WITH intermediate AS (SELECT service.description AS SERVICE,
service.id as ID, invoice.month as MONTH, (SUM(CAST(cost * 1000000 AS int64)) +
SUM(IFNULL((SELECT SUM(CAST(c.amount * 1000000 as int64)) FROM UNNEST(credits) c), 0))) / 1000000
AS COST FROM myproject.billingexport.gcp_billing_export_v1_022EAF_F204503_AR2346 WHERE service.id =
\"%1\" group by 1, 2, 3) SELECT distinct SERVICE, (SELECT round(cast(COST as float64),2)
FROM intermediate WHERE MONTH = \"202001\") as Jan, FROM intermediate"
The commands work fine when executed one-by-one. But when the FOR command in file #1 completes, the rest of the batch file is skipped, and execution stops. I have tried various combinations of CALL (or absent that), or moving the >> output redirect from the FOR command versus in the something.bat. Nothing obvious seems amiss--must be something simple. Ideas?? Thanks.

Backup files on Windows 10 computer

I'm supposed to backup a Windows 10 computer. This means copying specific filetypes e.g. .pdf, .doc, .xls, .jpg etc.
I've tried with xcopy but the backup doesn't get complete due to the fact that the computer apparently has folderpaths that are longer than 254 characters.
Therefore I googled, and found Microsofts robocopy, which can handle that particular problem (among others).
Robocopy is a foldercopy utility and not so much a filecopy utility, but with some parameters it should be able to solve the problem.
I created a batch file witch contains these two lines (+ all the rest of the relevant filetypes)
robocopy c:\ d:\B20180602\ *.pdf /s /A-:SH
robocopy c:\ d:\B20180602\ *.doc /s /A-:SH
The odd thing is that it now copies without problems (regarding to foldernames larger than 254 characters), but the final result of this backup is incomplete. There are several subfolders that aren't copied.
Is there anybody that can give me a hint? Thanks in advance.
UPDATE #1 *
After #selbie's advice I ran this one particular command (as Administrator):
robocopy c:\ d:\B20180602\ *.pdf /s /A-:SH
With the following result:
Total Copied Skipped Mismatch FAILED Extras
Dirs : 761465 761351 1 0 113 0
Files : 1233 1233 0 0 0 0
Bytes : 1.464 g 1.464 g 0 0 0 0
Times : 1:58:47 0:00:50 0:00:00 1:57:56
Speed : 30918074 Bytes/sec.
Speed : 1769.146 MegaBytes/min.
Ended : 4. juni 2018 21:32:21
Elevating to Administrator changed a lot. I will make some statistical samples tomorrow and thereby answer whether the problems is solved.
Thanks in advance

win cmd: remove whitespace > not enough storage to process this command

I have a long (several million lines) data sheet in plain txt. Looks like this:
cellnumber x-coordinate y-coordinate z-coordinate temperature
1 -6.383637190E-01 2.408539131E-02 -5.244855285E-01 3.081549136E+02
2 -6.390314698E-01 2.286404185E-02 -5.245100260E-01 3.081547595E+02
3 -6.381718516E-01 2.373264730E-02 -5.236577392E-01 3.081547591E+02
4 -6.360489130E-01 2.259869128E-02 -5.245736241E-01 3.081547591E+02
5 -6.369081736E-01 2.253472991E-02 -5.236831307E-01 3.081547591E+02
6 -6.382256746E-01 2.215057984E-02 -5.237988830E-01 3.081547591E+02
7 -6.381900311E-01 2.126700431E-02 -5.245448947E-01 3.081547591E+02
8 -6.373924613E-01 2.117809094E-02 -5.238834023E-01 3.081547591E+02
I currently only have win command line and need to get rid off the whitespaces ath the beginning (their length is not constant as the cellnumber increases) so that I get
cellnumber x-coordinate y-coordinate z-coordinate temperature
1 -6.383637190E-01 2.408539131E-02 -5.244855285E-01 3.081549136E+02
2 -6.390314698E-01 2.286404185E-02 -5.245100260E-01 3.081547595E+02
3 -6.381718516E-01 2.373264730E-02 -5.236577392E-01 3.081547591E+02
4 -6.360489130E-01 2.259869128E-02 -5.245736241E-01 3.081547591E+02
5 -6.369081736E-01 2.253472991E-02 -5.236831307E-01 3.081547591E+02
6 -6.382256746E-01 2.215057984E-02 -5.237988830E-01 3.081547591E+02
7 -6.381900311E-01 2.126700431E-02 -5.245448947E-01 3.081547591E+02
8 -6.373924613E-01 2.117809094E-02 -5.238834023E-01 3.081547591E+02
May I ask for a solution? I dont have a clue, am not really experienced with this. Thx!
I guess TrimStart may be my friend.
EDIT: I have put together this:
#ECHO OFF
set "victim=testJana.txt"
SETLOCAL
FOR /F "tokens=*" %%A IN (%victim%) DO (
IF NOT "%%A"=="_" ECHO %%A>>%victim%_edited.txt
)
ENDLOCAL
pause
it works fine for smaller files but Im getting the message
not enough storage to process this command
Any idea how to deal with this?
I would suggest using powershell:
First, Second and Third edit: To be executed in the directory where data.txt file is placed and in the powershell.exe shell:
(good point to add -ReadCount by #lit in other post)
Get-Content -ReadCount 500 -Path .\path_to_your_source\data.txt | % {$_ -replace " +", " "} | Set-Content -Path .\path_to_our_output\data_no_additional_spaces.txt
Why -ReadCount makes sense? Here it takes 500 lines per run via pipes.
Here is the info from Microsoft pages)
-ReadCount
Specifies how many lines of content are sent through the pipeline at a
time. The default value is 1. A value of 0 (zero) sends all of the
content at one time.
This parameter does not change the content displayed, but it does
affect the time it takes to display the content. As the value of
ReadCount increases, the time it takes to return the first line
increases, but the total time for the operation decreases. This can
make a perceptible difference in very large items.
Reads data, replaces all the spaces and then saves data into data_new.txt
This answer was meant for the powershell.exe shell not the cmd.exe where you normally run your *.bat files. In powershell you have scripts called *.ps1.
If you store this the above command in a trim_space.ps1 and then launch it as (you need to have the script in the same directory as the data being transformed):
powershell.exe -ExecutionPolicy Bypass &'C:\path_to_script\trim_space.ps1'. You will see it executed.
Forth edit
To address your:
it works fine for smaller files but Im getting the message not enough
storage to process this command
Any idea how to deal with this?
You have to process it by chunks which you are not doing in your batch file right now. You get just to the point where you exhaust all the thread memory and it naturally fails. You need to have approach which allows you to limit the chunk of lines which are processed at once like -Readcount. In batch file I imagine it would be possible to call one batch file from other which would process only limited part of the file.
Using PowerShell, you can limit how much data is processed at a time in the pipeline.
Get-Content -Path .\testJana.txt -ReadCount 1000 |
ForEach-Object { $_ -replace '^ +', '' } |
Out-File -FilePath .\testJana_edited.txt -Encoding ASCII
If you want to run this from a cmd.exe shell, put the PowerShell code above into a file named sourcelimit.ps1 and use the following in a .bat script file.
powershell -NoProfile -File .\sourcelimit.ps1

How to determine Windows version from a batch script for an inactive drive?

How can one get the version of Windows from the shell (command prompt) via a batch script for a drive that does not contain the active OS? I was hoping for some file that I could test, but it turns out things are a little more vague than I'd hoped. This should be able to determine the version of Windows for all NT releases from 2000 to 8.1.
you can load the windows registry from the "inactive OS' Drive" and read the version from it:
this is not tested, but it's something like this:
set SYSTEM_DRIVE=D:
reg load "HKU\ttt" "%SYSTEM_DRIVE%\Windows\System32\config\SOFTWARE"
reg query "HKU\ttt\SOFTWARE\Microsoft\Windows NT\CurrentVersion" /v ProductName
reg unload "HKU\ttt"
I would think a fairly robust method would be to look at the file version metadata for a standard OS file such as %SystemRoot%\system32\winver.exe.
I did the following (from command prompt):
'findstr /i "f.i.l.e.v.e.r.s.i.o.n" kernel32.dll > ver'
The dots are required because the metadata is unicode, and findstr will thus ignore the
zeros that are between the letters (dot being a wildcard for "any single character").
(see: findstr /?). The snippet can be examined in notepad or, on some systems that still support "EDIT", "EDIT /70 VER". The output is still unicode, but it can be "prettied up"
using programming (f/e vbscript). the raw material can be examined "as is":
F i l e V e r s i o n 6 . 1 . 7 6 0 1 . 1 9 1 3 5 ( w i n 7 s p 1 _ g d r . 1 6 0 1 2 1 - 1 7 1 8 ) 2. Googling windows versions yielded: me=4.9, 2000=5.0, xp=5.1, vista=6.0, 7=6.1, 8=6.2, 8.1=6.3, 10=10.0, (then a bunch more while microsoft put out fires, then 11=21H2. (7601 is the "build" number") Above taken from: https://en.wikipedia.org/wiki/List_of_Microsoft_Windows_versions

Windows copy command return codes?

I would like to test for the success/failure of a copy in a batch file, but I can't find any documentation on what if any errorlevel codes are returned. For example
copy x y
if %errorlevel%. equ 1. (
echo Copy x y failed due to ...
exit /B
) else (
if %errorlevel% equ 2. (
echo Copy x y failed due to ...
exit /B
)
... etc ...
)
I'd opt for xcopy in this case since the error levels are documented (see xcopy documentation, paraphrased below):
Exit code Description
========= ===========
0 Files were copied without error.
1 No files were found to copy.
2 The user pressed CTRL+C to terminate xcopy.
4 Initialization error occurred. There is not
enough memory or disk space, or you entered
an invalid drive name or invalid syntax on
the command line.
5 Disk write error occurred.
In any case, xcopy is a far more powerful solution. The equivalent documentation for copy does not document the error levels.
As an aside, you may want to rethink your use of the %errorlevel% variable. It has unexpected results, at least in some versions of Windows, if someone has explicitly done something silly like:
set errorlevel=22
In those cases, the actual variable will be used rather than grabbing the actual error level. The "normal" way of doing this is (in decreasing order since errorlevel is a "greater than or equal to" check):
if errorlevel 2 (
echo Copy x y failed due to reason 2
exit /B
)
if errorlevel 1 (
echo Copy x y failed due to reason 1
exit /B
)
In addition, if you are running Win7 or Win Server 2008 or later, you should look into Robocopy, which is now the preferred mass-copy solution.
It might also be worth pointing out that xcopy doesn't always return the error code you expect.
For example when trying to copy multiple files with a wildcard but there are no files to copy you expect a return error code of 1 ("No files were found to copy"), but it actually returns 0 ("Files were copied without error")
C:\Users\wilson>mkdir bla
C:\Users\wilson>mkdir blert
C:\Users\wilson>xcopy bla\* blert\
0 File(s) copied
C:\Users\wilson>echo %ERRORLEVEL%
0
I believe Copy only returns 0 for success or 1 for failure.
XCopy has documented return codes:
0 = Files were copied without error.
1 = No files were found to copy.
2 = The user pressed CTRL+C to terminate xcopy.
4 = Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.
5 = Disk write error occurred.
There is also one point I would like to emphasize: xcopy as well as robocopy can only copy files, but they can't rename them.
While looking at the original situation (copy x y, which looks like a rename to me), I have the impression that the copy command still is the only one suitable for this purpose.
Error# Description
0 No error
1 Not owner
2 No such file or directory
3 Interrupted system call
4 I/O error
5 Bad file number
6 No more processes
7 Not enough core memory
8 Permission denied
9 Bad address
10 File exists
11 No such device
12 Not a directory
13 Is a directory
14 File table overflow
15 Too many open files
16 File too large
17 No space left on device
18 Directory not empty
999 Unmapped error (ABL default)

Resources