1.

Solve : Moving files to a new directory?

Answer»

Good evening all, I am a new user so forgive me if this question has already been posted.
I have used ..

@echo off
date /t>%test%\date.txt
for /f "tokens=1,2,3 delims=/" %%a in (%test%\date.txt) do mkdir c:\test\%%a%%b%%c
move c:\test\*.txt ??
pause

to create a directory (ddmmyyyy), I now need to add a line to the script that will move a selection (all *.txt) files to this directory. Moving files is easy - but how can I specify the destination ?

Any help would be appreciated.Quote from: chrismac_88 on July 13, 2007, 11:58:16 AM

Moving files is easy - but how can I specify the destination ?

You seem to be aiming to:-

- Generate the name of a directory from today's date.
- Create that directory.
- Move the contents of an already existing directory into the new directory.

You are grabbing the date by using a one-line FOR to split out the three tokens, day, month, and year, separated by the / character, of the output of the date /t command. Then you use these to generate the directory name.

First you need to make the directory using mkdir (or its shorter form, md)

Quote
mkdir [directory name]

Then you need to move the files using the move command

Quote
move [filespec] [directory name]

So you need to use the directory name twice.

Trouble is, the generated directory name -- %%a%%b%%c -- only exists while the FOR statement is actually being processed. I can think of 3 ways around this.

(i) You can get multiple commands on one line by using the & character.

Quote
date /t>%test%\date.txt
for /f "tokens=1,2,3 delims=/" %%a in (%test%\date.txt) do mkdir c:\test\%%a%%b%%c & move c:\test\*.txt c:\test\%%a%%b%%c

(ii) You can EXTEND the FOR statement over multiple lines using parentheses.

Indenting of lines between the opening and closing parentheses is optional, but it is often done to improve readability of batch/command files.

Quote
date /t>%test%\date.txt
for /f "tokens=1,2,3 delims=/" %%a in (%test%\date.txt) do (
mkdir c:\test\%%a%%b%%c
move c:\test\*.txt c:\test\%%a%%b%%c
)

(iii) You can copy the date stuff into a VARIABLE and then it is available for reuse.

Quote
for /f "tokens=1,2,3 delims=/" %%a in (%test%\date.txt) do set foldername=c:\test\%%a%%b%%c
mkdir %foldername%
move c:\test\*.txt %foldername%

A couple of little tips you may or may not find USEFUL...

(1) You are writing the output of date /t into a file and then reading that file back. With FOR you can get at the command output by single-quoting the command.

What I mean is,

Why do this

Quote
date /t>%test%\date.txt
for /f "tokens=1,2,3 delims=/" %%a in (%test%\date.txt) do mkdir c:\test\%%a%%b%%c

When you can do this?

Quote
for /f "tokens=1,2,3 delims=/" %%a in ('date /t') do mkdir c:\test\%%a%%b%%c

No temp file to CLEAN up, less disk activity.

(2) If you really have to use a file to hold stuff, and you won't need it again, consider putting it in your Windows temp folder. You can read the %temp% system variable to get its location.

Quote
date /t>%temp%\date.txt
for /f "tokens=1,2,3 delims=/" %%a in (%temp%\date.txt) do mkdir c:\test\%%a%%b%%c
Thanks Contrex - It works perfectly..


Discussion

No Comment Found