I am making a little program for a coding club and some friends and it is like an offline database. I need to put in a dir path (ex. C:\Users\My.Name\Desktop\txtfile.txt) and I need to know how to set these variables -
Set Input = C:\Users\My.Name\Desktop\txtfile.txt
REM (SPLIT THE LINE HERE)
REM Output -
Set Path = C:\Users\My.Name\Desktop
Set Identifier = txtfile.txt
How would I do this, I have tried seperating at every "/" but I don't know that there will always be a certain amount of slashes.
Like this:
#echo off
setlocal
Set "Input=C:\Users\My.Name\Desktop\txtfile.txt"
for /f %%a in ("%Input%") do (
echo %%~dpa
echo %%~nxa
)
For has nice builtin parameters for splitting paths. Read FOR /? at the very end for details. Don't use PATH as a variable. It's a builtin environment variable. I usually use sPath or something similar.
Related
I need to delete the substring after *. with a batch file
Example :
The value of string is : TEST_SINISTRE*.csv
rem My code :
SET mystring="TEST_SINISTRE*.csv"
rem Do the split
SET ext_test=%mystring:*.="& rem %"
SET ext_test=%ext_test%
rem what i get
echo %ext_test% ===> "& rem csv"
rem What i want to see
===> TEST_SINISTRE
Can you help me :-)
If, and only if, the *. pattern can only occur once in the string, and the part after *. is not contained in the part before *., the following could be used:
rem this is the original string containing one `*.`:
set "STRING=TEST_SINISTRE*.csv"
rem now get everything after `*.`:
rem (if `*` is the first character in substring substitution, it means everything up to
rem and including the search string is to be replaced, by nothing here in this case)
set "SUBSTR=%STRING:**.=%"
rem get everything before `*.`, including the `*`:
setlocal EnableDelayedExpansion
set "SUBSTL=!STRING:.%SUBSTR%=!"
rem truncate the `*` from the string:
endlocal & set "SUBSTL=%SUBSTL:~,-1%"
Since the substitution syntax for variable expansion is used, this is done in a case-insensitive manner.
To make it more secure, you could append something that will most probably never occur to your original string temporarily and remove it afterwards. To accomplish this, replace the set command line between the setlocal/endlocal block with the following (using appendix ### here for instance):
set "STRING=!STRING!###"
set "SUBSTL=!STRING:.%SUBSTR%###=!"
The * character is a wildcard in batch variable substring substitution. When you do *.=something in an inline substitution, you're really saying "replace everything up to and including the dot". You should use a for /F loop so you can specify the asterisk as a delimiter.
set "str=TEST_SINISTRE*.csv"
for /f "tokens=1* delims=*" %%I in ("%str%") do set "ext_test=%%I%%J"
echo %ext_test%
I'm not sure what your ultimate goal is, but here's a hacksy possible alternative. You could actually create a file called TEST_SINISTRE.csv and then capture the filename into a variable as a wildcard match.
set "str=TEST_SINISTRE*.csv"
type NUL > TEST_SINSTRE.CSV
for %%I in (%str%) do set "ext_test=%%I"
echo %ext_test%
I'm sure that's not exactly you have in mind, but it does demonstrate that maybe you don't need to strip the asterisk if you are going to be performing filename matching.
I was given this code by Aacini (thanks!) but I don't know how to set which text file to search for the data.
#echo off
setlocal EnableDelayedExpansion
set "line=Username:Desired Information</br><br>"
set "string1=Username:"
set "string2=</br><br>"
rem Remove from beginning until string1
set "line=!line:*%string1%=!"
rem Change the string2 by a one character delimiter
set "line=!line:%string2%=|!"
rem Get the desired information
for /F "delims=|" %%a in ("%line%") do set "result=%%a"
echo Result: "%result%"
How would I do this? I'm sure it's just a set textfile=inbox.txt and another line of code to make it use the %TEXTFILE% variable, but I just don't know how.
You don't get the rigth answer because you don't post the right question. Neither in your original question nor in this one you asked for something like "How to read a file and get the text between two strings". Also, you should specify the details of the file and post a section that contain the desired information; otherwise a Batch solution may fail because a large number of factors.
Anyway, I created this simple data file just for testing:
This is a sample file
with unknown format
Previous info. Username:Desired Information</br><br>
End of file
And this is the solution:
#echo off
setlocal EnableDelayedExpansion
set "string1=Username:"
set "string2=</br><br>"
for /F "delims=" %%a in ('findstr "%string1%" file.txt') do (
rem Get the next line
set "line=%%a"
rem Remove from beginning until string1
set "line=!line:*%string1%=!"
rem Change the string2 by a one character delimiter
set "line=!line:%string2%=|!"
rem Get the desired information
for /F "delims=|" %%a in ("!line!") do set "result=%%a"
echo Result: "!result!"
)
Output:
Result: "Desired Information"
However, this solution is prone to fail because multiple reasons, for example:
May the file contain exclamation marks?
May the text that contain the desired information be split in several lines?
May that text contain the "|" choosen separator?
Do you want all instances of the desired information? Or just the first one? Or just the last one?
Etc...
That's because it's parsing specified variables. To read a line from a file
for /f "usebackq tokens=1 delims=" %%A in ("c:\somefolder\somefile.txt") do (
... put your other for loop here
)
See for /? for help. See set /? and call /? (and also for /? again) to see syntax details.
WHERE IS MY FORMATTING AND PREVIEW.
I need to extract information from a path found on a line in a batch file. Specifically the line says:
GLOBP4P=C:\folderx\foldery\folderz\datag.p4p
What I need to extract is the part above identified as "data". The g.p4p is always present, the GLOBP4P is always present, but the things in between can be varying from file to file. I essentially want to strip the g.p4p and then write whatever is left to the first "\" to a variable. in this case the variable would store "data". The problem I have is that "data" can be any number of characters, also there can be any number of path folders, etc.
I attempted to do aFor /f with "\" as a delim to try and break the string apart, but since i don't know how many folders there will be I don't know how to identify taking the last one (i.e. the filename) exclusively.
Thanks
This should work too:
#echo off
for /f "delims=" %%a in ('findstr /i "GLOBP4P=" "file.ini"') do set "var=%%~na"
set "var=%var:~0,-1%"
echo "%var%"
Try this. You will get the LAST occurence finded in your ini file :
#echo off
::The name of the bat file to test
Set $bat="The_ini_file_to_test.ini"
::Finding the string "GLOBP4P" and setting the value in it
for /f %%a in ('findstr /i "GLOBP4P" "%$bat%"') do set %%a
::Getting the first and the Last element of the path
set first=%GLOBP4P:\=&set last=%
::Outputting some results
echo File : [%last%]
echo Data : [%last:~0,-5%]
echo drive : [%first%]
#echo off
set GLOBP4P=C:\folderx\foldery\folderz\datag.p4p
for %%a in ("%GLOBP4P%") do set var=%%~Na
set var=%var:~0,-1%
echo %var%
EDIT: Some explanations added
In a FOR command, %%~N return just the file name of the FOR parameter, in this case is datag string. Enter FOR /? for further details.
When a variable is expanded via %var%, the %var:~START,LEN% notation indicate to extract a substring starting at START character (beginning at zero) and with LEN characters. If LEN have minus sign, indicate the last character backwards from string end. In this case, %var:~0,-1% indicate to extract all but last characters from var, that is, eliminate the last character from datag string. Enter SET /? for further details.
Complete novice here so all help gratefully recieved. I am hoping to edit a text file with a batch file.
I have a text file that is all one line.
I need the first 240 characters deleted
I need the last 82 characters deleted
Finally, anything in between I need each 100 characters to be separated with a line break
Thanks
Mark
try this (pure batch, no VBS):
#ECHO OFF &SETLOCAL
SET "longstring=012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901"
ECHO %longstring%
ECHO(
REM I need the first 240 characters deleted
SET "longstring=%longstring:~240%"
ECHO %longstring%
ECHO(
REM I need the last 82 characters deleted
SET "longstring=%longstring:~0,-82%"
ECHO %longstring%
ECHO(
REM I need each 100 characters to be separated with a line break
SET LB=^
SET "right=%longstring%"
SET "longstring="
SETLOCAL ENABLEDELAYEDEXPANSION
:loop
SET "left=%right:~0,100%"
SET "right=%right:~101%"
SET "longstring=!longstring!!left!!LB!"
IF DEFINED right GOTO :loop
ECHO(!longstring!
..output is:
012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901
X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X0123456789012345678901234567890123456789012345678901234567890123456789012345678901
X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X
X012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678
0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789X
Your problem is easily solved with nearly any programming language... except Windows batch :(
It is nearly impossible with Windows batch if your file is longer than 8192 bytes because batch variables are limited to 8192 characters. There is a complicated method that uses FC /B to effectively read one byte at a time, expressed as hex. It would be relatively easy to do the counting. But then each hex code would need to be converted back into the character value. You do not want to go through all that pain, except for maybe academic interest.
You could use PowerShell, VBScript, or JScript to easily do what you want.
But if you want to stick with batch, then you will need a non-standard utility to help you.
There are free utilities you could download that would make the solution trivial. For example, sed for Windows could make short work of this problem.
I have written a hybrid JScript/batch utility called REPL.BAT that performs regular expression search and replace on stdin, and writes the result to stdout. The utility is pure script that works on any Windows machine from XP onward - no exe download required. REPL.BAT is available here. Complete documentation is embedded within the script.
Assuming REPL.BAT is either in your current directory, or better yet, somewhere within your PATH, then the following simple batch script should do the trick. The script takes the name of the file to modify as the first and only argument. The file spec can include path information. Be sure to enclose the file in quotes if it contains spaces or other special characters.
#echo off
type %1|repl "^.{240}(.*).{82}$" "$1"|repl ".{100}" "$&\r\n" x >"%~1.mod"
move /y "%~1.mod" %1 >nul
I'd strongly recommend switching to PowerShell or at least VBScript if at all possible. It'd be a lot easier to do what you want with those languages.
PowerShell:
$filename = 'C:\path\to\your.txt'
(Get-Content $filename | Out-String) `
-replace '^[\s\S]{240}', '' `
-replace '[\s\S]{82}$','' `
-replace '([\s\S]{100})',"`$1`r`n" `
| Set-Content $filename
VBScript:
filename = "C:\path\to\your.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set re = New RegExp
re.Global = True
txt = fso.OpenTextFile(filename).ReadAll
re.Pattern = "^[\s\S]{240}"
txt = re.Replace(txt, "")
re.Pattern = "[\s\S]{82}$"
txt = re.Replace(txt, "")
re.Pattern = "([\s\S]{100})"
txt = re.Replace(txt, "$1" & vbNewLine)
fso.OpenTextFile(filename, 2).Write txt
I've been trying to concatenate strings with the lines of a text file, but something is wrong with my code and I belive is the agruments I am using in the the For cycle. If any one can help me I'll much appreciate it.
My code is:
#echo off
set "input=C:\Users\123\Desktop\List.txt"
for /f "usebackq tokens=*" %%F in ("%input%") do (
set "str1=C:\some directory\"
set "str2=%%~F"
set "str3=.pdf"
set "str4=%str1%%str2%%str3%"
echo.%str4%
)
and the text file is something like:
121122
122233
123344
124455
But I am only getting a wrong answer and I have to run it like 3 times to get a real result and it's wrong, the first two are blank spaces and the third one gives back the last line in the text file but repeated n times where, n is the number of lines in the text file.
Result:
C:\Users\123\Desktop>concatenate.bat
C:\Users\123\Desktop>concatenate.bat
C:\Users\123\Desktop>concatenate.bat
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\some directory\124455.pdf
C:\Users\123\Desktop>
So, if any one has a clue on what is wrong please let me know.
Regards
-Victor-
You'll need the Enable Delayed Expansion feature. It's required since within a FOR command block, you need to refer variables that was modified.
#echo off
setlocal enabledelayedexpansion
set "input=C:\Users\123\Desktop\List.txt"
for /f "usebackq tokens=*" %%F in ("%input%") do (
set "str1=C:\some directory\"
set "str2=%%~F"
set "str3=.pdf"
set "str4=!str1!!str2!!str3!!"
echo. !str4!
)