jamar49
asked on
Rename and ftp a file
I want improve this batch script I wrote to rename a file and upload
@echo off
#Rename Files
cd c:\aaaa\bbbb\
ren QQ-PS.txt QQ-PS.%date:~-10,2%%date:~ -7,2%%date :~-2,4%.%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
ren QQ-OR.txt QQ-OR.%date:~-10,2%%date:~ -7,2%%date :~-2,4%.%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
#Send QQ file to SFTP SERVER
ftp -n -i SFTP SERVER <<END
user xxxx xxxxx
lcd c:\aaaa\bbbb\
cd "\ccc\ddd\"
bin
mput QQ-*.*.txt
bye
exit
@echo off
#Rename Files
cd c:\aaaa\bbbb\
ren QQ-PS.txt QQ-PS.%date:~-10,2%%date:~
ren QQ-OR.txt QQ-OR.%date:~-10,2%%date:~
#Send QQ file to SFTP SERVER
ftp -n -i SFTP SERVER <<END
user xxxx xxxxx
lcd c:\aaaa\bbbb\
cd "\ccc\ddd\"
bin
mput QQ-*.*.txt
bye
exit
I think is the format you have for date currently for instance:
Which could make it:
@echo off
echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& right(year(date),2)^& "." ^& right(100+hour(time),2) ^& right(100+minute(time),2) ^& right(100+second(time),2)> "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set now=%%a
echo Now you can use this in your filename: %now%
Which could make it:
@echo off
REM Get date and time now in format ddmmyy.hhmmss
echo wscript.echo right(100+day(date),2) ^& right(100+month(date),2) ^& right(year(date),2)^& "." ^& right(100+hour(time),2) ^& right(100+minute(time),2) ^& right(100+second(time),2)> "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set now=%%a
REM Rename files
cd /d cd:\aaa\bbb
ren qq-ps.txt qq-ps.%now%.txt
ren qq-or.txt qq-or.%now%.txt
REM Upload them to FTP Server
(echo open servername
echo user xxxxx
echo password
echo lcd %cd%
echo cd "\ccc\ddd\"
echo bin
echo mput qq-*.*.txt
echo quit) | ftp -n -i
ASKER
Now I'm getting the following error but if I remove the syntax for time is works for date.
The syntax of the command is incorrect.
ren qq-ps.txt qq-ps.%date:~-10,2%%date:~ -7,2%%date :~-2,4%_%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
ren qq-or.txt qq-or.%date:~-10,2%%date:~ -7,2%%date :~-2,4%_%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
The syntax of the command is incorrect.
ren qq-ps.txt qq-ps.%date:~-10,2%%date:~
ren qq-or.txt qq-or.%date:~-10,2%%date:~
Did you look at my comment and suggestion?
Your two commands are fine. if you add the word ECHO before them then you can see what it is trying to do rather than doing it:
ECHO ren qq-ps.txt qq-ps.%date:~-10,2%%date:~ -7,2%%date :~-2,4%_%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
ECHO ren qq-or.txt qq-or.%date:~-10,2%%date:~ -7,2%%date :~-2,4%_%t ime:~0,2%% time:~3,2% %time:~6,2 %.txt
ECHO ren qq-ps.txt qq-ps.%date:~-10,2%%date:~
ECHO ren qq-or.txt qq-or.%date:~-10,2%%date:~
ASKER
Thanks, I will added "ECHO". Also let be clear the only time it fails is for single digit hours(00-0900), it works fine for hours (1000-2300)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You can amend %date% bits so that they are more reliable without using dates as strings like here... also at the moment your time and date will move on from the one rename to the next so, if it is important, the filenames could be different at the moment. That could be as simple as setting variable to the date and using that in both renames. You could use cd /d instead of cd so it changes drive too if needed, I haven't amended date bits yet but I prefer doing ftp like this.
Open in new window
You can tidy up the way FTP is done a bit like some of options here: http://scripts.dragon-it.co.uk/links/batch-ftp-scriptingand dates to calling VBScript to get the year/month etc. if wanted here: https://www.experts-exchange.com/articles/1153/Using-dates-in-batch-files-scripts.html
Steve