[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2014-07-16
7
Medium Priority
?
2,868 Views
Last Modified: 2014-07-25
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
Comment
Question by:Soeder
  • 3
  • 3
7 Comments
 
LVL 5

Expert Comment

by:Arjun Vyavahare
ID: 40199412
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"

I hope this will help you.

Regards,
Arjun
0
 
LVL 85

Accepted Solution

by:
oBdA earned 1000 total points
ID: 40199413
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 FileMask=image*.jpg
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!"
)

Open in new window

0
 

Author Comment

by:Soeder
ID: 40208937
Hi contributors,
thank you for your input.
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
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
LVL 85

Expert Comment

by:oBdA
ID: 40208958
"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

by:Soeder
ID: 40218580
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 FileMask=image*.jpg
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 85

Expert Comment

by:oBdA
ID: 40219245
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

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

I will credit your first answer.

Br
Henrik / Happy Dane
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 …
AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
Loops Section Overview
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question