Computing.Net > Forums > Programming > Copy and rename with a batch script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Copy and rename with a batch script

Reply to Message Icon

Name: mitchell311
Date: December 27, 2006 at 11:17:15 Pacific
OS: XP Pro SP 2
CPU/Ram: 2 gigs
Product: Dell
Comment:

I am going to create a batch file and put it in the send to folder. Users are going to right click on excel files and send to this batch file which will copy the file and place in a network drive and append the date to it. I know it sounds crazy but I do not want users to do this on their own.



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: December 28, 2006 at 02:40:11 Pacific
Reply:

I assume you mean to append the date to the filename, not the extension.

Try the BAT below. It's hardwired to copy to n:\data so you'll want to tailor it to your drive/directory.

::== datedata.bat

@echo off
setLocal EnableDelayedExpansion
call :sub1
for %%A in (%1) do (
copy %%A n:\data\%%~nA%YYYY%%MM%%DD%%%~xA
)

:sub1

:: get sys YMD into vars
@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
goto :eof

:sub1
> %1.# echo %~z1
goto :eof

:: DONE


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 2
Name: mitchell311
Date: December 28, 2006 at 04:43:12 Pacific
Reply:

Thanks a lot for the code, its great. Two questions for you. Is there a way to have files that have spaces in the file name still work? Also, is there a way to have the time in the file name as well, right after the date?


0

Response Number 3
Name: Mechanix2Go
Date: December 28, 2006 at 05:07:39 Pacific
Reply:

I'll give it a go, but it looks pretty messy.



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 4
Name: mitchell311
Date: December 28, 2006 at 06:35:04 Pacific
Reply:

M2, what I am worried about is users still copying files in over the older ones. Would it be easier to setup a counter for this? to have a counter start after the date in the filename. Like 01, 02, ...


0

Response Number 5
Name: Mechanix2Go
Date: December 28, 2006 at 07:09:01 Pacific
Reply:

I think I can hook up the time stamp.

Do this:

echo %TIME%>time.txt

and paste in the contents of time.txt, sp I know your layout.

Using filenames with spaces may prove to be a real bear.


=====================================
If at first you don't succeed, you're about average.

M2



0

Related Posts

See More



Response Number 6
Name: mitchell311
Date: December 28, 2006 at 07:15:35 Pacific
Reply:

Here is what I got:

10:14:46.74

EST


0

Response Number 7
Name: Mechanix2Go
Date: December 28, 2006 at 10:48:56 Pacific
Reply:

This will append the tine:

::== M5.bat
@echo off
::setLocal EnableDelayedExpansion
call :sub0
for %%A in (%1) do (
copy "%%A" "n:\data\%%~nA%YYYY%%MM%%DD%%TIME:~0,2%%TIME:~3,2%%%~xA"
)
goto :eof

:sub0

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
::echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 8
Name: mitchell311
Date: December 28, 2006 at 11:00:43 Pacific
Reply:

Thats great, thanks man. I was thinking about spaces in filenames and I was wondering if you think it is a good idea to have a script first remove the spaces then call this batch file. Do you think this would work?


0

Response Number 9
Name: Mechanix2Go
Date: December 28, 2006 at 11:17:20 Pacific
Reply:

That's a good idea. I'll check it out.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 10
Name: IVO
Date: December 29, 2006 at 06:39:31 Pacific
Reply:

Hi mitchell311 and M2,

the following slightly modified "incipit" of the batch solves the space problem by replacing blanks with underscores

@Echo Off
SetLocal EnableDelayedExpansion
Set NoSpace=%*
Set NoSpace=%NoSpace: =_%
Call :sub1
For %%A in (%NoSpace%) Do (
......


0

Response Number 11
Name: mitchell311
Date: December 29, 2006 at 06:51:51 Pacific
Reply:

IVO

I have put the following script in and nothing happens. I am missing something?
Thanks for the help


0

Response Number 12
Name: IVO
Date: December 29, 2006 at 13:49:07 Pacific
Reply:

The code I posted is not a new script, just a variant of the M2's batch where two lines are added at the beginning

Set NoSpace=%*
Set NoSpace=%NoSpace: =_%

(i.e. %NoSpace:<blank>=<underscore>%)
and the NoSpace variable used instead of %1 in the For clause.

That replaces the batch command's tail blanks with underscores, i.e. (assumed the script is X.bat)

X My file name.xls becomes My_file_name.xls in the body of the script.

I tested it and the code worked fine, but you have to run the whole M2 script with the above fixes not just the code I posted.


0

Response Number 13
Name: mitchell311
Date: January 2, 2007 at 05:59:29 Pacific
Reply:

IVO
Thats what I did and still no luck. I am posting the code and if you can see anything let me know. Thanks

@echo off
::setLocal EnableDelayedExpansion
::set NoSpace=%*
::set NoSpace=%NoSpace: =_%
call :sub0
for %%A in (%NoSpace%) do (
copy "%%A" "C:\%%~nA_%YYYY%%MM%%DD%_%TIME:~0,2%%TIME:~3,2%%%~xA"
)
goto :eof

:sub0

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
::echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof


0

Response Number 14
Name: Mechanix2Go
Date: January 2, 2007 at 08:24:27 Pacific
Reply:

For atarters, any line beginning with :: does nothing. Beyond that I need to figure out where IVO's lines belong. I'll work on it.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 15
Name: IVO
Date: January 2, 2007 at 12:23:46 Pacific
Reply:

Hi mitchell311,

as M2 says if you code

::setLocal EnableDelayedExpansion
::set NoSpace=%*
::set NoSpace=%NoSpace: =_%

the above lines are interpreted as comments and no action is performed (i.e. the NoSpace variable is not set to the command's tail and so on...).

So please, specify if what you posted is the code you actually run or the :: were added for some unspecified reason after test performed.


0

Response Number 16
Name: mitchell311
Date: January 2, 2007 at 12:42:08 Pacific
Reply:

Here is the code I am using. It works with file names that have no spaces but once there is a space in a file name, nothing happens. Thanks for you help guys.

@echo off
setLocal EnableDelayedExpansion
set NoSpace=%*
set NoSpace=%NoSpace: =_%
call :sub0
for %%A in (%NoSpace%) do (
copy "%%A" "C:\%%~nA_%YYYY%%MM%%DD%_%TIME:~0,2%%TIME:~3,2%%%~xA"
)
goto :eof

:sub0

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
::echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof


0

Response Number 17
Name: IVO
Date: January 2, 2007 at 13:21:45 Pacific
Reply:

FOUND!

Replace the following line

Copy "%%A"...

with

Copy "%*"...

and you are done... I hope...


0

Response Number 18
Name: mitchell311
Date: January 3, 2007 at 04:32:44 Pacific
Reply:

IVO

I change that and it still works with one word file names and any filenames that have spaces I get this error:
The system cannot find the file specified.

I even tried changed the %%A in this line to %* and had no luck.

for %%A in (%NoSpace%) do (



0

Response Number 19
Name: IVO
Date: January 3, 2007 at 06:22:07 Pacific
Reply:

Please add Echo in front of Copy "%*" and report the echoed line and the filename you are processing. The For statement is right with %%A.

Later I'll invetigate what you post as now I have to leave.

Thanks and sorry if I'm wasting your time.


0

Response Number 20
Name: mitchell311
Date: January 3, 2007 at 06:41:40 Pacific
Reply:

IVO, here is what I get. You are not wasting my time at all. Thanks for the help.


for %A in ("C:\test\test_copy.xls") do (
copy "C:\test\test copy.xls" "C:\%~nA_20070103_ 937%~xA"


0

Response Number 21
Name: IVO
Date: January 3, 2007 at 12:16:42 Pacific
Reply:

OK, Houston to mitchell311... try this

For %%A in (%*) Do (
Copy %* "C\%%~nA...

Report what you get, there are other shots on the gun if tht fails. (beware to set the double % in front of the ~)


0

Response Number 22
Name: mitchell311
Date: January 3, 2007 at 12:45:01 Pacific
Reply:

IVO

Save the shots, we are good. You guys are good.

IVO and M2, Thank you very much for all the help.


0

Response Number 23
Name: Mechanix2Go
Date: January 3, 2007 at 14:08:33 Pacific
Reply:

Please post final version.


=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 24
Name: IVO
Date: January 3, 2007 at 14:35:32 Pacific
Reply:

Hi M2,

the final version is just

@Echo Off
Call :sub0
For %%A in (%*) Do (
Copy %* "C:\%%~nA_%YYYY%%MM%%DD%_%TIME:~0,2%%TIME:~3,2%%%~xA"
)
GoTo :EOF

:sub0

........

Note that the filename on the command's tail is embraced by " (i.e. "My File Name.xls").

The above version allowed mitchell311 to fly to the moon.


0

Response Number 25
Name: Mechanix2Go
Date: January 4, 2007 at 02:25:04 Pacific
Reply:

Hi IVO,

I had to modify it top this:

::===========================
@Echo Off
For %%A in ("%*") Do (
echo Copy "%*" "C:\%%~nA_%YYYY%%MM%%DD%_%TIME:~0,2%%TIME:~3,2%%%~xA"
)
==================================

The one you posted gave me this:

C:\temp\-\rendate>fin test two.txt
Copy test two.txt "C:\test__1721"
Copy test two.txt "C:\two__1721.txt"
=========================================
I've really lost the plot on this one.

Yet another reason that LFNs should be left for a real OS, ie unix.



=====================================
If at first you don't succeed, you're about average.

M2



0

Response Number 26
Name: mitchell311
Date: January 4, 2007 at 08:05:26 Pacific
Reply:

Here is the final version that works:

---------------
@echo off
setLocal EnableDelayedExpansion
set NoSpace=%*
set NoSpace=%NoSpace: =_%
call :sub0
for %%A in (%*) do (
copy %* "C:\%%~nA_%YYYY%%MM%%DD%_%TIME:~0,2%%TIME:~3,2%%%~xA"
)
goto :eof

:sub0

:: get sys YMD into vars

@echo off

:: YYYY getter
> syyyy.d echo a 100
>> syyyy.d echo mov ah,2a
>> syyyy.d echo int 21
>> syyyy.d echo.
>> syyyy.d echo p=100 2
>> syyyy.d echo n sizeYYYY
>> syyyy.d echo w
>> syyyy.d echo q
debug < syyyy.d > nul
:: OK

:: MM getter
> sMM.d echo a 100
>> sMM.d echo mov ah,2a
>> sMM.d echo int 21
>> sMM.d echo mov cx,0
>> sMM.d echo mov cl,dh
>> sMM.d echo.
>> sMM.d echo p=100 4
>> sMM.d echo n sizeMM
>> sMM.d echo w
>> sMM.d echo q
debug < sMM.d > nul
:: OK

:: DD getter
> sDD.d echo a 100
>> sDD.d echo mov ah,2a
>> sDD.d echo int 21
>> sDD.d echo mov cx,0
>> sDD.d echo mov cl,dl
>> sDD.d echo.
>> sDD.d echo p=100 4
>> sDD.d echo n sizeDD
>> sDD.d echo w
>> sDD.d echo q
debug < sDD.d > nul
:: OK
del *.d

for %%F in (sizeYYYY sizeMM sizeDD) do call :sub1 %%F
set /p YYYY=<sizeYYYY.#
set /p MM=<sizeMM.#
if %MM% LSS 10 set MM=0%MM%
set /p DD=<sizeDD.#
if %DD% LSS 10 set DD=0%DD%
del size*.*
::echo YYYYMMDD=%YYYY%%MM%%DD%
goto :eof

:sub1
> %1.# echo %~z1
goto :eof


---------------


0

Response Number 27
Name: IVO
Date: January 5, 2007 at 03:03:32 Pacific
Reply:

Hi M2 and mitchell311,

To explain why the *correct* ultimate version is what I posted run the following sample batch

@Echo Off
Echo FileName is %*
For %%A in (%*) Do (Echo n=%%~nA x=%%~xA)

assumed the batch's name is X.bat, type

X My File Name.xls

then

X "My File Name.xls"

You see without doubts that parsing a variable embraced by " leads to the correct ultimate components.

The above sample suggests an exciting way to manipulate LFNs in batches, while indeed quite complex as nuclear physics is compared to ordinay mechanics.


0

Response Number 28
Name: mitchell311
Date: January 9, 2007 at 09:48:11 Pacific
Reply:

Sorry to ask you guys another question.

Is there a way to set this up so I can copy multiple files to this and not just one?


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Copy and rename with a batch script

Batch file copy and Rename www.computing.net/answers/programming/batch-file-copy-and-rename/18689.html

Need advice on a batch script www.computing.net/answers/programming/need-advice-on-a-batch-script/19701.html

Batch Script To FTP a file www.computing.net/answers/programming/batch-script-to-ftp-a-file/17008.html