Solved Read file info and other operations with 1 Bat script - Help

October 17, 2020 at 15:04:51
Specs: Windows 7/8/10
This is all for one specific folder with nothing more than a few images inside where we run all .bat files:

FOR /L %%a IN (1, 1, 3) DO (
echo /00tst/#.png
) >> fileA.txt

so content of fileA.txt will be:
/00tst/#.png
/00tst/#.png
/00tst/#.png

then I have "C:\Users\Administrator" folder with 3 downloaded images:
dog cat bird *.png 34byte's 50byte's 90 byte's

for /f %%I in ('dir /o:d /b "C:\Users\Administrator\"') do @echo %%~znI >> fileB.txt
(or %HOMEPATH%)

so fileB.txt would look like this:
dog 34
cat 50
bird 90

The first and most important part of my question/help request is:

Is there a comfortable method to combine both ideas into one .bat file to create only a fileA.txt with this content? (also replace # symbol, + add file ext. + add some commas as separator)
/00tst/dog.png,34,
/00tst/cat.png,50,
/00tst/bird.png,90,

and part 2 - can I somehow include a list of several web page links in the final .bat? /or alternatively read the list from another .txt ...

example list of web page links:
https://www.examplepage.com/?t=alg
https://www.examplepage.com/?t=qwe
https://www.examplepage.com/?t=sdf

... and place these in order after the last separator symbol (,) in each line?

so that finally the fileA.txt content would look like this?
/00tst/dog.png,34,https://www.examplepage.com/?t=alg
/00tst/cat.png,50,https://www.examplepage.com/?t=qwe
/00tst/bird.png,90,https://www.examplepage.com/?t=sdf

Thanks for any ideas! This task is probably not the super easiest.
I'm not sure how much my existing code snippets could be helpful - the end result is important, you can probably do something completely different.

(I need the above string format for my online assets download routine as encrypted content in a mainly Swift based iOS app. I want some automatization here, because it's a bit tedious when I have to prepare this manually for hundreds of different assets - a batch solution could also prevent possible spelling errors or other mistakes.)

message edited by taraz


See More: Read file info and other operations with 1 Bat script - Help


#1
October 17, 2020 at 19:36:26
@echo off & setlocal
for /f %%a in ('dir /b /od C:\Users\Administrator\*.png') do echo /00tst/%%~nxa,%%~za,

::---------- end of the beginning
that part doesn't seem to present much problem. What you failed to elucidate is how the urls are linked to the object-lines. If there's no xref, then it could only be one-to-one line synch to a url-list text file. I'll wait to see...


Reply ↓  Report •

#2
October 18, 2020 at 02:54:19
Thanks for your help.

About next part:
No xref, yes. I can generate the file links from my web site as .txt in ascending/descending upload date+time order which is the same order of my files in the folder that I use for the final upload.
So linking this content
/00tst/dog.png,34,
/00tst/cat.png,50,
/00tst/bird.png,90,
with one-to-one line sync to my url list .txt would be ok.

I found a few solutions online - the best method in my opinion was in stackoverflow question 8152861, here slightly modified by me:

@echo off
setlocal EnableDelayedExpansion
REM Load file 1 into array ARR1:
set i=0
for /F "delims=" %%a in (1.txt) do (
set /A i+=1
set ARR1[!i!]=%%a)
REM Load file 2 into array ARR2:
set i=0
for /F "delims=" %%a in (2.txt) do (
set /A i+=1
set ARR2[!i!]=%%a)
REM Merge data from both files and create file 3:
for /L %%i in (1,1,%i%) do echo !ARR1[%%i]!!ARR2[%%i]!>>3.txt
▼▼▼
How could I effectively combine the example in your first answer with this solution or another possible solution, so I can work with a single .bat file + one .txt file that contains the url list?

message edited by taraz


Reply ↓  Report •

#3
October 18, 2020 at 19:36:18
✔ Best Answer
The main ways I can offer, two approaches. One using "skip=", the other using 'find /v /n "" to generate a file with line numbers attached (thus, a temp file required). Batch cannot read sequentially on two channels, so you gotta have the main channel, and a "side sweep" following the main. I'll code (but not yet test) the two methods. This is the 'skip=' method:
@echo off & setlocal
set sk=
set c=-1
for /f %%a in ('dir /b /s /od C:\Users\Administrator\*.png') do call :xx "%%a"
exit /b

:xx
set /a c+=1
if %c% gtr 0 set sk=skip=%c%
:: retrieve the line-linked url
for /f "%sk% tokens=*" %%b in (urls.txt) do (
echo /00tst/%~nx1,%~z1,%b
goto :eof
)
:: end version one using 'skip='. Below is the 'find' version:
@echo off & setlocal
:: first build the temp file of urls
find /n /v ""<urls.txt > temp.txt
set c=0
for /f %%a in ('dir /b /s /od C:\Users\Administrator\*.png') do call :xx "%%a"
goto :eof
:xx
set /a c+=1
:: retrieve the linked url
for /f "tokens=1* delims=[]" %%b in ('find "[%c%]"^<temp.txt') do echo /00tst/%~nx1,%~z1,%c

::------------ end version two

Either concept should work, in theory (but we all know what happens to theories in practice or in the field!) Half-ass tested (but we also know how that goes!) If you have a time-stamp relationship, (urls to pngs),that might be an alternative for linking.

message edited by nbrane


Reply ↓  Report •

Related Solutions

#4
October 19, 2020 at 04:29:51
Both versions are working correctly. Thanks a lot!
I had to write dual % signs for b and c at the end of your code examples though.

Optionally I was looking for ideas to get output text in general with no empty line at the end.
Are below modifications an effective way to solve this task?

@echo off & setlocal
set "Discard_endingCRLFforall_ConvertIn_startingCRLF="
set sk=
set c=-1
for /f %%a in ('dir /b /s /od C:\Users\Administrator\*.png') do call :xx "%%a"
exit /b

:xx
set /a c+=1
if %c% gtr 0 set sk=skip=%c%
:: retrieve the line-linked url
<nul (for /f "%sk% tokens=*" %%b in (urls.txt) do (
if defined Discard_endingCRLFforall_ConvertIn_startingCRLF (echo() else (set "Discard_endingCRLFforall_ConvertIn_startingCRLF=1")
set /p ".=/00tst/%~nx1,%~z1,%%b"
goto :eof
)) >> output.txt
:: end version one using 'skip='.

message edited by taraz


Reply ↓  Report •

#5
October 19, 2020 at 19:06:28
Not sure what crlf you want to eliminate - ie where they enter the text-stream. For general reference, to output without crlf, this is common practice:
set /p=<nul>testing ABC %bb%
This covers both a literal, ABC, and a variable, %bb%, and outputs no CRLF to 'testing'.

Reply ↓  Report •

Ask Question