Solved

# batchfile to create folder (date) and move files according to filename (date)

Posted on 2014-07-16
2,266 Views
Hi "Experts" :-),
I have two cameras taking pictures of a construction work every 30 sec 24/7.
The files all ends up in a folder and I need to have them moved to a folder for each day.
The file format is :
image14-07-16_15-29-20-41.jpg
image14-07-16_15-28-50-41.jpg
image14-07-16_15-28-20-41.jpg

Currently I have 90.000 files to sort and it would be a lot easyer to use a batch file script or whatever can solve the task.

I expect the program to create the dir for each day and copy the relevant files to that dir.
The files are on a Win 2008R2 server.

Hope someone has the knowledge to solve the task,

Br

Henrik / Denmark
0
Question by:Soeder

LVL 5

Expert Comment

Hi,

Below command will help you but you need to explore this command more :

forfiles /P "c:\data\PRODDB\dir" /M *.DMP /D +0 /C "cmd /c copy @path d:\test"

Regards,
Arjun
0

LVL 82

Accepted Solution

Try this; it just uses the file's name to determine the target folder name. the variable "FolderSorted" contains the root folder where you want the pictures to end up; the "date" folder will be appended.
The script is currently in test mode and will only display the commands it would normally run; remove the uppercase ECHOs in lines 10 and 11 to run it for real:
@echo off
setlocal enabledelayedexpansion
set FolderIncoming=C:\Temp
set FolderSorted=C:\Temp
for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" ECHO md "%FolderSorted%\!TargetFolder!"
ECHO move "%%a" "%FolderSorted%\!TargetFolder!"
)

0

Author Comment

Hi contributors,
I have tried both suggestions without any luck.
If you can be more concrete to the task or if anybody else have a better solution please get back to me.
Br

Henrik
0

LVL 82

Expert Comment

"without any luck" is not really helpful as error description.
What exactly happened when you tried to run my script?
Please create a folder "C:\Temp", and copy 5 of the image files in question into this folder.
Open a new command prompt, "cd" into the folder where you saved my script, and run it without any changes.
What is its output?
Note that you can right-click the command prompt's title bar in order to select the output, you don't have to re-type it.
0

Author Comment

Hi oBdA,
sorry for not giving you a more specific feedback.
I have made a folder on an external disk Q called test.
I then copied some thousand files into it an created a .bat file named sorter.bat with the following content:

setlocal enabledelayedexpansion
set FolderIncoming=Q:\Test
set FolderSorted=Q:\Test

for %%a in ("%FolderIncoming%\%FileMask%") do (
set FileName=%%~na
echo Processing '!FileName!' ...
set TargetFolder=!FileName:~5,8!
if not exist "%FolderSorted%\!TargetFolder!" md "%FolderSorted%\!TargetFolder!"
move "%%a" "%FolderSorted%\!TargetFolder!"

When I run the bat file, it flashes a dos prompt window for a second and nothing have changed.
I runs so fast that I cant see what is happening.
I then tried to put some pause commands into the script to get hold of what happened, but it seems like they are ignored, it still just executes the script with a short flash.
I have tried to place the bat file in and out of the folder to see if that had any influence, but the result were the same.

I would appreciate a lot if you would help me get this up and running.

Br

Henrik
0

LVL 82

Expert Comment

As I said: Open a command prompt (by entering cmd.exe into the "run" box), enter "cd /d T:\he\Path\to\the\script.cmd", and enter the script name. That way, the command prompt will remain open when the script is done, and even errors will be displayed.
One error I can see in the script you posted is that the closing ")" in the last line is missing. Note the "Select all" button under the code box which makes it easier to copy script to the clipboard without missing anything.
0

Author Comment

Hi oBdA,
The script is WORKING :-)
Thank you for your patience and time to guide me.

Br
Henrik / Happy Dane
0

## Featured Post

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…