(.Bat file) Rename a file when it exists

February 4, 2017 at 10:42:54
Specs: Windows XP
I have a script that I use to download some files from a server, the bad thing is that when they are several files they use the same name and I have to go down 1 by 1 and rename them every time I download them, for example: ABC 12-07-2012, I need at the end of the file name to put a number in parenthesis, such as: ABC 12-07-2012 (01), ABC 12-07-2012 (02) and so, could someone help me to reach out To make the script do it?

This is the script:

@echo off

set user=%1
set user_id=%2
set broadcast_id=%3
set tempdir="../_temp/temp_%4"

title Downloading %user% #%broadcast_id%...

echo +-----------------------------------------^>
echo ^| Downloading %user% #%broadcast_id%...
echo +-----------------------------------------^>
echo ^| Press Ctrl+C and write "n" to stop
echo +-----------------------------------------^>
echo.

if not exist %tempdir% md %tempdir%
if not exist "../videos/%user%" md "../videos/%user%"

set /p dir=Directory To Save Stream:
set /d dir="%dir%"

wget --no-check-certificate -q http://api.younow.com/php/api/post/... -O %tempdir%/b_info.txt

for /f "tokens=1,2" %%a in ('xidel -q -e "($json).posts().media.broadcast.dateAired" %tempdir%/b_info.txt') do (
set mydate=%%a
set mytime=%%b
)

set filename=%user%_%mydate%_%count%.mp4
set filepath=%dir%:\%filename%

wget --no-check-certificate -q http://api.younow.com/php/api/youno... -O %tempdir%/session.txt
for /f %%a in ('xidel -q -e "($json).session" %tempdir%/session.txt') do set session=%%a
wget --no-check-certificate -q http://api.younow.com/php/api/broad... -O %tempdir%/rtmp.txt
for /f "tokens=1,2,3" %%a in ('xidel -q -e "join(($json)/(server,stream,hls))" %tempdir%/rtmp.txt') do (
set server=%%a
set stream=%%b
set hls=%%c
)
echo %stream% | findstr m3u8 >nul
if %errorlevel% equ 0 (
ffmpeg -analyzeduration 1000000000 -probesize 1000000000 -i %hls% -bsf:a aac_adtstoasc -pix_fmt yuv420p -c copy %filepath%
) else (
rtmpdump -v -o %filepath% -r "%server%%stream%?sessionId=%session%" -p "http://www.younow.com/"
)

rd /s /q %tempdir% 2>NUL

echo.
echo +-----------------------------------------------------------^>
echo ^| Done^! Saved in %user%'s folder as
echo ^| %filename%
echo +-----------------------------------------------------------^>
echo.

pause
exit


See More: (.Bat file) Rename a file when it exists

Report •

#1
February 5, 2017 at 09:05:46
::====== script starts here ===============
:: sequentilly number files
:: number.bat 2017-02-05 23:51:52.01
@echo off > NEWFILE & setLocal enableDELAYedeXpansioN

:main
for /f "tokens=2 delims=()" %%a in ('dir/b/on ABC*') do (
set #=%%a
)
set #= !#!
set #=!#: 0=!
echo.!#!
set/a #+=1
if !#! lss 10 set #=0!#!
ren NEWFILE "ABC 12-07-2012 (!#!)"
::====== script ends here =================

=====================

M2 Get custom script or take private lessons


Report •

#2
February 7, 2017 at 10:40:34
Thanks for helping me Mechanix2Go but how I integrate this code to the script that I put here because I already try and it does not work, I do not know if you could help me with that, thanks.

Report •

#3
February 8, 2017 at 02:50:58
Do you want these renamed or what:

b_info.txt
session.txt
rtmp.txt

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
February 8, 2017 at 18:45:01
@M2: I think he is referring to these statements where he wants the counter:
set filename=%user%_%mydate%_%count%.mp4
set filepath=%dir%:\%filename%
...
ffmpeg -analyzeduration 1000000000 -probesize 1000000000 -i %hls% -bsf:a aac_adtstoasc -pix_fmt yuv420p -c copy %filepath%

But my personal preference would be to append the system date+time to the filenames on the fly, then if you want them renamed later, you could always go back, with a batch,
to handle that. I don't have the programs you're using, so I can't really run a test...


Report •

#5
February 9, 2017 at 15:50:58
Mechanix2Go, what I need is when there is already a file with the name that the script generates with the data given by the server (example: ABC 12-10-2016.mp4), the script automatically rename me the new file the script are creating ( Ex: ABC 12-10-2016 (1) .mp4, and so on, because i need to download a few files with the same name but in the server have a diferent time, but i don't need the time in the file name

Report •

Ask Question