How to rename file with pattern using batch script - windows

For example I have a .txt file named pair.txt.
Example:
AAA_BBB_CCC_DDD_EEE_FFF_GGG_HHH.idoc.xml AAAA
AAA_BBB_CCC_DDD_EEE_FFF_111_222.idoc.xml BBBB
AAA_BBB_CCC_DDD_EEE_FFF_333_444.idoc.xml CCCC
Now this file contains 2 columns of filenames. First column will be the pattern to rename the second column. Now I want to use the right side of 6th and 7th "_" as pattern. The final filenames of the files in the second column must be:
AAAA.GGG_HHH
BBBB.111_222
CCCC.333_444
As you noticed, I didn't include the .idoc.xml part. Now I want to put the code within this for statement:
for /f "tokens=1,2" %%a in ('type c:\user\pair.txt') do (
echo Renaming file : %%b
)
How will I able to do this?

for /f "usebackq tokens=1,2" %%a in ("c:\user\pair.txt") do (
for /f "tokens=7,8 delims=_." %%c in ("%%a") do (
echo Renaming file : %%b = %%b.%%c_%%d
)
)
Use a second for command to split the first column and then use the adecuated tokens

Related

batchscript: Read file line by line and extract paths

I want to read a file content line by line and then in each line I want to retrieve only the paths.
sample.txt:
\.\docusp\ui\test\java\services\gway->\.\docusp\ui\test\java\pserv\com\controller\services\gway pserver\2 (5660\1) (backed) (elink)
\.\docusp\ui\test\java\pserv\services\run->\.\docusp\ui\test\java\pserv\com\controller\services\rip pserver\1 (376\1) (backed) (elink)
Output:
path: \.\docusp\ui\test\java\services\gway
target: \.\docusp\ui\test\java\pserv\com\controller\services\gway
path: \.\docusp\ui\test\java\pserv\services\run
target:\.\docusp\ui\test\java\pserv\com\controller\services\rip
test.bat:
#echo off
for /f "delims=" %%a in (C:\sample.txt) DO (
ECHO Line is: %%a
set line=%%a
for /f "tokens=1,2 delims=->" %%a in ("%line%") do (
echo %%a
echo %%b
set path=%%a
set target=%%b
REM target contains the substring after ->. Want to remove "pserver\2 (5660\1) (backed) (elink)" string
)
)
In each line target contains,
\.\docusp\ui\test\java\pserv\com\controller\services\rip pserver\1 (376\1) (backed) (elink)
Need to remove pserver\1 (376\1) (backed) (elink) and target should only contain \.\docusp\ui\test\java\pserv\com\controller\services\rip.
Note: target path may contain space in between. based on space and get substring will not work.
Any help is appreciated.
The problem here is that you are not clear about the details of your problem that are important, so I must did a guess about these two points:
The separation between the target and the part you want to remove is TWO SPACES as shown in the provided sample.txt file.
When you said "target path may contain space in between" I assume that the target path may contain ONE SPACE in between, not two adjacent spaces...
The working solution below is based on such assumptions.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in (sample.txt) DO (
ECHO Line is: %%a
set line=%%a
for /f "tokens=1,2 delims=->" %%a in ("!line: =-!") do (
echo path: %%a
echo target: %%b
)
)

Batch file to insert string in exact position in file txt

I have a test.txt file like this:
sylvester, stallone, 35,20, florida;
brad, pitt, 40,25, california;
sean, connery, 15,80, london;
I have to create a new one in which the surname begins with the 15th column and the name in the 30th.
I would like to do it with a batch file.
What I have managed to do is this:
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,2 delims=," %%G IN (test.txt) DO (
SET "line=%%H"
SET "spaces= "
ECHO (!spaces!!line!!spaces!%%G
)
) >> output.txt
pause
But in this way %%G does not begin always from the same position, it depends on how many characters has %%H. And more, does not write on output.txt but it makes me see the results on the batch window.
I know it's probably a trivial question, but I'm new to programming.
#echo off
setlocal enabledelayedexpansion
del output.txt
set "spaces= " :: 15 spaces
(FOR /F "tokens=1,2 delims=, " %%G IN (test.txt) DO (
SET "line=%spaces%%%H %spaces%"
set "line=!line:~0,29!"
echo !line:~0,29!%%G
)) >output.txt
add 14 spaces in front of the surname (to let surename start at 15) and append another 15 spaces (to get a cumulated length of at least 29). Then trim it to the first 29 characters and append the name (at Pos 30).
(added a space to the delims for proper handling)

Windows Batch - Find word in one string matching word in another string and capture output

While this may seem easy to some, I've struggled for hours on it.
I have a file:
MYFOLDER,JobE,JobD_ENDED_OK,
MYFOLDER,JobD,JobC_ENDED_OK,JobD_ENDED_OK
MYFOLDER,JobD,JobB_ENDED_OK,
MYFOLDER,JobC,JobA_ENDED_OK,JobC_ENDED_OK
MYFOLDER,JobB,JobA_ENDED_OK,JobB_ENDED_OK
MYFOLDER,JobA,,JobA_ENDED_OK
I need to loop through and find where token 4 in one line matches token 3 in another line and then echo a statement to a file. I am looking for an output file that shows this:
MYFOLDER_JobA_MYFOLDER_JobB matches JobA_ENDED_OK
MYFOLDER_JobA_MYFOLDER_JobC matches JobA_ENDED_OK
MYFOLDER_JobB_MYFOLDER_JobD matches JobB_ENDED_OK
MYFOLDER_JobC_MYFOLDER_JobD matches JobC_ENDED_OK
MYFOLDER_JobD_MYFOLDER_JobE matches JobD_ENDED_OK
I know it's a FOR loop with a DO, I am just not getting the rest of it.
Any assistance is greatly appreciated.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
SET "filename1=%sourcedir%\q46510665.txt"
SET "outfile=%destdir%\outfile.txt"
(
FOR /f "usebackqdelims=" %%h IN ("%filename1%") DO (
SET "col4line=%%h"
SET "col4line=!col4line:,=|,|!"
FOR /f "tokens=1-4delims=," %%a IN ("!col4line!") DO IF "%%d" neq "|" (
FOR /f "usebackqdelims=" %%H IN ("%filename1%") DO (
SET "col3line=%%H"
SET "col3line=!col3line:,=|,|!"
FOR /f "tokens=1-4delims=," %%A IN ("!col3line!") DO (
IF "%%d|"=="%%C" (
SET "reportline=%%a_%%b_%%A_%%B matches %%C"
ECHO !reportline:^|=!
)
)
)
)
)
)>"%outfile%"
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a file named q46510665.txt containing your data for my testing.
Produces the file defined as %outfile%
For each line in the file, set a variable col4line to the entire line via %%h, then replace each , with |,| so that successive , will be separated. Tokenise on , and ignore any line which has simply | as its 4th token (ie last-column-empty).
Repeat the process for every line in the file this time through %%H into col3line (note case differential to use different actual metavariables) and if the third column matches the fourth column+| from the outer loop, assemble the report line from the tokens and output, removing the |s.

Remove header while merging multiple .csv files using batch

I have written the code to concatenate sample files into a single file minus the headers each file.
Input files:
File1:
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
File 2:
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
Expected Output:
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
Actual Output:
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
Please find below code used for this operation:
#echo off
break>Combined.csv
cls
setlocal enabledelayedexpansion
if exist C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv del C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv
dir /a-d /b C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv>C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt
cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\
for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
set /p header=<%%A
if "!header!" neq "" (
(echo(!header!)>Combined.csv
goto :break_for
)
)
:break_for
for /f "tokens=*" %%A in (C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\dirfiles.txt) do (
more +1 %%A>>Combined.csv
)
del dirfiles.txt
}
Can someone please help me resolve this issue. I am a neophyte to batch scripting and unable to debug this issue.
A couple points about this question:
This question is an exact duplicate of Windows Batch file execution error
At that question there are 4 answers, one of which is mine.
In my answer I asked you to post a small section of your data files, but you never replied.
This is a copy of my answer at that question after I slightly modified it in order to insert the key point of your problem: the headers contain TWO lines:
EDIT: I modified the code accordingly to the new specifications posted in a comment: there are three lines of headers in each file, but just the 3rd must be included in the output.
#echo off
setlocal enabledelayedexpansion
cls
REM cd C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\
set "header3="
(for %%A in (*.csv) do (
if not defined header3 (
(set /p "header1=" & set /p "header2=" & set /p "header3=") <%%A
echo !header3!
)
more +3 %%A
)) > Combined.txt
And this is the generated Combined.txt file when this program run with your data above:
.
[ Row : Header ],,,,,,,,,
ContractNum,ProgramNum,CustomerNum,TierNum,StartDate,EndDate,DateCreated,CreatedBy,DateUpdated,UpdatedBy
00032116,21238,60304PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00032116,21238,81790PRMI,3,2014-05-02,2017-09-30,Administrator,Administrator,2016-02-29 10:46:14,2016-02-29 10:46:14
00024067,15562,9942PRMI,1,2014-09-16,2016-12-31,gintgUser,gintgUser,2016-02-21 05:59:43,2016-02-21 05:59:43
As you can see, the output is the same you want.
EDIT: I can't test the modification because the posted input files does not contain the same data as the real files...
You should follow up the questions you post and not post new questions with the exact same problem of a previous one.
You should be clearer in the description of your problem and post an example data.
There is no need for an interim file that contains a list of CSV files, you can read and combine them by a standard for loop and a nested for /F loop, using its skip option to get rid of the headers (assuming the header is always a single line). The initial header can be taken from another for/for /F loop construct that is broken upon its first iteration:
> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
for %%F in ("C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv") do (
for /F "usebackq eol=| delims=" %%L in ("%%~F") do (
echo(%%L
goto :LEAVE
)
)
)
:LEAVE
>> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
for %%F in ("C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv") do (
for /F "usebackq skip=1 eol=| delims=" %%L in ("%%~F") do (
echo(%%L
)
)
)
If you need a specific sort order of the CSV files, you need another for /F loop instead of the standard for loop that parses the output of a dir /B command to do that job. The following example takes a two-line header, then it sorts the files from oldest to newest modification dates:
> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
set "FLAG="
for %%F in ("C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv") do (
for /F "usebackq eol=| delims=" %%L in ("%%~F") do (
echo(%%L
if defined FLAG goto :LEAVE
set "FLAG=#"
)
)
)
:LEAVE
>> "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\Combined.csv" (
for /F "eol=| delims=" %%F in ('
dir /B /A:-D /O:D /T:W "C:\Users\kartikeya.avasthi\Desktop\Batch_Scripts\ContractEligibility_*.csv"
') do (
for /F "usebackq skip=2 eol=| delims=" %%L in ("%%F") do (
echo(%%L
)
)
)
If you felt like installing awk - one of the handiest programs around from Unix/Linux - your task would become very simple. It is available for Windows from here.
Then you could just use:
awk 'NR<3 || FNR>2' *.csv
To explain the command, you need to know that NR is the Number of the Record (i.e. the line number) and it starts at one for the first record/line of the first file and then increments with each record, so it will be less than 3 for just the first two records of just the very first file. FNR on the other hand, is the File Number of Record which is the same, but it resets to one as each new file is opened, so it will be less than 2 for the first two records of every file.
So, in summary, the command says... "Print any line if it is one of the very first two lines of all the input files, or if it is past line 2 of any of the files."
Note that you may need to replace the single quotes with double quotes on Windows.
Note that if you were to download gawk, it will work just the same as awk for this example.

Batch Scripting:search file, extract numbers, and copy into new file

I'm new to batch scripting and have a question. I was wondering if it's possible to search a .txt file by requirements and take data specified and copy into a new .txt file?
Like if I have 50 lines with 9 digit numbers and a bunch of other crap I don't need after them can I say,
"For any line beginning with a 1,2,3,4,5,6,7,8,or 9...take the first 9 digits and copy them into a new file, for all lines in the file???"
I thought this would be easier than trying to delete all the other stuff. Let me know if you know anything about how to do this! Thanks.
Here's an example of what one line looks like:
123456789#example
and I just need to extract the 9 digit numbers from about 50 lines of this.
You can use FINDSTR to filter out all lines that do not start with 9 digits. Then FOR /F can read the result, line by line. A variable is set, and a substring operation preserves just the first 9 digits.
#echo off
setlocal enableDelayedExpansion
(
for /f %%A in (
'findstr "^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]" yourFile.txt'
) do (
set "ln=%%A"
echo !ln:~0,9!
)
)>newFile.txt
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
(
FOR /f "delims=" %%a IN (q24824079.txt) DO (
SET "line=%%a"
REM set DIGITS to the first 9 characters of LINE
SET "digits=9!line:~0,9!"
FOR /L %%z IN (0,1,9) DO SET "digits=!digits:%%z=!"
IF NOT DEFINED digits ECHO(!line:~0,9!
)
)>newfile.txt
GOTO :EOF
I used a file named q24824079.txt containing data for my testing.
Produces newfile.txt
You did not specfy what to do if the line was all-digits but has fewer than 9 characters. I chose to report that line.
Hopefully this helps getting the job done:
#echo off
setlocal enabledelayedexpansion
for /f %%e in (emails.txt) do (
echo Email: %%e
for /f "delims=# tokens=1" %%b in ("%%e") do (
set BEGIN=%%b
echo Name: !BEGIN!
set FIRST=!BEGIN:~0,1!
echo First char: !FIRST!
set /a NUMERIC=!FIRST!+0
echo Converted to number: !NUMERIC!
if !FIRST!==!NUMERIC! echo Yippieh!
echo.
)
)
Instead of echo Yippieh! append the email (%%e) to a file, e.g. like
echo %%e >> output.txt

Resources