# copy file to folder with batch file



## nomad (Sep 30, 1999)

I am creating a batch file to ask me for a folder name then attach that folder name to a dated folder then within that folder create a folder called Issued then another folder in that one with the date again. Then copy a batch file called "Date.bat" to the issued folder. It all works except it keeps copying the batch file "Date.bat" to the current directory whien I want to copy it to the Issued folder. Can someone help me out please?

@echo off
cls
set /p folder=Enter Folder Name:
mkdir %DATE:/=.%" "-" "%folder%\Issued\%DATE:/=.%
copy C:\Users\tom\Desktop\date.bat %CD%

thanx


----------



## foxidrive (Oct 20, 2012)

```
@echo off
cls
set /p "folder=Enter Folder Name: "
set "f=%DATE:/=.%"
mkdir "%f% - %folder%\Issued\%f%"
copy "C:\Users\tom\Desktop\date.bat" "%f% - %folder%\Issued"
```


----------



## nomad (Sep 30, 1999)

thanks foxidrive. Worked lik a charm... but if i can ask you a question why didnt mine work?

thanx


----------



## foxidrive (Oct 20, 2012)

You used %cd% as the target which is the current working directory, and the name can contain spaces and poison characters so the double quotes are also necessary.

You had to use the path to the issued folder - you are specifying this in a relative way (to the current working directory) rather than using the drive letter and full path, too.


----------



## nomad (Sep 30, 1999)

thanks for the help foxidrive


----------



## nomad (Sep 30, 1999)

If I could I would like to re-open this thread. Further to what I have I would like to copy a file from my desk top and rename it with the following date format and prefix it with a Q-.

Q-mmddyy-hhmm
e.g. Q-100313-0658.

The file name is template.xlsm

The following is what I have. All works fine except the renaming of the file.

@echo off
cls
set /p "company=Enter Company Name: "
set /p "project=Enter project Name: "
set "f=%DATE:/=.%"
set "t=%time:/=.%"
mkdir "%company%\%f% - %project%\Issued\%f%"
mkdir "%company%\%f% - %project%\Pics"
copy "C:\Users\tom\Desktop\date.bat" "%company%\%f% - %project%\Issued"
copy "C:\Users\tom\Desktop\template.xlsm" "%company%\%f% - %project%"
copy "C:\Users\tom\Desktop\template.CDL" "%company%\%f% - %project%"
rename "%company%\%f% - %project%\template.xlsm" "Q-%f%_%t%.xlsm"

thanx


----------



## foxidrive (Oct 20, 2012)

nomad said:


> All works fine except the renaming of the file.


What is the actual problem?

Does this do what you need? Run your code with an echo before the rename - see what is printed.


```
rename "%company%\%f% - %project%\template.xlsm" "Q-%f:.=%_%t:.=%.xlsm"
```


----------



## foxidrive (Oct 20, 2012)

I notice that you have a file called date.bat in your script and date is an internal command so it is better to call it something else.


----------



## nomad (Sep 30, 1999)

Hi have change the date.bat to dat.bat. I have also follwed your instruction by putting an echo command before the rename statement and the dos screen doesnt stay up long enought for me to read it.... or have I misunderstood. My file template does not rename. I would like it to be renamed like Q-mmddyy-hhmm e.e. Q-100413-0755. Is this possible to do... thx


----------



## foxidrive (Oct 20, 2012)

you can start a cmd window and then type in the name of your batch file. The window will remain open (unless you have an exit command to close the window).

Or: put a pause command after the rename command.


----------



## nomad (Sep 30, 1999)

I dont get any error messages and it does copy the file. It just doesnt rename it.


----------



## foxidrive (Oct 20, 2012)

Use this command in place of your rename command. Paste what is printed on the console into a reply.


```
echo rename "%company%\%f% - %project%\template.xlsm" "Q-%f:.=%_%t:.=%.xlsm"
```


----------



## nomad (Sep 30, 1999)

I tried it again and it says it has renamed the file but when I go there the file name has not change. What happens if you run the bat?

thanx


----------



## nomad (Sep 30, 1999)

there must be something wrong with my rename command. Now I have moved template.xlsm to a folder on my destop and run the following and it still doesnt work.

@echo off
cls
echo ren "C:\Users\tom\Desktop\Batch\template.xlsm" "C:\Users\tom\Desktop\Batch\test.xlsm"
pause


----------



## foxidrive (Oct 20, 2012)

I asked you to paste what it prints to the console - so we can see where the actual problem lies.

There are either spaces where there shouldn't be, illegal characters in the name or path, or the path is wrong.

That command above is incorrect - the target name cannot include the path.


----------



## nomad (Sep 30, 1999)

Sorry for taking so long to reply but I have been away. Since my last post I have tried to simplify this batch file to narrow down where the problem is. You asked me run the echo and pause command which I have done and below are the results. Also below is what I have for my batch file. When the batch runs it copies the file over and in the command window it says it renamed the file but it did not. Could you please offer more assistance? 
After I run this batch the command window reads as follows:

Enter Company Name: company
Enter project Name: project
1 file(s) copied.
rename "company\Oct.15.2013 - project\template.xlsm" "Q-Oct152013_ 7:28:3800.xlsm"
Press any key to continue . . .

@echo off
cls
set /p "company=Enter Company Name: "
set /p "project=Enter project Name: "
set "f=%DATE:/=.%"
set "t=%time:/=.%"
mkdir "%company%\%f% - %project%\Issued\%f%"
copy "C:\Users\tom\Desktop\template.xlsm" "%company%\%f% - %project%"
echo rename "%company%\%f% - %project%\template.xlsm" "Q-%f:.=%_%t:.=%.xlsm"
pause

Thanks for your time.


----------



## foxidrive (Oct 20, 2012)

Change this line

set "t=%time:/=.%"

To this:

set "t=%time::=.%"


----------

