I try to write a batch file that parses a file to create another one with its content.
But in the new one I will ignore every line which begin with the -Dremote pattern.
Here is my file
-nl
fr_yy
-clean
-vmargs
-Dgma.environnement=staging
-Duser.timezone=CET
-Dgma.communication=remote
-Dgma.security.disable=false
-Xms256M
-Xmx768M
-XX:PermSize=64m
-XX:MaxPermSize=128m
-Dlogback.configurationFile=file:eclipse/conf-log/logback-error.xml
-Dshut.monitor.active=true
-Dshut.monitor.home=\\mynas05\GMData\Projets\RefonteUtilisateurs\Contrats\shut
-Dshut.applicationRuntimeEnv.systemPropertyKey=gma.environnement-nl
-Dremote_server_MessageWar=http://xxx.xxx.yy
-Dremote_port_MessageWar=40110
-Dremote_server_SinistreWar=http://xxx.xxx.yy
-Dremote_port_SinistreWar=40110
-Dremote_server_FacturationTiersWar=http://xxx.xxx.yy
And here is the code
for /f "tokens=*" %%A in (%tempFile%) do (
set test=%%A
IF NOT "!test:~0,7!"=="-Dremote"( type %%i>>%iniFile%)
)
But everything is copied in the iniFile...
findstr /v /b /l /c:"-Dremote" "%tempFile%" >"%iniFile%"
(Test with a dummy %iniFile% first, of course...)
There are a few errors in one line. Try this:
IF NOT "!test:~0,8!"=="-Dremote" (echo %%A>>%iniFile%)
You miscounted (-Dremote has 8 characters, not 7).
Use echo instead of type.
There is a space in the wrong position.
The wrong variable is used.
(I hope you are aware that you'll have to use delayed expansion (it's not in your code).)
Change 7 to 8 "-Dremote" has 8 characters.
Add a SPACE after "-Dremote" before the round bracket.
setlocal enabledelayedexpansion
for /f "tokens=*" %%A in (%tempFile%) do (
set test=%%A
IF NOT "!test:~0,8!"=="-Dremote" ( echo !test!>>%iniFile%)
)
A quick edit should fix it:
for /f "tokens=*" %%A in (%tempFile%) do (
set test=%%A
IF NOT "!test:~0,7!"=="-Dremote"( echo !test!>>%iniFile%)
)
Related
I have 324 files on a Windows 10 machine which are named in the following pattern:
[7 Numbers] [Space] [Last name] [Space] [First name]
And I need them to be:
[Last name] [Space] [First name] [Space] [7 Numbers]
I have done some quick research and found that I could write a batch script utilizing the 'rename' function:
#echo off
rename “y:\*.txt” “???-Test1.*”
However, I was unable to find out how I can program the script to take the first 7 chracters and put them to the end.
Any help is appreciated.
Given the little detail on the exact formatting of your structures, i.e what happens in the event a surname has a split like van Halen which also now contains a space:
anyway, this will cater for the situation as you've mentioned only and not for names/surnames containing spaces.
#echo off
for /f "tokens=1-3*" %%i in ('dir /b /a-d *.txt ^| findstr /R "^[1-9]"') do echo ren "%%i %%j %%k" "%%~nk %%~nj %%~ni%%~xk"
Note this example will simply echo the command and not perform the actual rename. You need to test it first before you do the renaming. Once you are happy with the result printed to console, then remove echo from the line.
Note. findstr is important here as we need to only perform the action if the file starts with numbers. You can define the findstr filter even more if you want to be more specific. Here I just focused on numbers in the beginning of any .txt file considering no name or surname should start with a number., Unless you're 50cent or some other random rapper.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
rem The following setting for the source directoryis a name
rem that I use for testing and deliberately includes spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
FOR %%b IN ("%sourcedir%\??????? * *.txt") DO FOR /f "tokens=1*delims= " %%u IN ("%%~nb") DO SET /a junk=0&SET /a "junk=1%%u" 2>nul&IF !junk! geq 10000000 IF !junk! leq 19999999 ECHO REN "%%b" "%%v %%u.txt"
GOTO :EOF
Please note that this routine uses delayedexpansion
The first for puts the absolute filename of each file matching the mask into %%b
The second partitions the name part of the filename (%%~nb) into that part before the first space (token 1) to %%u and the remainder (token *) to %%v
junk is then set to 0 and then reset to the value of 1%%u. set /a will leave junk unchanged (therefore, 0) if %%u is not a numeric string (the 2>nul suppresses the error message) so if %%u is numeric, junk will be set to 10000000 ... 19999999.
Use !junk! to access the run-time value of junk, check it is within range and if so, echo the ren required.
Remove the echo keyword before the ren after checking the resultant report to actually rename the files.
I need to split some text in lines and concatenate with a sufix using Windows cmd .bat.
I recieve lists that came like:
9448
9453
9463
9464
9474
9477
or like:
9448, 9453, 9463, 9464, 9474, 9477
So I need to put every number of these added with .jpg, like:
9448.jpg
9453.jpg
9463.jpg
them the program would run the way I need.
here goes the code I'm working on:
echo off
for %%a in (.) do set currentfolder=%%~na
set src_folder= %CD%
set dst_folder= "%currentfolder%_SELECTED/%date:/=%%"
md %dst_folder%
for /f %%i in (list.txt) DO copy %%i %dst_folder%\%%i
run two nested for loops: one to split into lines and another to split a line into separate tokens. So you don't have to care, which of the two formats the file has.
#echo off
for /f "delims=" %%a in (list.txt) do (
for %%b in (%%a) do (
ECHO copy "%%b.jpg" "%dst_folder%\%%b"
)
)
Note: it isn't clear to me, what you exactly try to do. Adapt the ECHO line until the output is what you want, then remove the ECHO.
I wanted to make help generator for all command in help in Windows cmd and write it to separate files. So you are asking /? on all commands that are on the list when you type help in cmd.
Here are the main part of my code:
rem mypath - it's a folder where I put my results
rem In help all command are written by capitals letters
for /f "tokens=1 usebackq" %%i in (`help^|findstr /B /R "[QWERTYUIOPASDFGHJKLZXCVBNM][QWERTYUIOPASDFGHJKLZXCVBNM]"`) do (
if NOT "%%i"=="GRAFTABL" (
if NOT "%%i"=="DISKPART" (
if NOT "%%i"=="SC" (
help %%i > !mypath!\%%i.txt
)
)
)
)
I use all sequence from [Q..M] in my Regular exp because there are some problems with just set of [A-Z]
But the problem is that in my FOR and IF files - there are help for REM command. Does anyone have any idea why is it so ?
To fix it I use:
FOR/? >%mypath%\FOR.txt
IF/? >%mypath%\IF.txt
But I can't understand why it is so.
The code you posted in your question gives the correct result for me (even before I reformatted it a bit).
See Why does findstr not handle case properly (in some circumstances)? for an explanation of why [A-Z] does not work properly with FINDSTR. For an exhaustive list of known FINDSTR quirks, see What are the undocumented features and limitations of the Windows FINDSTR command?
A better way to filter out command names is to look for lines that begin with at least one non-space character, followed by any number of additional non-space characters, followed by 2 spaces.
If you want to ignore certain commands, you can simply use an additional FINDSTR with the /V option.
The solution becomes a reasonable one liner that can run from the command prompt without a batch script:
for /f %A in ('help^|findstr /rc:"^[^ ][^ ]* "^|findstr /v "GRAFTABL DISKPART SC"') do #help %A >%A.txt
Or as code that can be plugged into your script:
for /f %%A in (
'help^|findstr /rc:"^[^ ][^ ]* "^|findstr /v "GRAFTABL DISKPART SC"'
) do help %%A >"!mypath!\%%A.txt"
EDIT - 2015-10-11
The first and last lines of HELP output start with the word For (mixed case), on my English machine. That word happens to be a valid command with help, so the FOR.TXT file gets created 3 times.
I presume that all languages use mixed case for the first and last line. It is not hard to refine the FINDSTR filter to exclude any line where the 2nd character is space or a lower case character:
for /f %A in ('help^|findstr /rvc:"^.[abcdefghijklmnopqrstuvwxyz ]"^|findstr /v "GRAFTABL DISKPART SC"') do #help %A >%A.txt
I couldn't reproduce your issue, but this worked correctly for me and I found a simpler regular expression:
#echo off
for /f %%i in ('help^|findstr /B /R [A-Z][^^^^o]') do (
if NOT "%%i"=="GRAFTABL" (
if NOT "%%i"=="DISKPART" (
if NOT "%%i"=="SC" (
help %%i > %%i.txt
)
)
)
)
I'm trying to get a side-by-side file path and file name in a text file so I can make inserting into a database easier. I've taken a look at other examples around SO, but I haven't been able to understand what is going on. For instance, I saw this batch file to append file names to end of lines but figured that I shouldn't ask for clarification because it's 1.5 years old.
What I have is a text file of file paths. They look like this:
\\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
What I want it to look like is this:
1AD0019.tif \\proe\igi_files\TIFFS\AD\1_SIZE_AD\1AD0019.tif
so that I can insert it into a database. Is there an easy way to do this on Windows via Batch files?
No batch file required. From the command line:
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do #echo %~nxF %~dpF)
But that output format is risky because file and folder names can contain spaces, so it may be difficult to determine where the file name ends and the path begins. Better to enclose the file and path within quotes.
>"outputFile.txt" (for /f "usebackq eol=: delims=" %F in ("inputFile.txt") do echo "%~nxF" "%~dpF")
if done within a batch file, then percents must be doubled.
#echo off
>"outputFile.txt" (
for /f "usebackq eol=: delims=" %%F in ("inputFile.txt") do echo "%%~nxF" "%%~dpF"
)
You should read the built in help for the FOR command. Type help for or for /? from a command prompt to get help. That strategy works for pretty much for all commands.
In powershell, this little script should do the trick. In the first line, just specify the name of the text file that contains all the file paths.
$filelist="c:\temp\filelist.txt"
foreach($L in Get-Content $filelist) {
$i = $L.length - $L.lastindexof('\') -1
$fname=$L.substring($L.length - $i, $i)
echo ($fname + ' ' + $L)
}
If you don't have powershell installed on your machine, check out http://technet.microsoft.com/en-us/library/hh847837.aspx.
#ECHO OFF
SETLOCAL
(
FOR /f "delims=" %%i IN (yourfile.txt) DO ECHO %%~nxi %%i
)>newfile.txt
GOTO :EOF
No big drama - all on one active line, but spaced for clarity
I'm trying to remove the first 10 characters from multiple lines inside a text file using a batch script, then output the results to a new file. I ran across this and it got me pointed in the right direction but the final output isn't working.
Here's what I've got so far:
setLocal EnableDelayedExpansion
CSCRIPT /nologo %windir%\System32\prnport.vbs -l > c:\IPPorts.txt
type c:\IPPorts.txt | findstr IP_ > c:\IPPorts2.txt
for /f "tokens=*" %%a in (c:\IPPorts2.txt) do (set line=%%a set chars=!line:~10! > c:\IPPorts3.txt)
for /f "delims=" %%x in (c:\IPPorts3.txt) do CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%x
The 2nd line exports a list of printer ports to a file named IPPorts.txt. The 3rd finds the lines with "IP_" in them and exports to IPPorts2.txt. The 4th line is supposed to remove unneeded text (which it isn't doing) and export to IPPorts3.txt. And the last line will take the results from IPPorts3.txt and then delete those ports.
IPPorts.txt is as follows:
Server name
Port name IP_172.20.51.11
Host address 172.20.51.11
Protocol RAW
Port number 9100
SNMP Disabled
These lines are repeated for every port, of which there are several. Since I only need the line containing the port name, IPPorts2.txt looks like this:
Port name IP_172.20.51.11
Port name IP_172.20.52.58
Port name IP_172.20.53.16
Port name IP_172.20.54.19
Port name IP_172.20.55.15-1
Port name IP_172.20.55.15
Port name IP_172.20.55.11
Where I'm having trouble is removing the "Port name " portion of the lines (the first 10 characters). I want the output to read on each line as "IP_X.X.X.X". The problem is the 3rd file is always empty.
Where am I going wrong? Any help is greatly appreciated.
EDIT:
This is further down under Endoro's answer, but I thought it might be nice to post the answer here. Here's what I changed the 4th line to:
for /f "tokens=* delims=" %%c in ('type c:\IPPorts2.txt') do (
set LINE=%%c
>> c:\IPPorts3.txt echo !LINE:~10!
)
This has corrected my problems. Thanks everyone!
try this:
(for /f "tokens=3" %%i in (IPPorts2.txt) do #echo %%i)>IPPorts3.txt
Script to get directory name out of DIR command output :
...
20/09/2014 01:23 [DIR] some1
21/09/2014 02:34 [DIR] some2
22/09/2014 03:45 [DIR] some3
23/09/2014 11:22 [DIR] some4
...
We want it to be:
some1
some2
some3
some4
...
Code :
#FOR /f "tokens=4" %%D IN (i:\test.txt) DO #( echo %%D ) >> result.txt
In your case tokens=3, not perfect but does the job with few lines manually edited in the result.
(For /f "tokens=3delims= " %%i in (ipports2.txt) do echo %%i) >ipports3.txt
should do it for you.
The paretheses are important - ensure that the file is created anew. If omitted, will only generate the last line.
Simply uses the delimiter [space] to tokenise the string on each line into token1=Port, token2=Name and sets %%i to each token3 in turn.
The following isn't really a different solution but merely a suggestion to simplify your script by reducing the number of output files.
In fact, it is possible to exclude all of them from the script, unless you need to keep them for history.
Basically, the idea is first to apply FINDSTR directly to the output of prnport.vbs:
CSCRIPT /nologo %windir%\System32\prnport.vbs -l | FINDSTR "IP_"
then apply a loop directly to the output of FINDSTR (note the single quotation marks around the piped command line, as well as the escaped |):
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO …
and call prnport.vbs with another set of arguments in that same loop:
FOR /F "tokens=3" %%A IN (
'CSCRIPT /nologo %windir%\System32\prnport.vbs -l ^| FINDSTR "IP_"'
) DO (
CSCRIPT /nologo %windir%\System32\prnport.vbs -d -r %%A
)
The tokens option of a FOR /F loop specifies which token (or field) to take based on a specific delimiter or set of delimiters. The default set of delimiters is a space, a comma, a tab. Your Port name IP_whatever lines conveniently consist of exactly three tokens and the third one is what you are after, hence "tokens=3" in the options.
So, as you can see, no output files, the necessary value is extracted and passed to the target command in the same iteration.