Batch file name string concatenation - string

this problem has been bothering me for hours. I hope someone could help shed some light. Basically, I am trying to scan through all the files (of a specific type) in a folder (there are no subfolder inside, so no need to worry this case), get those files' name, and concatenate all into one string for example, if a folder has two files,
a.xml, b.xml and c.xml, I want to get a string looks like
-a a.xml -a b.xml -a c.xml
Below is my code.
copy *.xml C:\FTP
setLocal Enabledelayedexpansion
set "directory=C:\temp"
set "attachment= "
set "a= -a "
for %%n in (%directory% *.xml) DO (
set "attachment=!attachment! %a% %directory%\%%n "
echo.%attachment%
)
setlocal disabledelayedexpansion
echo.%attachment%
The output is shown in this image http://imgur.com/vpUUe05
the problem I have is that first,
all echoes in the for loop prints out nothing. as shown in those blank lines.
the final string I want,i.e. attachment, contains an initial substring of
-a C:\temp\C:temp.
This is actually not a file. the final string I want should be one that is without this substring, only those behind it. By the way, if there is no file inside the folder, I want the string "attachment" to just an empty string like "". Can anyone help me? Thanks a lot!

Inside your for loop you show the value of %attachment%, so that value is NOT updated in each iteration; you must use delayed expansion to do so.
The set of values in for command is: (%directory% *.xml) that is, the value C:\temp and all files with .xml extension, that I assumed is none in current directory. After that, you use this value in %a% %directory%\%%n expression, so the result is -a C:\temp\C:\temp. I think there is not a point here.
If you want not the folder value in the list, just don't insert it and use %~NXn modifier in the for replaceable parameter.
Below is the correct code:
copy *.xml C:\FTP
setLocal Enabledelayedexpansion
set "directory=C:\temp"
set "attachment= "
set "a= -a "
for %%n in ("%directory%\*.xml") DO (
set "attachment=!attachment! %a% %%~NXn "
echo.!attachment!
)
setlocal disabledelayedexpansion
echo.%attachment%

Related

Extract after delimter in a text file with Batch

I have been searching for hours, however most of the results give examples that deal with directories. I need to read a text file to achieve this and extract after the last \ and output into a new file
Below is what my file contains in a text file
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\yahoo.com
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\NYU.edu
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Internet Settings\ZoneMap\Domains\openssl.org
I need to extract all the different strings after the last \ in the string. How do I go about doing this?
I tried this one but it is not working properly Extracting string after last instance of delimiter in a Batch file
This is on Windows 7 x64 SP1 and I can't install other software to achieve this either.
This should work:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET inFile=C:\SomePath\someFile.txt
SET outFile=C:\SomePath\anotherFile.txt
TYPE NUL>%outFile%
FOR /F "tokens=*" %%L IN (%inFile%) DO (
ECHO %%~nxL>>%outFile%
)
You'll have to replace inFile and outFile with proper paths. If they contain spaces, surround them with quotation marks.
%outFile% will always be overwritten so make sure you don't loose your data!

Split string by special characters '*' in batch file

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.

Extract a file name out of a path found in an ini file

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.

Batch String Manipulation

I am trying to write a batch program that writes more code based on what the user inputs. I am stuck on the string manipulation, as "!", ">", "&", and "%" all need to be escapeded before they are to be outputted to other files. This is what I have so far:
#echo off
set /p code=
set newcode=%code:^%=^%%,^>=^^>,!=^^!,^&=^^&%
echo %newcode%>>file.bat
All this escapinging escaped stuff is making my brain hurt, so can you please help me?
Thanks!
Since you haven't explained clearly what you are trying to do with the input, I am assuming you are trying to get the user to type something and then for that to be entered into a file. If that is the case te utilise copy in combination with con
#echo off
Echo Enter ^^Z (Ctrl + Z) and return it to end input
copy con file.txt
And that will allow the user to type WHATEVER they want, and it to be put in a file to be examined. Otherwise you're gonna have a lot of fun (And trouble) dealing with all the escaping you're gonna have to do (Rather ironic wasn't that?).
Mona.
Simply use delayed expansion in your case, as delayed expansion needs no further escaping for the content.
#echo off
setlocal EnableDelayedExpansion
set /p code=
echo(!code!
The echo( is for safe text output, even if the text is empty or is something like /?.
To the comment of #foxidrive:
When delayed expansion is enabled, then exclamation marks ! are altered, when they are visible before the delayed expansion is done, but the expanded content of delayed expanded variables will never be altered.
#echo off
set "var=Hello!"
setlocal EnableDelayedExpansion
echo "!var!" "Hello!"
This will result to: "Hello!" "Hello"

how to concatenate strings with the lines of a text file

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!
)

Resources