Solved Need help with a batch file to create a folder & move files

October 16, 2012 at 11:46:19
Specs: Windows 7
I need to create a batch script to create a folder in the destination path and write some files to it. These files are some event log files. The next time I run the batch, I need the new set of files to be written in a new folder with time stamp without deleting the old files.
It has to create a new folder everytime it moves the files.

See More: Need help with a batch file to create a folder & move files

Report •


#1
October 16, 2012 at 16:32:46
Do you want to do any folder cleanup as part of the batch file, or will you be managing the old folders manually?

Do you really want a time stamp or did you mean a date stamp? Or both even?

In any case, here is the basic script. This can be changed as needed.

@echo off
;REM Batch file to create time-stamped folder and copy files
set TIMEFIXED=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%
md <PathToFolderLocation>\<FolderBaseName>-%TIMEFIXED%
xcopy /E /V /Y <PathToExistingFiles> <PathToFolderLocation>\<FolderBaseName-%TIMEFIXED%\

Replace the <PathToFolderLocation> entry with the path to where you want the new folder to be created when the script is run. Replace <FolderBaseName> with the base folder name (If applicable) that you want the folder to have before the time stamp area. Also replace the <PathToExistingFiles> entry with the location of the existing files that you want to copy to the new folder.

The TIMEFIXED variable is needed as the normal time output in DOS is separated by colons which cannot be used in folder names.

Let me know if you have any questions or need more assistance.

-----
IT Desktop & Network Consultant - MOS Master Certified, MCP, MCSA, MCITP - Windows 7, CCNA Certificate Pending, A+, Network +

::geek::


Report •

#2
October 17, 2012 at 11:33:56
Thank you so much! I was able to create the new folder successfully. But I am unable to move the files in the folder to the new location.
My requirement is to create the folder and move the files there. (Basically archiving the files--in my case; few log files to a different location)

Report •

#3
October 17, 2012 at 21:42:23
✔ Best Answer
What is the error message (if any) that you get when you try to move the files?

To move and not copy the files, replace the "xcopy" line in me example above with the following:

move /Y <PathToExistingFiles><PathToFolderLocation>\*.* <FolderBaseName-%TIMEFIXED%\

Make sure that you have the paths enclosed in quotation marks if they contain spaces.

Also the move command will only move files but will leave any sub-folders and files within those sub-folders alone. If you need to recursively move files and sub-folders, you will have to use a multi-step process of copying the files to the new location, ensuring that they match, then deleting the original files. Let me know if this is what you need and I can put together another example for you.

-----
IT Desktop & Network Consultant - MOS Master Certified, MCP, MCSA, MCITP - Windows 7, CCNA Certificate Pending, A+, Network +

::geek::


Report •

Related Solutions

#4
October 22, 2012 at 09:34:17
Great! This worked!! Thanks a lot for the help

Report •

#5
October 22, 2012 at 12:29:26
No problem.

Please remember to mark an answer as "Best Answer" so that others searching the forums looking for the same info are able to find it easier.

Glad I could help! :)

-----
IT Desktop & Network Consultant - MOS Master Certified, MCP, MCSA, MCITP - Windows 7, CCNA Certificate Pending, A+, Network +

::geek::


Report •


Ask Question