Solved

rename file using bat

Posted on 2014-11-05
4
286 Views
Last Modified: 2014-11-06
Hi,

At around 10am daily I get a csv file saved to a network drive called 01rev.csv, I would like to create a batch file to rename the file to the previous days date in the format yyyymmdd, how would I do this?
0
Comment
Question by:hellblazeruk
  • 2
4 Comments
 
LVL 83

Expert Comment

by:oBdA
ID: 40424172
This uses a temporary VB script to do the date calculation; it's currently in test mode and will only display the "ren" command it would normally run. Remove the uppercase ECHO in line 11 to run it for real:
@echo off
setlocal enabledelayedexpansion
set File=C:\Temp\01rev.csv
set AddDays=-1
for /f "tokens=1 delims=[]" %%a in ('type "%~f0" ^| find /n "[VBScript]"') do set VBStart=%%a
(for /f "skip=%VBStart% delims=" %%a in ('type "%~f0"') do (echo %%a)) >"%Temp%\GetTimeStamp.vbs"
for /f "delims=" %%a in ('cscript.exe /nologo "%Temp%\GetTimeStamp.vbs" %AddDays%') do (set TimeStamp=%%a)
del "%Temp%\GetTimeStamp.vbs"
echo Time stamp: %TimeStamp%
for %%a in (%File%) do set Extension=%%~xa
ECHO ren "%File%" "%TimeStamp%%Extension%"
echo Done.

goto :eof
REM ===== No batch below this line! =====
[VBScript]
NewDate = DateAdd("d", WScript.Arguments(0), Now)
NewYear = Year(NewDate)
NewMonth = Month(NewDate)
NewDay = Day(NewDate)
If NewMonth < 10 Then
	NewMonth = "0" & NewMonth
End If
If NewDay < 10 Then
	NewDay = "0" & NewDay
End If
Wscript.Echo NewYear & NewMonth & NewDay

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 40424197
I like that approach for being more generalised but would this do for just yesterdays date  (from article of mine here:)

http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html - specifically the yesterday-yyyymmdd.cmd one:

@echo off
REM Create temporary VBS file to make yesterday's date:
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

cd /d C:\somedir
ECHO rename 01rev.csv "%yesterday%.csv"

Open in new window

0
 

Author Comment

by:hellblazeruk
ID: 40424386
Hi Steve,

Thanks for the link,
so my batch file is now

REM Generate File
d:
cd \Export\
dbisql.exe -datasource sales -c Server=sqlsalessrv01;UID=custom;PWD=custom -q SalesExport1.sql

echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

ECHO rename 01rev.csv "%yesterday%.csv"

move D:\export\csv\*.csv* Z:\

REM End of Script

This creates the file but its still called 01rev.csv ?
0
 
LVL 43

Accepted Solution

by:
Steve Knight earned 500 total points
ID: 40424893
Sorry should have said, you need to remove the upper case word ECHO like with the previous example.  That means it just echoes to the screen the command rather than doing it (so you could see it was correct)... have adjusted it a bit what I think you are trying to do:

i.e.

@echo off
REM Get yesterdays date
echo wscript.echo year(date - 1) ^& right(100 + month(date-1),2) ^& right(100+day(date-1 ),2)  > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yesterday=%%a

REM Generate File
cd /d D:\export
dbisql.exe -datasource sales -c Server=sqlsalessrv01;UID=custom;PWD=custom -q SalesExport1.sql

REM Rename file and move to z: drive, presume this is what you want to rename?
cd /d D:\export\csv
if exist 01rev.csv (
  echo Renaming file to %yesterday%
  rename 01rev.csv "%yesterday%.csv"
  move *.csv* Z:\
) ELSE (
  echo 01rev.csv not found
  pause
)

REM End of Script

Open in new window


Steve
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now