Solved

MS DOS- Copy & Rename File

Posted on 2007-11-30
5
1,239 Views
Last Modified: 2012-05-05
I have a text file that is on a different server.  I would like to copy and rename the file to a different location.  The issue that I'm facing is that the file names have the date written after them (with the current date).  

The format of the date is year-month-date (with nothing between them).   I know the basic move and rename commands, but I will need to set a variable for these but don't know how to do this.
0
Comment
Question by:jjrr007
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20385277
Here's some code:
@echo off
 

setlocal
 

call :GETDATEPARTS "%date%"
 

set curDate=%yy%%mm%%dd%
 

echo curDate=%curDate%
 

REM ** Above line is test code. You would place your code here before the goto :EOF command.
 

goto :EOF
 

:GETDATEPARTS
 

set dt=%~1

set tok=1-3
 

if "%dt:~0,1%" GTR "9" set tok=2-4
 

set yyyy=
 

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (

  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c

)
 

if not "%yyyy%"=="" set yy=%yyyy%
 

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))

if 1%mm% LSS 100 set mm=0%mm%

if 1%dd% LSS 100 set dd=0%dd%
 

goto :EOF

Open in new window

0
 
LVL 1

Author Comment

by:jjrr007
ID: 20385321
You are very good at this.  This code looks excellent!

 I won't be able to test it without being able to add the "txt" extension to the basic copy and rename statements that I have.  Could you please let me know how I can that? I can then test this. Thanks again.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 500 total points
ID: 20385355
If the format of the file is simply YYYYMMYY.txt then you'd say something like this. Of course you'd want to change the drive and directories of the source and destination files as necessary.


@echo off

 

setlocal

 

call :GETDATEPARTS "%date%"

 

set curDate=%yy%%mm%%dd%

 

REM ** Here's your line:

if exist "c:\sourceDir\%curDate%.txt" move "c:\sourceDir\%curDate%.txt" "c:\destDir"

 

goto :EOF

 

:GETDATEPARTS

 

set dt=%~1

set tok=1-3

 

if "%dt:~0,1%" GTR "9" set tok=2-4

 

set yyyy=

 

for /f "tokens=%tok% delims=.:/-, " %%a in ('echo %~1') do (

  for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do set %%x=%%a&set %%y=%%b&set %%z=%%c

)

 

if not "%yyyy%"=="" set yy=%yyyy%

 

if 1%yy% LSS 1000 (if %yy% LSS 70 (set yy=20%yy%) else (set yy=19%yy%))

if 1%mm% LSS 100 set mm=0%mm%

if 1%dd% LSS 100 set dd=0%dd%

 

goto :EOF

Open in new window

0
 
LVL 1

Author Closing Comment

by:jjrr007
ID: 31412033
You are  a wonderful programmer.  You did this with great speed. Thank you!!
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20385538
Thanks :) I'm glad it worked for you.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

12 Experts available now in Live!

Get 1:1 Help Now