Replace a character till fixed position in a file by batch command - windows

I hava a file. that contains values like
E:\ABC\XYZ1\1231\AAA\SSS\name1.sql
E:\ABC\XYZ2\1232\AAA\TTT\name2.sql
E:\ABC\XYZ3\1233\AAA\UUU\name3.sql
E:\ABC\XYZ4\1234\AAA\YYY\name4.sql
E:\ABC\XYZ5\1235\AAA\ZZZ\name5.sql
and i have to rearrange these values like
#SSS\name1.sql
#TTT\name2.sql
#UUU\name3.sql
#YYY\name4.sql
#ZZZ\name5.sql
(edit - improved format)

Try this (change the name oft the input file):
#echo off &setlocal enabledelayedexpansion
set "fname=test.txt"
for /f "delims=" %%i in (%fname%) do (
set "fname=%%~nxi"
set "fpath=%%~dpi"
set "fpath=!fpath:~0,-1!
for %%j in (!fpath!) do set "fpath=%%~nxj"
echo #!fpath!\!fname!
)
endlocal
.. and if the path structure is always the same use the command line:
for /f "tokens=6,7delims=\" %i in (test.txt) do #echo #%i\%j

Related

read multiple paths from a .txt file in a batch file

I have a batch script which requires to read multiple directory paths mentioned in config.txt. I am able to achieve this for one directory path but modified version of it is not working for multiple paths.
Below is the sample which is working fine.
#echo off
for /F "usebackq delims=" %%L in (config.txt) do set "DataPath=%%L"
set "DataPath=%DataPath:/=\%"
echo Application path is: %DataPath%
How to modify this to handle for multiple directory paths.
EDIT:
Below is my attempt to get two paths and '%DataPath%' is printing the value.
#echo off
for /F "usebackq tokens=1,2 delims=" %%L in (config.txt) do set
"DataPath=%%L"&set "filepath=%%M"
set "DataPath=%DataPath:/=\%"
set "filepath=%filepath:/=\%"
echo Application path is: %DataPath%
echo Application path is: %filepath%
Based upon the advice I gave in the comments, which you didn't implement in your edit, the following methodology is what I was expecting you to come up with:
#Echo Off
SetLocal DisableDelayedExpansion
For /F "UseBackQ Delims=" %%L In ("config.txt") Do (
Set "DataPath=%%L"
SetLocal EnableDelayedExpansion
Set "DataPath=!DataPath:/=\!"
Echo Application path is: !DataPath!
EndLocal
)
Pause
This assumes that config.txt contains a list, one per line, of just file paths.
Array version of Path recovery from file.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
Set "_P=0"
FOR /F "tokens=* USEBACKQ" %%a IN (%userprofile%\desktop\logfile.txt) DO (
Set /a _P+=1
Set "Pathway[!_P!]=%%a"
)
::: use the below to take actions with elements in the array.
::: For example, search the array for specific files to delete/copy/move/open.
FOR /L %%e IN (1,1,!_P!) DO (
ECHO Path !%%e! = !Pathway[%%e]!
)
pause

Insert a string at a specific position in a file with batch script

I'm new to batch script, I have a file with a string containing the word "media"(quotes included) and I need to insert another string right before it.
I messed around with findstr but couldn't make heads or tails of it.
Edit2:
here's what i did, doesn't seem to work:
#echo off
SETLOCAL=ENABLEDELAYEDEXPANSION
for /f "delims=," %%a in (f1.txt) do (
set foo=%%a
if !foo!=="media"
set var=!foo:"media"=aa"media"!
echo !foo! >> f2.txt)
You have two options to do this. You can read the file with a FOR /F command or if you are just editing a single line file then you can use the SET /P command.
Here are both of those examples in a single batch file.
#echo off
setlocal enabledelayedexpansion
for /F "delims=" %%G in (sotemp.txt) do (
set "line=%%G"
set "foo=!line:"media"=aa"media"!"
echo !foo!
)
set /p "line="<sotemp.txt
echo %line:"media"=aa"media"%
pause

Change each line of text to each column of csv file

I have a text file. For example
This is computer
That one is monitor
Some products are printer
I want to save each line of data into each column of csv file as a one row.
So, I try to create a batch file like this.
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set file=a.txt
set content=
echo %file%
for /F %%i in (a.txt) Do (
set content=!content!,%%i
)
echo %content%>>result.csv
pause
But I get only the first word from every line of text.
When I try to add tokens=* to for loop,
for /F "tokens=*" %%i in (a.txt) Do....
I got the error: The system cannot find the file specified.
I'm not understand whats wrong with my code and I'm not familiar with batch script.
You need to ensure that your protecting spaces in your line content and file names:
#Echo Off
SetLocal EnableDelayedExpansion
Set "fIn=a.txt"
Set "fOut=result.csv"
Set "str="
For /F "UseBackQ Delims=" %%A In ("%fIn%") Do Set "str=!str!,%%A"
If Defined str >>"%fOut%" Echo(%str:~1%
Edit
A slight alternative to cater for some potential problematic characters which may exist in the not provided content of the file:
#Echo Off
SetLocal DisableDelayedExpansion
Set "fIn=a.txt"
Set "fOut=result.csv"
Set "str="
SetLocal EnableDelayedExpansion
For /F "UseBackQ Delims=" %%A In ("%fIn%") Do Set "str=!str!,%%A"
EndLocal & Set/P "=%str:~1%"<Nul>>"%fOut%"
There were several bugs in your code. I've fixed them so now a proper csv string is being generated. However, newer Excel versions actually use ; instead of , to separate cells. This is your code:
#ECHO OFF
setlocal ENABLEDELAYEDEXPANSION
set file=a.txt
for /F %%i in (%file%) Do (
set content=!content!;%%i
)
set content=!content:~1!
echo %content%>>result.csv
pause

Windows Batch job to precede all lines with filename

I have 50 text files and each text file has around 6000 lines. I am looking to make a batch job that will append the filename to each line of the text file.
I have a batch job to append data to the each line but cant wrap my head around getting the filename
This is what I have so far
#echo off
setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
echo ^"(my filename)%%a^",>>output.txt
)
--- Example
filename 3315.txt
124123541234
1234123
2345623462356234
12341234562356245
Desired end result
3315124123541234
33151234123
33152345623462356234
331512341234562356245
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
SET "destdir=U:\destdir"
FOR /f "delims=" %%a IN (
'dir /b /a-d "%sourcedir%\R*.txt" '
) DO (
(
FOR /f "usebackqdelims=" %%q IN ("%sourcedir%\%%a") DO (
ECHO (%%a^)%%q
)
)>"%destdir%\%%~na.out"
)
GOTO :EOF
You would need to change the settings of sourcedir and destdir to suit your circumstances.
I used a filemask of R*.txt to restrict the number of files processed on my test system.
Essentially, build a list of filenames using the dir command, and assign each to %%a in turn.
read each line of %%a into %%q and output to a new file in the destination directory made up of the name part of %%a (%%~na) and .out
--- later
To prepend the name part of the file to each line of the file, change
ECHO (%%a^)%%q
to
ECHO %%~na%%q
%%~na selects the Name part of %%a (see for /f|more from the prompt for more info)

Batch commands working after copying the contents of source text file to another

I have an application which exports some data and stores them in a text file (ex.: abc.txt).
I want to remove quotes from that text file.
So I wrote a batch file for that.
But there is no output in my destination file.
But when I copy the contents of abc.txt to def.txt and run the batch file on this new file (def.txt) then it is working fine.
My Code is:
#echo off & setlocal
set "textfile=%1"
set "newfile=%2"
set "CS1=""
set "CR1="
(for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
setlocal enabledelayedexpansion
set "line=!line:%CS1%=%CR1%!"
echo(!line!
endlocal
))>"%newfile%"
Your code is quite confusing. For me this works:
#echo off
setlocal enabledelayedexpansion
set "textfile=%1"
set "newfile=%2"
for /f "delims=" %%i in (%textfile%) do (
set "line=%%i"
set "line=!line:"=!"
echo !line!>>%newfile%
)
Convert the file from UNICODE to ASCII format using TYPE command

Resources