Help Writing DOS Batch files

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
74,155
Office Version
  1. 365
Platform
  1. Windows
I am trying to copy a file from one lcoation to another using a batch (.bat) file, but I am trying to put a date stamp on it.

The trick is I am trying to put it in the middle, and while every file has the same prefix and suffix, but the middles are different (though always the same length).

My file name convention is:
DEP_nnnn.txt
where 'nnnn' = the 4 digit client number

So if I have two files, i.e.
DEP_1234.txt
DEP_0987.txt


I am trying to rename them:
DEP_1234_20060420.txt
DEP_0987_20060420.txt


I tried to do it with wildcards, but it doesn't appear that batch files like "?" as wildcards. Here is what I tried.
Code:
::  *** CAPTURE DATE FOR USE IN RENAMING 
C:
CD\
FOR /F "TOKENS=2,3,4 DELIMS=/ " %%I IN ('DATE/T') DO SET D=%%K%%I%%J

:: Put date stamp on files as they are moved to H drive
copy C:\TEMP\DEP_????.txt H:\DEP_????_%d%.txt

exit

Can anyone help?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Do you need to do this using batch files?

Couldn't you use VBA, namely the Name or FileCopy statements?
 
Upvote 0
This is an automated process that runs from a scheduler that uses no Excel or Access programs. It runs strictly from Batch files, as it also does a lot of archiving and zipping of files.

I would prefer not to introduce Excel or Access to the process, if necessary.
 
Upvote 0
No probs.:)

Just wondering really.

I'm afraid my DOS is a bit rusty, if not totally seized up so can't really help.

I think the last time I used it was when a friend's computer had died and I had
to locate the XP system restore exe.
 
Upvote 0
Hi jmiskey,
The problem is that %d% in your command adds an extra space into the command line :
Code:
copy C:\TEMP\DEP_1234.txt H:\DEP_1234_20060420 .txt
                                              /\
                there is an extra space here  ||
This can be solved :-D

Split the command into
copy C:\TEMP\DEP_????.txt H:\DEP_????_%d%
ren H:\DEP_* H:\DEP_*.txt

or in case there are other files like DEP_* use :
ren H:\DEP_????_???????? H:\DEP_????_????????.txt
 
Upvote 0
Bruno,

It didn't exactly work, but it got me on right track. My computer does not like the rename statements:
ren H:\DEP_* H:\DEP_*.txt
or
ren H:\DEP_????_???????? H:\DEP_????_????????.txt

I had to remove the path from the second part of each to get it to work, like this:
ren H:\DEP_* DEP_*.txt
or
ren H:\DEP_????_???????? DEP_????_????????.txt

Then it all worked out!

Thanks.
:-D
 
Upvote 0

Forum statistics

Threads
1,224,885
Messages
6,181,574
Members
453,055
Latest member
cope7895

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top