Tuesday, July 21, 2015

windows - Batch File runs exe, creates file, saves and post in new folder nightly




This was a repost, I had it and was told it was better suited here for the superuser site. Not sure the difference. First time visitor to either site...but here it goes. ~Cheers



I am a novice at best struggling along as my job is evolving and owner will not provide training. Thus, I have a multiple batch files codes that read like this or very similar, ultimately running a nightly report and dropping the pdf file into a shared drive for me.



My batch reads like this and has five reports...I entered two below.



R:\Simply\Simply.exe /u:username /p:password /rpt:"Daily Report" /pgp:Custom /pgs:pdf /el:R:\reports\str001\DailyReports /s

R:\Simply\Simply.exe /u:username /p:password /rpt:"Negative Report" /pgp:Custom /pgs:pdf /el:R:\reports\str001\DailyReports /s



Every morning I have to create a new folder with yesterdays date and move all the files that were generated to this folder.



I should note that this batch needs to run on winxp and win7 so dating from what I have read needs to take this into consideration.



I have read and tried several posts on here. I am not sure where to put the code, how to insert it behind my existing code, infront of, on each line. Again, novice looking for some advice. Hell, I even tried running a new .bat file with only creating a new folder code based on other posts on here...and I couldn't get those to work. I can link them here but that seems a waste of time. Trust me, I've tried to solve this....I enjoy solving and learning but I'm lost. Please help



I'd like to have the batch file create a folder dated with yesterdays date in format of YYYY_MM_DD and then save the files into said folder automatically when it runs rather than me having to move them daily over and over again.




Note: If the date exists I do not want to overwrite the data but would like to drop the files into that location.


Answer



This solution assumes your reports are PDFs and that they are the only PDFs in your report folder. Additionally, some of this solution was adapted from:




https://stackoverflow.com/questions/20796749/re-naming-a-file-name-to-include-yesterdays-date-using-command-prompt/20798129#20798129




New batch file contents:




@ECHO OFF

R:\Simply\Simply.exe /u:username /p:password /rpt:"Daily Report" /pgp:Custom /pgs:pdf /el:R:\reports\str001\DailyReports /s

R:\Simply\Simply.exe /u:username /p:password /rpt:"Negative Report" /pgp:Custom /pgs:pdf /el:R:\reports\str001\DailyReports /s

... (more of your report stuff goes here)

SET day=-1
ECHO >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)

ECHO >>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
FOR /f %%a IN ('cscript /nologo "%temp%\%~n0.vbs"') DO SET "result=%%a"
DEL "%temp%\%~n0.vbs"
SET "YYYY=%result:~0,4%"
SET "MM=%result:~4,2%"
SET "DD=%result:~6,2%"
SET "date-yesterday=%yyyy%_%mm%_%dd%"

IF NOT EXIST %date-yesterday%\ (
MD %date-yesterday%

)

MOVE *.PDF %date-yesterday%

No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...