I have a series of .txt files that I am trying to get into a database.
The 7th row "contains" the delimitation in dashes and spaces. Tools like excel try and space delimit based on the entire file. but rows after row 7 have spaces within the fields.
From a post I found I have managed to create a cmd.bat script to remove the first 6 rows and footer. Now I need advise on how to "delimit" the file using row7. deleting row7 afterward would be a bonus. 4 of the fields fluctuate in length so fixed field length is not and option. Would appreciate any help.
Report Number: ST-49 ENERGY RESOURCES CONSERVATION BOARD Page 1 of 1
Run Date: 01 Jan 2012 DAILY DRILLING ACTIVITY LIST
For the Notification Period 31 Dec 2011 To 01 Jan 2012
WELL ID WELL NAME LICENCE +----------------CONTRACTOR-----------------+ RIG ACTIVITY DATE FIELD CENTRE BA ID LICENSEE NEW PROJECTED ACTIVITY
BA ID NAME NUMBER TOTAL DEPTH TYPE
------------------- ----------------------------------- -------- ------ -------------------------------------- ------ ----------------------- -------------- ----- ------------------------------------- ------------- -----------
00/04-11-028-28W4/0 ECA CROSSE 4-11-28-28 0438271 0Y52 Precision Drilling Corporation 239 31 Dec 2011 10:30:00 PM Midnapore 0026 EnCana Corporation Drill To LD
00/10-21-038-24W4/0 ECA HAYNES 10-21-38-24 0439238 0Y52 Precision Drilling Corporation 154 31 Dec 2011 11:00:00 PM Red Deer 0026 EnCana Corporation Drill To LD
00/04-08-049-18W5/0 HUSKY MINEHEAD 4-8-49-18 0440874 0Y52 Precision Drilling Corporation 401 01 Jan 2012 01:00:00 PM Drayton Valley 0R46 Husky Oil Operations Limited Drill To LD
TOTAL - Drill To LD - Drilling to Licensed Depth = 15
TOTAL - Re-entry - Re-entry of an Abandoned Well = 0
TOTAL - Resumption - Resumption of Drilling of a Non-abandoned Well = 0
TOTAL - Set Surface - Drilling to Set Surface Casing Only = 2
TOTAL NUMBER OF WELLS LISTED = 17
*** END OF REPORT ***
try this (it might be slow, there is not to change):
#ECHO OFF &SETLOCAL ENABLEDELAYEDEXPANSION
SET "infile=file"
SET "outfile=out"
FOR /f %%a in ('^<"%infile%" find /c /v ""') do SET /a lline=%%a-9
FOR /f "usebackqskip=6delims=" %%a IN ("%infile%") DO IF NOT DEFINED line SET "line=%%a"
FOR %%a IN (%line%) DO (
SET /a counter+=1
FOR /f "delims=:" %%b IN ('(echo(%%~a^&echo(^)^|findstr /o $') DO SET /a $!counter!=%%b-2
)
(FOR /f "skip=6delims=" %%a IN ('findstr /n $ "%infile%"') DO IF %lline% gtr !cline! (
SET "line=%%a"
FOR /f "delims=:" %%b IN ("!line!") DO SET /a cline=%%b
SET "line=!line:*:=!"
SET /a cstart=0
SET "Newline="
FOR /l %%b IN (1,1,%counter%) DO (
CALL SET "token=%%line:~!cstart!,!$%%b!%%"
SET "flag=true"
SET /a tstop=!$%%b!-1
FOR /l %%c IN (!tstop!,-1,0) DO IF DEFINED flag IF NOT "!token:~%%c,1!"==" " (
SET /a tstop=%%c+1
FOR /f %%d IN ("!tstop!") DO SET "token=!token:~0,%%d!"
FOR /f "tokens=*" %%d IN ("!token!") DO SET "token=%%d"
SET "flag="
) ELSE (
IF %%c equ 0 SET "token="
)
IF %%b equ 1 (SET "Newline="!token!"") ELSE SET "Newline=!Newline!,"!token!""
SET /a cstart+=!$%%b!
)
ECHO(!Newline!
))>"%outfile%"
TYPE "%outfile%"
..output:
"00/04-11-028-28W4/0","ECA CROSSE 4-11-28-28","0438271","0Y52","Precision Drilling Corporation","239","31 Dec 2011 10:30:00 PM","Midnapore","0026","EnCana Corporation","","Drill To LD"
"00/10-21-038-24W4/0","ECA HAYNES 10-21-38-24","0439238","0Y52","Precision Drilling Corporation","154","31 Dec 2011 11:00:00 PM","Red Deer","0026","EnCana Corporation","","Drill To LD"
"00/04-08-049-18W5/0","HUSKY MINEHEAD 4-8-49-18","0440874","0Y52","Precision Drilling Corporation","401","01 Jan 2012 01:00:00 PM","Drayton Valley","0R46","Husky Oil Operations Limited","","Drill To LD"
This doesn't work with special characters like ^"!%*=~ in the infile.
You should better use awk for Windows and set FIELDWIDTHS in the BEGIN pattern. You might combine it with sed for Windows.
Try using qualifies, i.e. " or anything that can make your application or tool to identify that column start from here. and it will ignore the delimiter with in column.
Related
This is much easier than what I'm making it...
I have a non-delimited input file that I want to grab three columns from and output them to a new layout. The input file is fixed length. I want to take the data from the following positions:
field1 - position 1 for 7 characters
field2 - position 13 for 50 characters
field3 - position 187 for 10 characters
I want my output file to be field1,field2,field3...comma delimited.
This is what I thought would work, but alas, it doesn't:
#echo off
setlocal EnableDelayedExpansion
for /F "tokens=*" %%A in (C:\mydata.txt) do (
set line=%%A
set var_id=!line:~0,7%!
set var_name=!line:~12,50%!
set var_amt=!line:~186,10%!
set ABC=!%var_id%%var_name%%var_amt%!
echo ABC
)
So, I'm making a game using batch coding, and I have a shop within the game that's only supposed to sell one of each item. Unfortunately, when I set the variable for the item to 1, then reduce it to 0, the code doesn't acknowledge that there are no more items to sell.
:ForestHagShop
cls
set WSwrdHag=1
set WShldHag=1
set GldApHag=1
echo "What would you like, my dear?"
echo 1.) Wooden Sword - 50 gold - (+10 Attack)
echo 2.) Wooden Shield - 70 gold - (+30 Max HP)
echo 3.) Shiny Apple - 40 gold - (+30 HP)
echo.
set /p input6=)
if %input6% equ 1 goto BuySwrdHagChk
if %input6% equ 2 goto BuyShldHag
if %input6% equ 3 goto BuyAplHag
:BuySwrdHagChk
echo Checking...
echo.
echo **PRESS A KEY**
Pause >nul
if WSwrdHag LSS 1 goto OutSwordHag
:BuySwrdHag
set /a Gold=Gold-50
set /a Attack=Attack+10
set /a WSwrdHag=WSwrdHag-1
echo You hand over 50 gold for the
echo wooden sword. Your new stats are...
echo Health Points .. .. .. %HP%/%MaxHP%
echo Gold .. .. .. .. .. .. %Gold%
echo Attack Power .. .. .. %Attack%
echo.
echo **PRESS A KEY**
pause >nul
goto ForestHagShop
:OutSwordHag
echo The hag frowns, looking at you like
echo you're stupid.
echo "You already bought my wood sword,
echo idiot!"
echo.
echo **PRESS A KEY**
pause >nul
goto ForestHagShop
Looks like you forgot the % delimiters when checking the variable...
Change
if WSwrdHag LSS 1 goto OutSwordHag
to be
if %WSwrdHag% LSS 1 goto OutSwordHag
and also these
set /a Gold=Gold-50
set /a Attack=Attack+10
set /a WSwrdHag=WSwrdHag-1
to be
set /a Gold=%Gold%-50
set /a Attack=%Attack%+10
set /a WSwrdHag=%WSwrdHag%-1
I am making a Batch RPG and currently I'm trying to create a shop where you can
buy weapons and stuff. I want to make it so that if the player has enough gold (gld) that they can buy the weapon and it changes the weapon's boolean to true and takes away 20 gold from the gld variable. What am I doing wrong? It doesn't subtract the gold or change to boolean to true.
Here is the code:
set /p sword="Enter your selected sword number:"
if %sword%==1 if %gld% GEQ 20 set /a sword1 EQU true
if %sword%==1 if %gld% GEQ 20 set /a gld EQU %gld%-20
if %sword%==1 if %gld% LSS 20 echo You don't have enough gold!
The problem is that you're not using the set command correctly.
set /p sword="Enter your selected sword number:"
if %sword%==1 if %gld% GEQ 20 set sword1=true
if %sword%==1 if %gld% GEQ 20 set /a gld-=20
if %sword%==1 if %gld% LSS 20 echo You don't have enough gold!
You can also make one if else statement to do the same thing
set /p sword="Enter your selected sword number:"
if %sword%==1 if %gld% GEQ 20 (
set sword1=true
set /a gld-=20
) else (
echo You don't have enough gold!
)
sorry for the question I am a total beginner in programming and probably this is very easy for someone who knows what to do:
I need to create, into a txt file, a serie of links with incremental numbers, for example I want to keep a certain part "fixed" and a part has to change; for example imagine this:
http://user:password#website.com/members/sets/001/somethingfixed-001.zip
in the text the following lines should be:
http://user:password#website.com/members/sets/002/somethingfixed-002.zip
etc..
I think there must be a way to generate those where I could specify, create the link keep the fixed part equal in every line and then incrementally put 001, 002, 003, up to the point i need it.
it could be awesome if I could create a .bat file that would ask me: what is the initial link, what is the part to be fixed and what to change.
I'd do the program like this:
insert the first link, and I paste it.
then it asks me for what is the fixed part and I put:
.../members/sets/X/somethingfixed-Y.zip
what is the range for the variables X and Y?
starting number? and I put 001 and then it asks me what is the end number? and i put 150
and it generates the links in the txt file so that I can use them.
Hope it is clear.
Cheers
Example
This will do it for the first number X, I will leave some of it for you to develop and learn. :)
#echo off
setlocal EnableExtensions EnableDelayedExpansion
echo LeftXRight Gen
set /p "Left=Left = "
set /p "Right=Right = "
set /p "Start=Begin = "
set /p "End=End = "
>output.txt ( <nul set /p "=" )
for /L %%A in (%Start%, 1, %End%) do (
set "X=00%%A"
echo !Left!!X:~-3!!Right!>>output.txt
)
endlocal
Output
LeftXRight Gen
Left = http://user:password#website.com/members/sets/
Right = /somethingfixed.zip
Start = 1
End = 10
output.txt
http://user:password#website.com/members/sets/001/somethingfixed.zip
http://user:password#website.com/members/sets/002/somethingfixed.zip
http://user:password#website.com/members/sets/003/somethingfixed.zip
http://user:password#website.com/members/sets/004/somethingfixed.zip
http://user:password#website.com/members/sets/005/somethingfixed.zip
http://user:password#website.com/members/sets/006/somethingfixed.zip
http://user:password#website.com/members/sets/007/somethingfixed.zip
http://user:password#website.com/members/sets/008/somethingfixed.zip
http://user:password#website.com/members/sets/009/somethingfixed.zip
http://user:password#website.com/members/sets/010/somethingfixed.zip
Update
Added this complete solution to the question.
#echo off
setlocal EnableExtensions EnableDelayedExpansion
echo LeftXMidXRight Gen
set /p "Left=Left = "
set /p "Middle=Middle = "
set /p "Right=Right = "
set /p "Start=Begin = "
set /p "End=End = "
>output.txt ( <nul set /p "=" )
for /L %%A in (%Start%, 1, %End%) do (
set "X=00%%A"
echo !Left!!X:~-3!!Middle!!X:~-3!!Right!>>output.txt
)
endlocal
Example
LeftXMidXRight Gen
Left = http://user:password#website.com/members/sets/
Middle = /somethingfixed-
Right = .zip
Begin = 1
End = 150
output.txt
http://user:password#website.com/members/sets/001/somethingfixed-001.zip
http://user:password#website.com/members/sets/002/somethingfixed-002.zip
...
http://user:password#website.com/members/sets/150/somethingfixed-150.zip
This gets near what you are after, with a couple of limitations..
It can only append to the end of the url.
1 and 2 digit numbers are not zero prefixed.
#echo off
set /p url="Enter URL: "
set /p start="Start Number: "
set /p end="End Number: "
for /L %%X in (%start%, 1, %end% ) do echo %url% %%X >> file.txt
I want to modify this script. I can't get it to accept wildcard charcters:
#echo off
setLocal EnableDelayedExpansion
set /a value=0
set /a sum=0
FOR /R %1 %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
#echo Size is: !sum! k
It is in a batch file called dirsize and is called like so:
dirsize c:\folder
I want it to check the folder sizes for me. This one here is an example, the cache in firefox:
dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\*.default\Cache
It returns the value 0.
But if I go
dirsize C:\users\%username%\AppData\Local\Mozilla\Firefox\sr1znnb4.default\Cache
It works and I get the value 55322 returned.
PowerShell makes this easy, of course:
(gci . -Recurse | Measure-Object -Property Length -Sum).Sum
And PowerShell is already installed on Windows 7. Get on the bandwagon! :-)
Also, C:\users\%USERNAME%\AppData\ isn't a reliable way of finding AppData. Try %APPDATA% (or $env:APPDATA in PowerShell).
Ok for a starter, lets say wildcards are not well supported throughout batch files, and specially in for loops.
That's what I came up with:
DirSize.cmd "valid path"
#echo off
rem Args[1]= "Path" or "valid path with wildcard"
set _size=0
set dRaw=%~1
echo %~1|findstr "*"&& set dSource=%~dp1|| (
set dSource=%~1\
if not "%dRaw:~-1%"=="\" set dRaw=%dRaw%\
)
for /f "delims=" %%D in ('dir /ad /b %dRaw%') do call :DirSize "%dSource%%%~D"
set /a size=_size/1024
echo size=%size% k (%_size% bytes)
exit /b 0
:DirSize
call :FileSize "%~1"
for /f "delims=" %%d in ('dir /ad /b "%~1"2^>nul') do call :DirSize "%~1\%%~d"
exit /b 0
:FileSize
for /f "delims=" %%f in ('dir /a-d /b "%~1\"2^>nul') do for %%F in (%1\%%f) do set /a _size+=%%~zF
exit /b 0
___Usage___
Argument: Needs to be a path (not a file) that either contain wildcards or not. Path need top be valid when typing dir path
___Notes___
This script does check for all files and sub-directories, even hidden/system ones.
change the dir /a argument and add -h-s (like /ad-h-s) to remove this behaviour.
I revised the argument checking to my liking now, so I think it is usable.
I hope this helps.
You would need to wrap what you currently have in another for loop which expands the * into a series of directories.
So
#echo off
setLocal EnableDelayedExpansion
set /a sum=0
for /d %%D in (%1) do (
FOR /R %%D %%I IN (*) DO (
set /a value=%%~zI/1024
set /a sum=!sum!+!value!
)
)
echo Size is: !sum! k
might work (untested, though, but should give you the general idea)
If you have a Windows version of grep (you can find one at GNU utilities for Win32) you can do something like this:
dir /s %1 | grep --after-context=1 "Total Files Listed:"
This code will increase directory size limit up to 2,000,000 TB and will display more real size, what would be useful if the folder contains too many files.
rem SET DIR=%1
SET DIR=C:\DIR1\
IF NOT EXIST "%DIR%" (
ECHO Directory "%DIR%" does not exist.
goto END
)
setlocal EnableDelayedExpansion
set GB=
set SIZE=
set exp=000000000
FOR /R "%DIR%" %%F IN (*) DO (
set /a SIZE=!SIZE!+%%~zF
if !SIZE! geq 1!exp! (
set /a GB=!GB!+!SIZE:~0,-9!
for /f "tokens=* delims=0" %%a in ("!SIZE:~-9!") do set SIZE=%%a
)
)
if not defined SIZE set SIZE=0
if not defined GB goto EOS
FOR /L %%N IN (1,1,9) DO (
if "!SIZE:~%%N!" == "" (
set SIZE=!GB!!exp:~%%N!!SIZE!
goto EOS
)
)
:EOS
call :Divide !SIZE! 1024 SIZEKB
echo Size is: !SIZE!B (!SIZEKB!kB)
endlocal
:END
pause
exit
:Divide
set Num1=%~1
set Num2=%~2
set %3=
set Num=
set Number=
IF !Num2! EQU 0 goto :EOF
FOR /L %%N IN (0,1,18) DO (
if "!Num1:~%%N!" == "" goto :EOD
set Number=!Number!!Num1:~%%N,1!
if !Number! geq !Num2! (
set /a quotient=!Number!/!Num2!
set /a Number=!Number!-!quotient!*!Num2!
if !Number! equ 0 set Number=
set Num=!Num!!quotient!
) else (
if defined Num set Num=!Num!0
)
)
:EOD
if not defined Num set Num=0
set %3=%Num%
goto :EOF
10x faster than doing it through FOR.
With the advantage of being able to use any of the permitted parameters of the DIR command (in this case /S)
#ECHO OFF
SET folder="C:\WINDOWS"
call:timer
ECHO Processing, please wait...
CALL :folderSize size %folder% "/S"
CALL :strNumDivide sizeKb %size% 1024
CALL :strNumDivide sizeMb %sizeKb% 1024
CALL :strNumDivide sizeGb %sizeMb% 1024
CALL :formatNumber size %size%
CALL :formatNumber sizeKb %sizeKb%
CALL :formatNumber sizeMb %sizeMb%
CALL :formatNumber sizeGb %sizeGb%
ECHO.
ECHO Size of %folder%:
ECHO.
ECHO %size% bytes
ECHO %sizeKb% Kb
ECHO %sizeMb% Mb
ECHO %sizeGb% Gb
ECHO.
pause
exit /b
:: Function to calculate the size of a directory and its subdirectories
::----------------------------------------------------------------------
:folderSize <returnVariableName> <folder> [DIR parameters]
SetLocal EnableExtensions EnableDelayedExpansion
SET folder=%2
SET params=%~3
IF NOT DEFINED folder SET folder="%CD%"
DIR %params% /W "%folder:"=%" > %TEMP%\folderSize.tmp
FOR /F "tokens=1 delims=:" %%x IN ('findstr /n /e ":" %TEMP%\folderSize.tmp') DO (SET line=%%x)
IF DEFINED line (
SET /A line = !line! + 1
FOR /F "tokens=4 delims= " %%i IN ('findstr /n "bytes" %TEMP%\folderSize.tmp^|findstr "!line!:"') DO (SET size=%%i)
SET size=!size:.=!
) ELSE (
FOR /F "tokens=3 delims= " %%i IN ('findstr /e "bytes" %TEMP%\folderSize.tmp') DO (SET size=%%i)
SET size=!size:.=!
)
DEL %TEMP%\folderSize.tmp > nul
EndLocal & SET "%~1=%size%"
GOTO:EOF
:: Extras functions to convert between different units and to give numerical format
:: --------------------------------------------------------------------------------
:strNumDivide <returnVariableName> <stringNum> <divisor>
SetLocal EnableExtensions EnableDelayedExpansion
SET strNum=%~2
SET divisor=%~3
SET result=
SET number=
IF !divisor! EQU 0 GOTO:EOF
FOR /L %%n IN (0,1,18) DO (
IF NOT "!strNum:~%%n!" == "" (
SET number=!number!!strNum:~%%n,1!
IF !number! GEQ !divisor! (
SET /A quotient=!number! / !divisor!
SET /A number=!number! - !quotient! * !divisor!
IF !number! EQU 0 SET number=
SET result=!result!!quotient!
) ELSE (
IF DEFINED result SET result=!result!0
)
)
)
IF NOT DEFINED result SET "result=0"
EndLocal & SET "%~1=%result%"
GOTO:EOF
:formatNumber <returnVariableName> <number> [separator [group size]]
SetLocal EnableExtensions EnableDelayedExpansion
SET "raw=%~2"
SET "separator=%~3"
SET "group=%~4"
SET "answer="
IF NOT DEFINED raw GOTO :EOF
IF NOT DEFINED separator SET "separator=."
IF NOT DEFINED group SET "group=3"
FOR %%g IN (-%group%) DO (
FOR /F "tokens=1,2 delims=,." %%a IN ("%raw%") DO (
SET int=%%a
SET frac=%%b
FOR /F "delims=:" %%c IN ('^(ECHO;!int!^& Echo.NEXT LINE^)^|FindStr /O "NEXT LINE"') DO (
SET /A length=%%c-3
)
FOR %%c IN (!length!) DO (
SET radix=!raw:~%%c,1!
)
FOR /L %%i IN (!length!, %%g, 1) DO (
SET answer=!int:~%%g!!separator!!answer!
SET int=!int:~0,%%g!
)
)
)
SET answer=%answer: =%
SET answer=%answer:~0,-1%
EndLocal & SET "%~1=%answer%%radix%%frac%"
Goto :EOF
Result output example:
(in the example, the routine took 3 seconds to calculate the size of the C: \ WINDOWS with a size of 28GB)
Processing, please wait...
Size of "C:\WINDOWS":
30.936.389.769 bytes
30.211.318 Kb
29.503 Mb
28 Gb
Elapsed Time: 3 secs